Java stream

首页

流处理

需求:从用户集合中获取id为基数的用户名称,得到名称集合

    public static void main(String[] args) {
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User(i, "name" + i);
            userList.add(user);
        }
        //集合转为流
        Stream<User> stream = userList.stream();
        //对流进行过滤,使用filter方法,传入Predicate<T>函数接口
        Stream<User> userStream = stream.filter(u -> u.getId() % 2 != 0);
        //将用户流转为字符串流,使用map方法,传入Function<T,R>函数接口
        Stream<String> stringStream = userStream.map(user -> user.getName());
        //将流转为集合,使用collect方法,传入Collector
        List<String> collect = stringStream.collect(Collectors.toList());
        System.out.println(collect);
    }

最终得到结果
[name1, name3, name5, name7, name9]

流创建的几种方式

一、将集合转为流

可以将list,set转为流

        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add("num" + i);
        }
        Stream<String> stream = list.stream();
        stream.forEach(System.out::println);
        System.out.println("----");
        Set<String> set = new HashSet<>();
        for (int i = 0; i < 10; i++) {
            set.add("set" + i);
        }
        Stream<String> stream1 = set.stream();
        stream1.forEach(System.out::println);

二、将数组转为流

        Integer[] nums = new Integer[]{1,2,3,4,5};
        Stream<Integer> stream2 = Arrays.stream(nums);
        stream2.forEach(System.out::println);

三、使用Stream类中的方法

使用of(),generate,iterate等方法创建流

        Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6);
        Stream<Integer> s1 = Stream.iterate(1, x -> x + 2).limit(10);
        s1.forEach(System.out::println);
        System.out.println("----");
        Stream<Double> s2 = Stream.generate(Math::random).limit(5);
        s2.forEach(System.out::println);

四、将文件转为流

        BufferedReader reader = new BufferedReader(new FileReader("D:\\a.txt"));
        Stream<String> lines = reader.lines();
        lines.forEach(System.out::println);
        System.out.println("----");

五、使用正则将字符串转为流

        Pattern pattern = Pattern.compile(",");
        Stream<String> stringStream = pattern.splitAsStream("a,b,c,d");
        stringStream.forEach(System.out::println);
        System.out.println("----");

流操作

过滤

使用filter方法过滤流中数据

    public static void main(String[] args){
        Integer[] nums = new Integer[]{1,2,3,4,5};
        Stream<Integer> stream = Arrays.stream(nums);
        stream.filter(i -> i>2).forEach(System.out::println);
    }

获取n个数据

使用limit(n)方法获取n个数据

    public static void main(String[] args){
        Integer[] nums = new Integer[]{1,2,3,4,5};
        Stream<Integer> stream = Arrays.stream(nums);
        stream.limit(2).forEach(System.out::println);
    }

跳过n个元素

使用skip(n)跳过n个元素

    public static void main(String[] args){
        Integer[] nums = new Integer[]{1,2,3,4,5};
        Stream<Integer> stream = Arrays.stream(nums);
        stream.skip(2).forEach(System.out::println);
    }

当limit和skip配合使用时,可以实现分页效果

去重

使用distinct()方法实现流中的元素去重

    public static void main(String[] args){
        Integer[] nums = new Integer[]{1,2,3,4,5,3,4,5};
        Stream<Integer> stream = Arrays.stream(nums);
        stream.distinct().forEach(System.out::println);
    }

映射新元素

使用map来映射新元素,传参为function<T,R>,流中每个数据都作为参数传递进去,返回一个新值,用返回的新值组成新的stream

    public static void main(String[] args){
        List<String> list = new ArrayList<>();
        list.add("a,b,c");
        list.add("1,2,3");
        list.stream().map(s -> s.replace(",","")).forEach(System.out::println);
    }

得到的结果为流中包含两个元素,分别为abc和123
请添加图片描述

映射新元素二

使用flatMap方法,将每个元素转为流,再将转换的流拼接为一个流

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("a,b,c");
        list.add("1,2,3");
        Stream<String> stringStream = list.stream().flatMap(s -> {
            String[] split = s.split(",");
            Stream<String> stream = Arrays.stream(split);
            return stream;
        });
        stringStream.forEach(System.out::println);
    }

得到的stringStream中包含了abc123这几个元素
请添加图片描述

默认排序

    public static void main(String[] args) {
        List<String> list = Arrays.asList("aa", "ab", "ba", "bb", "ac");
        list.stream().sorted().forEach(System.out::println);
    }

输出结果为
aa
ab
ac
ba
bb

自定义排序

自己编写排序规则进行排序

public class StreamDemo {
    public static void main(String[] args) {
        Student s1 = new Student(18, 150);
        Student s2 = new Student(16, 160);
        List<Student> students = Arrays.asList(s1, s2);
        students.stream().sorted((o1,o2) -> {
            //年龄正序排列
            return o1.getAge()-o2.getAge();
        }).forEach(System.out::println);
    }
}

class Student{
    private int age;
    private int height;

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

    public int getAge() {
        return age;
    }

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

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", height=" + height +
                '}';
    }
}

得到结果为
请添加图片描述

消费

使用peek方法,传入consume函数进行数据消费,消费完后,数据还是原来的数据,对比map,则是返回新的数据

public class StreamDemo {
    public static void main(String[] args) {
        Student s1 = new Student(18, 150);
        Student s2 = new Student(16, 160);
        List<Student> students = Arrays.asList(s1, s2);
        students.stream().peek(s -> {
            s.setHeight(200);
        }).forEach(System.out::println);
    }
}

class Student{
    private int age;
    private int height;

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

    public int getAge() {
        return age;
    }

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

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", height=" + height +
                '}';
    }
}

输出结果为两个学生身高都为200
请添加图片描述

判断流中数据是否满足条件

所有匹配

使用allMatch进行匹配

    public static void main(String[] args) {
        List<Integer> its = Arrays.asList(1, 2, 3, 4, 5);
        boolean b = its.stream().allMatch(i -> i > 3);
        System.out.println(b);
    }

流中元素不是全部满足,所以返回false

所有都不匹配

使用noneMatch,所有都不匹配时返回true

    public static void main(String[] args) {
        List<Integer> its = Arrays.asList(1, 2, 3, 4, 5);
        boolean b = its.stream().noneMatch(i -> i > 3);
        System.out.println(b);
    }

因为不是所有都不匹配,所以返回false

任意一个匹配

使用anyMatch进行任意一个匹配,任意一个匹配即返回true

    public static void main(String[] args) {
        List<Integer> its = Arrays.asList(1, 2, 3, 4, 5);
        boolean b = its.stream().anyMatch(i -> i > 3);
        System.out.println(b);
    }

因为流中有4、5等数据匹配,所以返回true

找到第一个

使用findFirst方法找到满足条件的第一个元素

    public static void main(String[] args) {
        List<Integer> its = Arrays.asList(1, 2, 3, 4, 5);
        its.stream().filter(o -> o > 3).findFirst().ifPresent(System.out::println);
    }

找到满足条件的第一个元素 4

找到任意一个

使用findAny方法找到任意一个元素

    public static void main(String[] args) {
        List<Integer> its = Arrays.asList(1, 2, 3, 4, 5);
        its.stream().filter(o -> o > 3).findAny().ifPresent(System.out::println);
    }

经过检验,与findFirst方法返回的内容一致,目前还没有发现其不一致的地方。待后面补充

找到流中最大的元素

使用max方法,找到流中最大值,传入Comparator函数

    public static void main(String[] args) {
        List<Integer> its = Arrays.asList(1, 2, 3, 4, 5);
        its.stream().filter(o -> o > 3).max(Integer::compareTo).ifPresent(System.out::println);
    }

找到流中最小的元素

使用min方法,找到流中最小值,传入Comparator函数

    public static void main(String[] args) {
        List<Integer> its = Arrays.asList(1, 2, 3, 4, 5);
        its.stream().filter(o -> o > 3).min(Integer::compareTo).ifPresent(System.out::println);
    }

计数,统计流中元素总数

    public static void main(String[] args) {
        List<Integer> its = Arrays.asList(1, 2, 3, 4, 5);
        long count = its.stream().filter(o -> o > 3).count();
        System.out.println(count);
    }

流中的顺序操作

需求,求1-100的和

    public static void main(String[] args) {
        List<Integer> its = getList(100);
        Integer integer = its.stream().reduce((x1, x2) -> x1 + x2).get();
        System.out.println(integer);
    }

    private static List<Integer> getList(int i) {
        List<Integer> its = new ArrayList<>();
        for (int i1 = 1; i1 <= i; i1++) {
            its.add(i1);
        }
        return its;
    }

使用reduce方法,第一次函数执行的时候,流中的第一个和第二个元素为参数。第二次函数执行的时候,第一个参数为上一次函数执行的结果,第二个参数为流中的第三个参数。
还可以使用reduce的另外一个方法进行求和

    public static void main(String[] args) {
        List<Integer> its = getList(100);
        Integer integer = its.stream().reduce(100,(x1, x2) -> x1 + x2);
        System.out.println(integer);
    }

    private static List<Integer> getList(int i) {
        List<Integer> its = new ArrayList<>();
        for (int i1 = 1; i1 <= i; i1++) {
            its.add(i1);
        }
        return its;
    }

此时reduce方法多传入了一个100的参数,这个时候,函数执行的第一个参数就是100,第二个参数是流中的第一个数据。第二次函数执行的时候,第一个参数为上一次函数执行的结果,第二个参数为流中的第二个数据。依此类推。此时得到的求和结果为5150,多了100

collector工具库操作

对象转为list集合

    public static void main(String[] args) {
        Person p1 = new Person(1, "wfc", 18);
        Person p2 = new Person(2, "lf", 18);
        Person p3 = new Person(3, "wy", 6);
        List<Person> people = Arrays.asList(p1, p2, p3);
        people.stream().map(Person::getName).collect(Collectors.toList()).forEach(System.out::println);
    }

获取所有name数据

获取set集合

    public static void main(String[] args) {
        Person p1 = new Person(1, "wfc", 18);
        Person p2 = new Person(2, "lf", 18);
        Person p3 = new Person(3, "wy", 6);
        List<Person> people = Arrays.asList(p1, p2, p3);
        people.stream().map(Person::getName).collect(Collectors.toSet()).forEach(System.out::println);
    }

获取map

    public static void main(String[] args) {
        Person p1 = new Person(1, "wfc", 18);
        Person p2 = new Person(2, "lf", 18);
        Person p3 = new Person(3, "wy", 6);
        List<Person> people = Arrays.asList(p1, p2, p3);
        people.stream().collect(Collectors.toMap(Person::getName, Person::getAge)).forEach((key, value) -> {
            System.out.println(key + "::" + value);
        });
    }

将内容转为map,获取的结果为
请添加图片描述

获取流中某个内容的最大值

    public static void main(String[] args) {
        Person p1 = new Person(1, "wfc", 18);
        Person p2 = new Person(2, "lf", 18);
        Person p3 = new Person(3, "wy", 6);
        List<Person> people = Arrays.asList(p1, p2, p3);
        Integer integer = people.stream().map(Person::getAge).collect(Collectors.maxBy(Integer::compareTo)).get();
        System.out.println(integer);
    }

获取流中某个内容的最小值

    public static void main(String[] args) {
        Person p1 = new Person(1, "wfc", 18);
        Person p2 = new Person(2, "lf", 18);
        Person p3 = new Person(3, "wy", 6);
        List<Person> people = Arrays.asList(p1, p2, p3);
        Integer integer = people.stream().map(Person::getAge).collect(Collectors.minBy(Integer::compareTo)).get();
        System.out.println(integer);
    }

获取流中某个数据的汇总值

    public static void main(String[] args) {
        Person p1 = new Person(1, "wfc", 18);
        Person p2 = new Person(2, "lf", 18);
        Person p3 = new Person(3, "wy", 6);
        List<Person> people = Arrays.asList(p1, p2, p3);
        Integer integer = people.stream().collect(Collectors.summingInt(Person::getAge));
        System.out.println(integer);
    }

获取流中某个数据的平均值

    public static void main(String[] args) {
        Person p1 = new Person(1, "wfc", 18);
        Person p2 = new Person(2, "lf", 18);
        Person p3 = new Person(3, "wy", 6);
        List<Person> people = Arrays.asList(p1, p2, p3);
        Double dd = people.stream().collect(Collectors.averagingDouble(Person::getAge));
        System.out.println(dd);
    }

根据流中某个数据进行分组,然后获取key为数据,value为list的map数据

    public static void main(String[] args) {
        Person p1 = new Person(1, "wfc", 18);
        Person p2 = new Person(2, "lf", 18);
        Person p3 = new Person(3, "wy", 6);
        List<Person> people = Arrays.asList(p1, p2, p3);
        Map<Integer, List<Person>> collect = people.stream().collect(Collectors.groupingBy(Person::getAge));
        System.out.println(collect);
    }

得到一个map,其中key为18的value有两个值,key为6的value有一个值。
请添加图片描述

根据流中的某个数据进行分区

    public static void main(String[] args) {
        Person p1 = new Person(1, "wfc", 18);
        Person p2 = new Person(2, "lf", 18);
        Person p3 = new Person(3, "wy", 6);
        List<Person> people = Arrays.asList(p1, p2, p3);
        Map<Boolean, List<Person>> collect = people.stream().collect(Collectors.partitioningBy(p -> p.getAge() > 10));
        System.out.println(collect);
    }

得到key为true和false的两个集合
请添加图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值