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);

Java 8 中可以使用 Stream API 来实现集合之间的交集差集。 假设有两个 List 集合:list1 和 list2,分别包含一些元素,代码如下: ```java List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5); List<Integer> list2 = Arrays.asList(4, 5, 6, 7, 8); ``` 下面分别介绍如何实现集合之间的操作。 1. 取交集交集即获取两个集合中共同拥有的元素。可以使用 Stream API 的 `filter()` 方法和 `contains()` 方法实现。代码如下: ```java List<Integer> intersection = list1.stream() .filter(list2::contains) .collect(Collectors.toList()); System.out.println("交集:" + intersection); // 输出 [4, 5] ``` 2. 取即获取两个集合中所有的元素,但是。可以使用 Stream API 的 `distinct()` 方法和 `concat()` 方法实现。代码如下: ```java List<Integer> union = Stream.concat(list1.stream(), list2.stream()) .distinct() .collect(Collectors.toList()); System.out.println(":" + union); // 输出 [1, 2, 3, 4, 5, 6, 7, 8] ``` 3. 取差集差集即获取两个集合中不同的元素。可以使用 Stream API 的 `filter()` 方法和 `!contains()` 方法实现。代码如下: ```java List<Integer> diff1 = list1.stream() .filter(e -> !list2.contains(e)) .collect(Collectors.toList()); System.out.println("差集1:" + diff1); // 输出 [1, 2, 3] List<Integer> diff2 = list2.stream() .filter(e -> !list1.contains(e)) .collect(Collectors.toList()); System.out.println("差集2:" + diff2); // 输出 [6, 7, 8] ``` 4. 即获取两个集合中所有的元素,。可以使用 Stream API 的 `distinct()` 方法和 `flatMap()` 方法实现。代码如下: ```java List<Integer> distinctUnion = Stream.of(list1, list2) .flatMap(List::stream) .distinct() .collect(Collectors.toList()); System.out.println(":" + distinctUnion); // 输出 [1, 2, 3, 4, 5, 6, 7, 8] ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人类幼崽养成记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值