Java8语法

转换成List

@Test
public void t1(){
    List<String> list = Stream.of("1", "2", "3").collect(Collectors.toList());
    list.forEach(System.out::println);
}

转换成Set

@Test
public void t2(){
    Set<String> collect = Stream.of("1", "2", "3").collect(Collectors.toSet());
    collect.forEach(System.out::println);
}

转换成其他集合,比如TreeSet

@Test
public void t3(){
    TreeSet<String> collect = Stream.of("1", "2", "3").collect(Collectors.toCollection(TreeSet::new));
    collect.forEach(System.out::println);
}

计算平均值

@Test
public void t4() {
    Double collect = Stream.of("1", "2", "3").collect(Collectors.averagingInt(n -> Integer.parseInt(n)));
    System.out.println(collect);

    Double collect = Stream.of("1", "2", "3").collect(Collectors.averagingInt(Integer::parseInt));
    System.out.println(collect);
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容