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 streamList = Arrays.asList(2L,3L,4L,5L,6L,2L,3L,4L);

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

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

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

        //排序
        List 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 studentList = Arrays.asList(s1,s2);

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

        //组装二
        List stringList = Arrays.asList("1,2,3,4","2,3,4,5");
        List longList4 = stringList.stream().map(o -> o.replaceAll(",",""))
                .collect(Collectors.toList());
        longList4.forEach(System.out::print);
    }
}

你可能感兴趣的:(Java基础,Java,stream)