stream()转map转list、distinct()去重、判断空值、sorted排序正序多字段排序

  • Post author:
  • Post category:其他


package demo.io;

import demo.api.JavaBean.Student;
import org.junit.platform.commons.util.StringUtils;

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

/**
 * @Author: donghaotian
 * @Date: 2022-04-11 16:35
 */
public class stream流操作 {
    public static void main(String[] args) {
        Student student = new Student();
        student.setId(1);
        student.setName("dht");
        Student student2 = new Student();
        student2.setId(2);
        student2.setName("dht2");
        List<Student> studentList = new ArrayList<>();
        studentList.add(student);
        studentList.add(student2);

        //获取list<实体类>其中一个属性(可指定类型)
        List<Object> nameList = studentList.stream().map(Student::getName).collect(Collectors.toList());
        System.out.println("nameList = " + nameList);
        Object o = nameList.stream().reduce((x, y) -> x.toString() + " + " + y.toString()).get();
        System.out.println("list所有值相加 = " + o);
        //判断list<String>是否包含空字符串
        boolean b = nameList.stream().allMatch(name -> StringUtils.isBlank(name.toString()));
        System.out.println(b);
        //获取list<实体类>其中两个属性为map集合
        Map<Integer, String> map = studentList.stream().collect(Collectors.toMap(Student::getId, Student::getName));
        System.out.println("map = " + map);
        //获取list<实体类>其中两个属性为map集合,若id重复则获取后者
        Student student3 = new Student(2, "dht3");
        studentList.add(student3);
        Map<Integer, String> map2 = studentList.stream().collect(Collectors.toMap(Student::getId, Student::getName,
                (String key1, String key2) -> key2));
        System.out.println("若id重复则获取后者:map2 = " + map2);
        //获取list<实体类>其中两个属性为map集合,若id重复则获取后者
        Map<Integer, String> map3 = studentList.stream().collect(Collectors.toMap(Student::getId, Student::getName, (key1, key2) -> key1 + "," + key2));
        System.out.println("若id重复则获取两者:map3 = " + map3);
        //重要:重复时将重复key的数据组成集合
        // toMap()的第一个参数就是用来生成key值的,第二个参数就是用来生成value值的。
        // 第三个参数用在key值冲突的情况下:如果新元素产生的key在Map中已经出现过了,第三个参数就会定义解决的办法。
        Map<Integer, List<String>> map4 = studentList.stream().collect(Collectors.toMap(Student::getId,
                p -> {
                    List<String> getNameList = new ArrayList<>();
                    getNameList.add(p.getName());
                    return getNameList;
                },
                (List<String> value1, List<String> value2) -> {
                    value1.addAll(value2);
                    return value1;
                }
        ));
        System.out.println("重要:重复时将重复key的数据组成集合" + map4);

        //获取list<实体类>其中一个属性(有空值)
        Student student4 = new Student(4, null);
        // studentList.add(student4);
        List<Object> nameList2 = studentList.stream().map(sdsTest -> sdsTest.getName() == null ? "0" : sdsTest.getName()).collect(Collectors.toList());
        System.out.println("获取list<实体类>其中一个属性(有空值)nameList2 = " + nameList2);

        //去重(需使用@Data重写hashCode和equals)
        Student student5 = new Student(2, "dht3");
        studentList.add(student5);
        List<Integer> nameListNoDistinct = studentList.stream().map(Student::getId).collect(Collectors.toList());
        List<Integer> nameListDistinct = studentList.stream().distinct().map(Student::getId).collect(Collectors.toList());
        System.out.println("未去重 = " + nameListNoDistinct);
        System.out.println("distinct()去重 = " + nameListDistinct);

        //去重实体类的指定属性
        List<Integer> ids = new ArrayList<>();//用于临时存储TestValue的Name
        List<Student> studentDistinctId = studentList.stream().filter(
                v -> {
                    if (ids.contains(v.getId())) {
                        return false;
                    } else {
                        ids.add(v.getId());
                        return true;
                    }
                }).collect(Collectors.toList());
        System.out.println("去重实体类的指定属性id : " + studentDistinctId);

        //排序 加reversed()倒序
        List<Student> idListSorted = studentList.stream().sorted(Comparator.comparing(Student::getId, Comparator.nullsFirst(Integer::compareTo))//nullsFirst:空值放前面
                .reversed()//倒序(注意倒序后空值在后面)
                .thenComparing(Student::getName)//第二个排序字段正序
                .reversed()//倒序
        ).collect(Collectors.toList());
        System.out.println("排序 = " + idListSorted);

        //根据字段分组
        Map<String, List<Student>> map1 = studentList.stream().collect(Collectors.groupingBy(Student::getName));
        System.out.println("map1 = " + map1);

        System.out.println("studentList = " + studentList);

        //根据实体类某字段去重
       List<Student> studentDistinctByNameArrayList = studentList.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getName))), ArrayList::new));
        System.out.println("studentDistinctByNameArrayList = " + studentDistinctByNameArrayList);

        //求和
        System.out.println("求和:" + studentList.stream().mapToInt(Student::getId).sum());
    }
}



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