1、list<Bean>
过滤
List<student> list = studentList.stream.filter(student ->Obgects.equals(studengt.getSex(),"女")).collect(Collectors.toList());
2、list<Bean>
转List<String>
List<类> list = new ArrayList<>() ;
list.stream().map(类::get属性).collect(Collectors.toList()) ;
3、2、list<Map>
转List<String>
List<Map> deptEmp = new ArrayList<>() ;
List<String> departIds = deptEmp .stream()
.map(employee -> employee.get("departId").toString())
.collect(Collectors.toList());
4、List<Bean>
转map
public Map<Long, Account> getIdAccountMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getId, account -> account));
}
5、根据bean某一属性List<Bean>
转map
public Map<Long, String> getIdNameMap(List<Account> accounts) {
return accounts.stream().collect(Collectors.toMap(Account::getId, Account::getUsername));
}
6、List<Map>
转map
//使用
List<Map> teamEmpCount = tFtHrAttendaneHrGroupStatisticsMapper.getTeamEmpCount(params);
Map<String, Map> teamEmpCountMap = teamEmpCount.stream().collect(Collectors.toMap(this::groupIdKey, vo->vo));
private String groupIdKey(Map<String,Object> map){
return map.get("groupId").toString();
}
7、list<Bean>
根据某一属性去重
//去重
List<ClassEntity> distinctClass = classEntities.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.get去重属性1 + ";" + o.get去重属性2))), ArrayList::new));
8、List<Bean>
分组
Map<Byte,List<ActivityLecture>>groupBy= list.stream().collect(Collectors.groupingBy(ActivityLecture::getBusinessType));
9、List<Map>
分组
Map<String, List<Map<String, Object>>> mapGroup = list.stream().
collect(Collectors.groupingBy(e->e.get("xxx").toString()));
10、list分组且获取组内最新一条。
new ArrayList<>(tFtHrKpiEvaluateResultModifyEntities.stream().collect(Collectors.toMap(
TFtHrKpiEvaluateResultModifyEntity::getExecId,
v -> v,
(v1, v2) -> {
Date dataTime = v1.getCreateDate();
Date startDate1 = v2.getCreateDate();
if (dataTime == null || startDate1 == null) {
return dataTime != null ? v1 : v2; //拿到不为空的 ,都为空取后一条
}
return dataTime.after(startDate1) || dataTime.equals(startDate1) ? v1 : v2;
}
)).values());
11、List
//Bean是实体类
List<Bean> collect = datas.stream()
.map(map -> JSON.parseObject(JSON.toJSONString(map),Bean.class))
.collect(Collectors.toList());
12、List<实体>转List
//实体转map
List<Map> result = rows.stream().map(iter -> {
return JSON.parseObject(JSON.toJSONString(iter),Map.class);
}).collect(Collectors.toList());