java8 stream流操作集合交集,差集,并集,过滤,分组,去重,排序,聚合等

测试对象

public class Person {

    private  String  name;

    private  Integer  age;

    private  Integer  weight;


    public Person() {
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public Integer getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

测试数据

两个list的交集、差集、并集

package Map;

import com.alibaba.fastjson.JSON;

import java.util.*;
import java.util.stream.Collectors;


public class ListStream {
    public static void main(String[] args) {
      List<Person> list = new ArrayList<Person>();
        Person person1 = new Person();
        person1.setName("张三");
        person1.setAge(18);
        person1.setWeight(50);

        Person person2 = new Person();
        person2.setName("李四");
        person2.setAge(26);
        person2.setWeight(70);

        Person person3 = new Person();
        person3.setName("张三");
        person3.setAge(26);
        person3.setWeight(60);
        list.add(person1);
        list.add(person2);
        list.add(person3);

        List<String> list1 = new ArrayList<>();
        list1.add("1");
        list1.add("2");
        list1.add("3");

        List<String> list2 = new ArrayList<>();
        list2.add("2");
        
        // 交集
        List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());
        System.out.println("交集:"+JSON.toJSONString(intersection));

        // 差集 (list1 - list2)
        List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());
        System.out.println("差集1:"+JSON.toJSONString(reduce1));
        // 差集 (list2 - list1)
        List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(Collectors.toList());
        System.out.println("差集2:"+JSON.toJSONString(reduce2));
        // 并集
        List<String> listAll = list1.parallelStream().collect(Collectors.toList());
        List<String> listAll2 = list2.parallelStream().collect(Collectors.toList());
        listAll.addAll(listAll2);
        System.out.println("并集:"+JSON.toJSONString(listAll));

        // 去重并集
        List<String> listAllDistinct = listAll.stream().distinct().collect(Collectors.toList());
        System.out.println("去重并集:"+JSON.toJSONString(listAllDistinct));
    }

}

过滤

 //过滤出符合条件的数据 保留name等于张三
List<Person> temp = list.stream().filter(a -> a.getName().equals("张三")).collect(Collectors.toList());
//过滤出符合条件的数据 过滤name等于张三
List<Person> temp1 = list.stream().filter(a -> !a.getName().equals("张三")).collect(Collectors.toList());

分组

//List 以Name分组 
Map<String, List<Person>> groupBy = list.stream().collect(Collectors.groupingBy(Person::getName));

去重

//去重
List<String> collect = list.stream().map(Person::getName).distinct().collect(Collectors.toList());
//去重 返回list对象
List<Person> unique = list.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparingLong(Person::getAge))), ArrayList::new)
        );

排序

//排序-->升序
List<Person> sortName = list.stream().sorted(Comparator.comparing(Person::getAge)).collect(Collectors.toList());
String sortName1 = JSON.toJSONString(sortName);
System.out.println("升序:"+sortName1);
//排序-->降序
List<Person> temp2 = list.stream().sorted(Comparator.comparing(Person::getAge).reversed()).collect(Collectors.toList());
String sortName2 = JSON.toJSONString(temp2);
System.out.println("降序:"+sortName2);
//排序-->两个字段排序 如果年龄相同,再使用体重排序
List<Person> temp3 = list.stream().sorted(Comparator.comparing(Person::getAge).thenComparing(Person::getName)).collect(Collectors.toList());
String sortName3 = JSON.toJSONString(temp3);
System.out.println("两个字段排序:"+sortName3);

聚合-->求和,最大值,最小值,平均值,统计

//mapToInt(),mapToDouble(),mapToLong()等
list.stream().mapToDouble(Person::getHeight).sum();//和
list.stream().mapToDouble(Person::getHeight).max();//最大
list.stream().mapToDouble(Person::getHeight).min();//最小
list.stream().mapToDouble(Person::getHeight).average();//平均值
list.stream().mapToDouble(Person::getHeight).count();//统计

list中本身存的就是基本类型的数字

List<Integer> listInteger = Arrays.asList(10,20,30);
listInteger.add(10);
listInteger.add(20);
Integer sum= listInteger.stream().reduce(Integer::sum).orElse(0);
Integer max = listInteger.stream().reduce(Integer::max).orElse(0);

List集合拼接成逗号分隔的字符串

List<String> listStr = Arrays.asList("a", "b", "c");
//String自带方式
String s2 = String.join(",", listStr);
System.out.println(s2);
//用stream流
String s = listStr.stream().collect(Collectors.joining(","));
System.out.println(s);


版权声明:本文为yao583224426原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。