Java8 stream()常用方法

public class StreamDemo {

   static class Student{
        private int age;

        public Student(int age){
            this.age = age;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }

    public static void main(String[] args) {
        List<Long> streamList = Arrays.asList(2L,3L,4L,5L,6L,2L,3L,4L);

        //遍历
        List<Long> longList = streamList.stream().collect(Collectors.toList());
        //longList.forEach(System.out::print);

        //筛选
        List<Long> longList1 = streamList.stream().filter(o -> o>3).collect(Collectors.toList());
       // longList1.forEach(System.out::print);

        //去重
        List<Long> longList2 = longList1.stream().distinct().collect(Collectors.toList());
        //longList2.forEach(System.out::print);

        //排序
        List<Long> longList3 = streamList.stream().sorted().collect(Collectors.toList());
        //longList3.forEach(System.out::print);

        //统计
        long count = streamList.stream().count();
        //System.out.println(count);


        Student s1 = new Student(20);
        Student s2 = new Student(10);
        List<Student> studentList = Arrays.asList(s1,s2);

        //组装一
        List<Integer> ageList = studentList.stream().map(Student::getAge).collect(Collectors.toList());
       // ageList.forEach(System.out::print);

        //组装二
        List<String> stringList = Arrays.asList("1,2,3,4","2,3,4,5");
        List<String> longList4 = stringList.stream().map(o -> o.replaceAll(",",""))
                .collect(Collectors.toList());
        longList4.forEach(System.out::print);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值