Stream

转集合

1
List<String> strList=list.stream.map(T::get).collect(Collectors.toList);

根据分类分组

1
2
3
4
5
6
7
List<AppModel> list=new ArrayList<>();
Map<String, List<AppModel>> groupByCategoryList = appModelList
.stream()
.collect(
Collectors
.groupingBy(AppModel::getModelCategory)
);

分组后数据排序

1
2
3
4
5
6
7
8
9
Map<String, List<AppModel>> groupAppMap = new HashMap<>(50);
Map<String, List<AppModel>> groupByCategoryList
groupByCategoryList.forEach((s, appModels) -> {
List<AppModel> collect = appModels.stream()
.filter(appModel -> appModel.getSort() != null)
.sorted(Comparator.comparing(BaseEntity::getSort))
.collect(Collectors.toList());
groupAppMap.put(s, collect);
});

按照分类顺序获取数据

1
2
3
4
5
6
7
8
9
10
11
12
13
List<Map<String, Object>> categoryList;
categoryList.forEach(stringObjectMap -> {
String id = String.valueOf(stringObjectMap.get("id"));
String name = String.valueOf(stringObjectMap.get("name"));
String icon = String.valueOf(stringObjectMap.get("icon"));
List<AppModel> appModels = groupAppMap.get(id);
LinkedHashMap<String, Object> hashMap = new LinkedHashMap<>();
hashMap.put("name", name);
hashMap.put("icon", icon);
hashMap.put("apps", appModels);
linkedList.add(hashMap);
}
);

过滤

1
2
3
4
List<String> idList;
idList.stream().map(this::getById).filter(Objects::nonNull).forEach(docNo -> {
//执行操作
});

getById 是调用方法获取一个对象

去重

1
List<String> appIds = list.stream().distinct().collect(Collectors.toList());

重组Map

1
2
3
Map<String,String> map=historicProcessInstanceList
.stream()
.collect(Collectors.toMap(HistoricProcessInstance::getId,HistoricProcessInstance::getBusinessKey);
作者

Heng.Wang

发布于

2020-10-28

更新于

2023-09-20

许可协议

CC BY-NC-SA 4.0

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×