Java8streamforEachfilteranyMatch符:去重,排序,过滤,分组,统计
forEach的循
someObjects.forEach(obj -> {
//to do something
})
如果这个循环的⽬标是到匹配某个谓词的第⼀个元素
Optional<SomeObject> result =
someObjects.stream().filter(obj -> some_condition_met).findFirst();
如果你只是想知道集合中是否有⼀个元素的条件为真,你可以使⽤anyMatch:
boolean result = someObjects.stream().anyMatch(obj -> some_condition_met);
distinct()  :字符串去重
符:jdk8对list的各种处理实例详解,包括去重,排序,过滤,分组,统计
/*去重,去除重复对象(每个属性的值都⼀样的),需要注意的是要先重写对象TestStreamModel的equals和hashCode⽅法*/
System.out.println("去重前:" + list);
List<TestStreamModel> distinctList = list.stream().distinct().List());
System.out.println("去重后:" + distinctList);
/*排序,按id升续排列,如果要降续则改成:(a, b) -> b.getId() - a.getId(); a和b都是变量名(可以按⾃⼰意愿取名字),都是list中的对象的实例*/
System.out.println("排序前:" + list);
List<TestStreamModel> sortList = list.stream().sorted((a, b) -> a.getId() - b.getId()).List());
System.out.println("排序后" + sortList);
a.stream().sorted((i, j) -> Integer.("sum").toString()) - Integer.("sum").toString()))
a.stream().sorted(Comparatorparing(StudentInfo::getAge).reversed()).List());
/*过滤,按照⾃⼰的需求来筛选list中的数据,⽐如我筛选出不及格的(⼩于60分)的⼈,t为实例*/
System.out.println("过滤后:" + list);
List<TestStreamModel> filterList = list.stream().filter(t -> t.getScore() < 60).List());
System.out.println("过滤后" + filterList);
/*map, 提取对象中的某⼀元素,例⼦中我取的是每个⼈的name,注意list中类型对应,如果取的是id或者班级,就应该是integer类型*/
System.out.println("提取前:" + list);
sortedlist
List<String> mapList = list.stream().map(t -> t.getName()).List());
System.out.println("提取后:" + mapList);
/*统计,统计所有⼈分数的和, 主要我设置的分数属性是double类型的,所以⽤mapToDouble,如果是int类型的,则需要⽤mapToInt*/
double sum = list.stream().mapToDouble(t -> t.getScore()).sum();
int count = list.stream().mapToInt(t -> t.getId()).sum();
/*分组,按照字段中某个属性将list分组*/
Map<Integer, List<TestStreamModel>> map = list.stream().upingBy(t -> t.getGrade()));
System.out.println("按年级分组" + map);
/*然后再对map处理,这样就⽅便取出⾃⼰要的数据*/
for (Map.Entry<Integer, List<TestStreamModel>> entry : Set()) {
System.out.println("key:" + Key());
System.out.println("value:" + Value());
}
/*多重分组,先按年级分组,再按班级分组*/
Map<Integer/*年级id*/, Map<Integer/*班级id*/, List<TestStreamModel>>> groupMap = list.stream().upingBy(t -> t.getGrade(), upingBy(t -> t.getClasses())));
System.out.println("按照年级再按班级分组:" + groupMap);
System.out.println("取出⼀年级⼀班的list:" + (1).get(1));
/*多重分组,⼀般多重分组后都是为了统计,⽐如说统计每个年级,每个班的总分数*/
Map<Integer/*年级id*/, Map<Integer/*班级id*/, Double>> sumMap = list.stream().upingBy(t -> t.getGrade(), upingBy(t -> t.getClasses(), Collectors.summingDouble(t -> t.getScore()))));
System.out.println(sumMap);
System.out.println("取出⼀年级⼀班的总分:" + (1).get(1));
/*stream是链式的,这些功能是可以⼀起使⽤的,例如:计算每个年级每个班的及格⼈数*/
Map<Integer/*年级*/, Map<Integer/*班级*/, Long/*⼈数*/>> integerMap = list.stream().filter(t -> t.getScore() >= 60).upingBy(t -> t.getGrade(), upingBy(t -> t.getClasses(), unting())));        System.out.println("取出⼀年级⼀班及格⼈数:" + (1).get(1));
//将list转换map
Map<String, String> map = list.stream().Map(Person::getId, Person::getName));
Map<String, Appoint> collect = list.stream().Map(a -> (a.getSchoolId() + "-" + a.getTimeQuantumId() + "-" + a.getMachinPlace()), Function.identity()));
Map<String, Appoint> collect = list.stream().Map(a -> (a.getSchoolId() + "-" + a.getTimeQuantumId() + "-" + a.getMachinPlace()), a -> a));
//最⼤值   
Map<String, Object> tiyan = a.stream().filter(t -> t.get("tiyan") != null).max(Comparatorparing(m -> Integer.("tiyan").toString()))).get();
原⽂链接:blog.csdn/qq_33142257/article/details/62891612

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。