1、把list集合转换成Map
①
List<OrgResponDetail> oldDetails = new ArrayList();
Map<String,OrgResponDetail> map = oldDetails.stream()
.collect(Collectors.toMap(temp -> temp.getId(), temp -> temp));
②
Map<String,String> map = oldDetails.stream() .collect(Collectors.toMap(OrgResponDetail::getId,OrgResponDetail::getParentId));
2、把list转换为list
List<String> detailIds = list.stream()
.map(temp -> temp.getId()).collect(Collectors.toList());
//filter方法里的条件是作为筛选条件
List<Scheduling> resultList = list.stream()
.filter(distinctByKey(temp -> temp.getOrgId())).collect(Collectors.toList());
public static <T> Predicate<T> distinctByKey
(Function<? super T, Object> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
3、去重distinct()
List<String> oldDetails = new ArrayList();
List<String> list = list.stream().distinct().collect(Collectors.toList());
4、排序
//根据orders升序sorted方法
List itemVoList = list.stream().sorted(Comparator.comparing(DicGroupItemVo::getOrders))
.collect(Collectors.toList());
本文详细介绍如何使用Java Stream API进行数据转换,包括将List转换为Map,List内元素的去重、排序及筛选操作。通过具体代码示例,深入浅出地讲解Stream API在实际开发中的应用技巧。
1841

被折叠的 条评论
为什么被折叠?



