青草味的懒羊羊  
List stream流
  • 使用Java8 stream流对List进行遍历、过滤、查询、去重、排序等,Java8提供了Stream(流)处理集合的关键抽象概念,它可以对集合进行的操作,可以执行非常复杂的查找、过滤和映射数据等操作。Stream API 借助于同样新出现的Lambda表达式,极大的提高编程效率和程序可读性
实例一
    Map<String,Object> map = new HashMap<String,Object>();
    map.put("name","张三");
    map.put("age","20");
    map.put("sex","男");
    Map<String,Object> map2 = new HashMap<String,Object>();
    map2.put("name","张四");
    map2.put("age","20");
    map2.put("sex","男");
    Map<String,Object> map3 = new HashMap<String,Object>();
    map3.put("name","张五");
    map3.put("age","20");
    map3.put("sex","男");
    List<Map<String,Object>> list = new ArrayList<Map<String,Object>();
    list.add(map);
    list.add(map2);l
    ist.add(map3);
  • .stream().map().collect()

  • 获取list1中的所有对象的name,放入另一个list数组,注意,这个.map()方法返回的还是流对象,而不是返回map的意思

 List<Object> nameList = list.stream().map(item -> item.get("name")).collect(Collectors.toList());
 System.out.println(nameList);
 //运行结果
 //[张三, 张四, 张五]
  • .stream().filter().map().collect()

  • 使用filter方法,筛选list1中的所有Bean对象,留下id属于ids中的Bean对象,并把它们的name字段转成一个list

//删除一个,便于看出差别来
 nameList.remove(0);
List<Object> name =list.stream().filter(item -> nameList.contains(item.get("name"))).map(item -> item.get("name")).collect(Collectors.toList());
System.out.println(name);
//运行结果
//[张四, 张五]
  • .stream().collect()
  • 把list1中的所有Bean对象转成map,其中,id是map的key,Bean对象本身是map的value
Map<Object,Object> changeMap = list.stream().collect(Collectors.toMap(item -> item.get("name"),e->e));
System.out.println(changeMap);
//运行结果
//{张四={sex=男, name=张四, age=20}, 张三={sex=男, name=张三, age=20}, 张五={sex=男, name=张五, age=20}}
  • .stream().filter().collect()
  • 按条件过滤后生成map,与上方的filter类似,只保留ids中有的id的bean对象
Map<Object,Object> changeMap2 = list.stream().filter(item -> nameList.contains(item.get("name"))).collect(Collectors.toMap(item -> item.get("name"),e -> e));
System.out.println(changeMap2);
//运行结果
//{张四={sex=男, name=张四, age=20}, 张五={sex=男, name=张五, age=20}}
实例二
package com.test.entity;
import lombok.Data;
@Data
public class Student {
    /**
     * id 主键
     */
    private Integer Id;

    /**
     * 姓名
     */
    private String name;

    /**
     * 年龄
     */
    private Integer age;

    /**
     * 年级
     */
    private String grade;

    /**
     * 班级
     */
    private String classNmae;

    public Student(Integer id, String name, Integer age, String grade, String classNmae) {
        Id = id;
        this.name = name;
        this.age = age;
        this.grade = grade;
        this.classNmae = classNmae;
    }
}
List进行遍历、过滤、查询、去重、排序等
 List<Student> studentList = new ArrayList<>();
studentList.add(new Student(3,"小明",16,"高一","3班"));
studentList.add(new Student(6,"小李",18,"高三","1班"));
studentList.add(new Student(9,"小王",16,"高一","2班"));
studentList.add(new Student(3,"小徐",17,"高二","4班"));
studentList.add(new Student(5,"小马",19,"高三","6班"));
  • 使用 forEach()遍历List
studentList.forEach(System.out::println);
//运行结果
/**
Student(Id=3, name=小明, age=16, grade=高一, classNmae=3班)
Student(Id=6, name=小李, age=18, grade=高三, classNmae=1班)
Student(Id=9, name=小王, age=16, grade=高一, classNmae=2班)
Student(Id=3, name=小徐, age=17, grade=高二, classNmae=4班)
Student(Id=5, name=小马, age=19, grade=高三, classNmae=6班) **/
  • findAny() 和 findFirst()
  • findFirst() 和 findAny() 都是获取列表中的第一条数据,但是findAny()操作,返回的元素是不确定的,对于同一个列表多次调用findAny()有可能会返回不同的值。使用findAny()是为了更高效的性能。如果是数据较少,串行地情况下,一般会返回第一个结果,如果是并行(parallelStream并行流)的情况,那就不能确保是第一个。
System.out.println("====================获取姓名为'小徐'的学生信息,如果没有找到则返回null====================");
Student selectOne = studentList.stream().filter(stu -> stu.getName().equals("小徐")).findAny().orElse(null);
Student selectTwo= studentList.stream().filter(stu -> stu.getName().equals("小徐")).findAny().get();
System.out.println(selectOne);
System.out.println(selectTwo);
//运行结果
//Student(Id=3, name=小徐, age=17, grade=高二, classNmae=4班)
//Student(Id=3, name=小徐, age=17, grade=高二, classNmae=4班)
  • 使用 distinct() 方法可以去除重复的数据。
  • 获取年纪列表,去除重复的数据
 System.out.println("====================对list去重====================");
List<String> gradeList = studentList.stream().map(stu -> stu.getGrade()).distinct().collect(Collectors.toList());
gradeList.forEach(System.out::println);
//运行结果
//高一
//高三
//高二
  • sorted()排序
 System.out.println("====================对list排序====================");
System.out.println("====================升序(默认)====================");
List<Student> ascList = studentList.stream().sorted(Comparator.comparing(Student::getId)).collect(Collectors.toList());
ascList.forEach(System.out::println);
System.out.println("====================降序====================");
List<Student> descList = studentList.stream().sorted(Comparator.comparing(Student::getId).reversed()).collect(Collectors.toList());
descList.forEach(System.out::println);
System.out.println("====================多条件降序排序====================");
List<Student> descConList = studentList.stream().sorted(Comparator.comparing(Student::getId).reversed().thenComparing(Comparator.comparing(Student::getAge)).reversed()).collect(Collectors.toList());
descConList.forEach(System.out::println);
//运行结果
====================对list排序====================
====================升序(默认)====================
Student(Id=3, name=小明, age=16, grade=高一, classNmae=3班)
Student(Id=3, name=小徐, age=17, grade=高二, classNmae=4班)
Student(Id=5, name=小马, age=19, grade=高三, classNmae=6班)
Student(Id=6, name=小李, age=18, grade=高三, classNmae=1班)
Student(Id=9, name=小王, age=16, grade=高一, classNmae=2班)
====================降序====================
Student(Id=9, name=小王, age=16, grade=高一, classNmae=2班)
Student(Id=6, name=小李, age=18, grade=高三, classNmae=1班)
Student(Id=5, name=小马, age=19, grade=高三, classNmae=6班)
Student(Id=3, name=小明, age=16, grade=高一, classNmae=3班)
Student(Id=3, name=小徐, age=17, grade=高二, classNmae=4班)
====================多条件降序排序====================
Student(Id=3, name=小徐, age=17, grade=高二, classNmae=4班)
Student(Id=3, name=小明, age=16, grade=高一, classNmae=3班)
Student(Id=5, name=小马, age=19, grade=高三, classNmae=6班)
Student(Id=6, name=小李, age=18, grade=高三, classNmae=1班)
Student(Id=9, name=小王, age=16, grade=高一, classNmae=2班)
  • groupingBy对list分组
  • 根据年级分组
 System.out.println("====================对List分组====================");
Map<String,List<Student>> groupStu = studentList.stream().collect(Collectors.groupingBy(Student::getGrade));
groupStu.forEach((key,value) ->{
    System.out.println("KEY:"+key);
    value.forEach(System.out::println);
    System.out.println("*************************");
});
System.out.println(groupStu);
//运行结果
====================对List分组====================
KEY:高三
Student(Id=6, name=小李, age=18, grade=高三, classNmae=1班)
Student(Id=5, name=小马, age=19, grade=高三, classNmae=6班)
*************************
KEY:高二
Student(Id=3, name=小徐, age=17, grade=高二, classNmae=4班)
*************************
KEY:高一
Student(Id=3, name=小明, age=16, grade=高一, classNmae=3班)
Student(Id=9, name=小王, age=16, grade=高一, classNmae=2班)
*************************
{高三=[Student(Id=6, name=小李, age=18, grade=高三, classNmae=1班), Student(Id=5, name=小马, age=19, grade=高三, classNmae=6班)], 高二=[Student(Id=3, name=小徐, age=17, grade=高二, classNmae=4班)], 高一=[Student(Id=3, name=小明, age=16, grade=高一, classNmae=3班), Student(Id=9, name=小王, age=16, grade=高一, classNmae=2班)]}
整理自

https://blog.csdn.net/BHSZZY/article/details/122860048
https://blog.csdn.net/qq_42021376/article/details/109141670

posted on 2022-10-27 15:19  青草味的懒羊羊  阅读(578)  评论(0)    收藏  举报