美文网首页
Java8语法

Java8语法

作者: 超音速6 | 来源:发表于2021-11-23 18:53 被阅读0次

    转换成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);
    }
    

    相关文章

      网友评论

          本文标题:Java8语法

          本文链接:https://www.haomeiwen.com/subject/vqawtrtx.html