Java中的Stream使用

测试类,TODO为我们要做的事,代码在下面的分点中实现的

package com.company;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class testStreamFilter {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<Student>(5) {{
            add(new Student("张三", 22, "男"));
            add(new Student("李四", 19, "女"));
            add(new Student("王五", 34, "男"));
            add(new Student("赵六", 30, "男"));
            add(new Student("田七", 25, "女"));
        }};
		// TODO
      }
}

class Student {
    private String name;
    private int age;
    private String sex;

    public Student(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

1 filter 过滤

  //过滤集合
        List<Student> filterList = list.stream().filter(student -> student.getAge() == 22).collect(Collectors.toList());

2 比较后升序

        //排序 
        //升序 这种写法只能按照字符串进行排序
        List<Student> sortOneList = list.stream().sorted(Comparator.comparing(student -> student.getName())).collect(Collectors.toList());
        System.out.println("sortOneList = " + sortOneList);
        //升序 这种写法按照实体取属性的方式排序
        List<Student> sortTwoList = list.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
        System.out.println("sortTwoList = " + sortTwoList);

3 降序
其实就是将属性使用reversed函数进行 降序。

     List<Student> reversedList = list.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());
        System.out.println("reversedList = " + reversedList);

4 去重 distinct
1 实体根据属性去重

    //去重
        List<Student> distinctList = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(o->o.getName()))), ArrayList::new));
        System.out.println("distinctList = " + distinctList);

如果是要求两个字段就用下面的办法

 //实体中的对象去重
        List<Student> distinctList = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(o->o.getName()+";"+o.getAge()))), ArrayList::new));
        System.out.println("distinctList = " + distinctList);

2字符串集合去重

List unique = list.stream().distinct().collect(Collectors.toList());

5 跳过前面几个元素

 //跳过前面的5个元素
 List result2 = list.stream().skip(5).collect(Collectors.toList());
 System.out.println(result2);

6 只要前面5各元素

//只要前面的5个
 List result3 = list.stream().limit(5).collect(Collectors.toList());
 System.out.println(result3);

7对象属性中,增加值

        List<Integer> addList = list.stream().map(student -> student.getAge() + 1).collect(Collectors.toList());
        System.out.println("addList = " + addList);

8遍历获取对象中属性的值

        List<String> nameList = list.stream().map(student -> student.getName()).collect(Collectors.toList());
        System.out.println("nameList = " + nameList);

9使用flatMap进行扁平化处理数据

        //flatMap扁平化
        String[] words = {"Hello", "World"};
        //{H,e,l,l,o}, {W,o,r,l,d}
        List<String> result3 = Arrays.stream(words).map(w -> w.split("")).flatMap(Arrays::stream).distinct().collect(Collectors.toList());
        System.out.println(result3);

10将集合中的对象转为Map结构

        //结合中的对象转为Map结构
        Map<Integer, Student> studentMap = list.stream().collect(Collectors.toMap(Student::getAge, item -> item, (a, b) -> b));
        System.out.println("studentMap = " + studentMap);

11 取集合中属性按照都好拼接

        //集合中属性按照逗号拼接
        String nameJoinList = list.stream().map(student -> student.getName()).collect(Collectors.joining(","));
        System.out.println("nameJoinList = " + nameJoinList);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值