编辑
2023-02-01
学习记录
00
请注意,本文编写于 796 天前,最后修改于 447 天前,其中某些信息可能已经过时。

目录

单一List进行操作
多条List操作

单一List进行操作

public class StreamDemo { List<Student> list = null; //初始化数据 @Before public void beforetest() { list = Arrays.asList(new Student("Tom", 18, 88, 90), new Student("Jerry", 20, 77, 89), new Student("Lily", 17, 98, 79), new Student("Lucy", 19, 70, 80), new Student("LiLei", 18, 88, 90), new Student("HanMeiMei", 21, 87, 79)); } @Test public void streamtest() { // filter 过滤器返回还是一个stream流对象 //查询math成绩大于80的学生并遍历输出 list.stream().filter(e->e.getMath()>80).forEach(System.out::println);//.forEach(e->System.out.println(e)) //统计数量count System.out.println(list.stream().count()); //如统计总分大于160的人数 System.out.println(list.stream().filter(e->e.getEnglish()+e.getMath()>160).count()); //limit 取前n个值 list.stream().limit(3).forEach(System.out::println); //skip 跳过前n个 list.stream().skip(2).forEach(System.out::println); //distinct 去除重复数据 list.stream().distinct().forEach(System.out::println); //map 映射元素可以对元素进行操作 例如对每个学生年龄加1 list.stream().map(e->{ e.setAge(e.getAge()+1); return e; }).forEach(System.out::println); //sorted 排序 //升序 list.stream().sorted((a,b)->{ return a.getEnglish().compareTo(b.getEnglish()); }); //降序 list.stream().sorted((a,b)->{ return b.getEnglish().compareTo(a.getEnglish()); }); //返回第一个元素 Optional<Student> first = list.stream().findFirst(); System.out.println(first.get()); //返回任意一个元素 System.out.println(list.stream().findAny().get()); //anyMatch 是否匹配任意一元素 检查是否包含名字为Tom的 System.out.println(list.stream().anyMatch(e->e.getName().equals("Tom"))); //allMatch 是否匹配所有元素 System.out.println(list.stream().allMatch(e->e.getName().equals("Tom"))); //noneMatch 是否未匹配所有元素 System.out.println(list.stream().noneMatch(e->e.getName().equals("Tom"))); //findFirst 返回元素中第一个值 Student student = list.stream().findFirst().get(); //findAny 返回元素中任意一个值 Student student1 = list.stream().findAny().get(); //max 返回最大值 查询英语成绩最高的学生 Student student2 = list.stream().max((l1,l2)->l2.getEnglish().compareTo(l1.getEnglish())).get(); //min 最小值 将上面l1,l2位置对调 Student student3 = list.stream().max((l1,l2)->l2.getEnglish().compareTo(l1.getEnglish())).get(); //将流对象转为list list.stream().filter(e->e.getMath()>80).collect(Collectors.toList()); //将流转未set list.stream().filter(e->e.getMath()>80).collect(Collectors.toSet()); //对象中的某项进行统计 IntSummaryStatistics c = list.stream().collect(Collectors.summarizingInt(Student::getEnglish)); System.out.println(c); //IntSummaryStatistics{count=6, sum=507, min=79, average=84.500000, max=90} //获取list中的某两个元素 Map<String,String> map=list.Stream.collect(Collectors.toMap(Student::Name,Student::age)) //先过滤再获取list中的某两个元素 Map<String,String> map=list.Stream.filter(i->"张三".equals(i.getName)).collect(Collectors.toMap(Student::Name,Student::age))}} //把一个list对象的内容取出来,创建一个新的对象存进去,改为一个新的list List<StringSimilarityHanLPRequest> HanLPList=provinceDictEntities.stream().map(i->StringSimilarityHanLPRequest.builder().name(i.getOperProvinceName()).code(i.getOperProvinceCode()).build()).collect(Collectors.toList()); //将某个字符串对象取出转成字符串数组 String[] pictureIds = request.getSaveMetaDataMarkList().stream().map(i->i.getMarkPictureId()).toArray(String[]::new);

多条List操作

java
import java.util.ArrayList; import java.util.List; import static java.util.stream.Collectors.toList; public class Test { public static void main(String[] args) { List<String> list1 = new ArrayList<String>(); list1.add("1"); list1.add("2"); list1.add("3"); list1.add("5"); list1.add("6"); List<String> list2 = new ArrayList<String>(); list2.add("2"); list2.add("3"); list2.add("7"); list2.add("8"); // 交集 List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList()); System.out.println("---交集 intersection---"); intersection.parallelStream().forEach(System.out :: println); // 差集 (list1 - list2) List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList()); System.out.println("---差集 reduce1 (list1 - list2)---"); reduce1.parallelStream().forEach(System.out :: println); // 差集 (list2 - list1) List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList()); System.out.println("---差集 reduce2 (list2 - list1)---"); reduce2.parallelStream().forEach(System.out :: println); // 并集 List<String> listAll = list1.parallelStream().collect(toList()); List<String> listAll2 = list2.parallelStream().collect(toList()); listAll.addAll(listAll2); System.out.println("---并集 listAll---"); listAll.parallelStream().forEachOrdered(System.out :: println); // 去重并集 List<String> listAllDistinct = listAll.stream().distinct().collect(toList()); System.out.println("---得到去重并集 listAllDistinct---"); listAllDistinct.parallelStream().forEachOrdered(System.out :: println); System.out.println("---原来的List1---"); list1.parallelStream().forEachOrdered(System.out :: println); System.out.println("---原来的List2---"); list2.parallelStream().forEachOrdered(System.out :: println); //进阶 //有时候并不是整个list的对比,而是list中的某个元素和另外list的全部的某个元素对比 Entity entity=new Entity; entity.setName("123"); entity.setAge(123); List<Entity> list1= new ArrayList<Entity>(); list1.add(entity) List<Entity> list2= new ArrayList<Entity>(); list2.add(entity) List<Entity> intersection = list1.stream().filter(item -> list2.map(Entity::getAge).collect(Collectors.toList()).contains(item.getAge)).collect(toList()); } }

本文作者:Weee

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.14.8