java8集合相关操作
2021-08-17 14:13:04 12 举报
AI智能生成
集合快速操作
作者其他创作
大纲/内容
List集合转Map集合
字段和对象的map
Map<String, User> detailMap = detailList.stream().collect(Collectors.toMap(User::getPoolId, a -> a,(k1,k2)->k1));<br>
两个字段的map
Map<Long, String> map = batchShopById.stream().collect(Collectors.toMap(Shop::getId, Shop::getShopName));
字段分组转map
Map<Integer, List<CategoryAndProduct>> groupBy = categoryAndProductList.stream().collect(Collectors.groupingBy(CategoryAndProduct::getType));
List集合去重
List存储基础数据类型去重
List<Long> filterList = list.stream().distinct().collect(Collectors.toList());
List存储复合类型去重
List<SkuPool> firstImportSkuPools = skuPools.stream().filter(item ->differenceList.contains(item.getSkuCode())).collect(Collectors.toList());
List集合过滤
根据某个字段过滤
List<User> filterList = list.stream().filter(user -> user.getId() > 5 && "1组".equals(user.group)).collect(Collectors.toList());
List获取某个字段的集合
List<Long> poolIdList = list.stream().map(ShopSkuPoolDetail::getPoolId).collect(Collectors.toList());
List集合按照字段排序
正序
resultList.stream().sorted(Comparator.comparing(Role::getCreateTime)).collect(Collectors.toList());
倒序
resultList.stream().sorted(Comparator.comparing(Role::getCreateTime).reversed()).collect(Collectors.toList());
List集合取交并差集
交集
list1.stream().filter(item -> list2.contains(item)).collect(toList());
差集
list1对list2取差集
list1.stream().filter(item -> !list2.contains(item)).collect(toList());
list2对list1取差集
list2.stream().filter(item -> !list1.contains(item)).collect(toList());
并集
list1.addAll(list2);
去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
listAll未去重的并集
容器对象转List集合
Iterator 转成 List
apache
导包
import org.apache.commons.collections.IteratorUtils;
Iterator<Element> myIterator = //some iterator
List<Element> myList=IteratorUtils.toList(myIterator);
List<Element> myList=IteratorUtils.toList(myIterator);
google
导包
import com.google.common.collect.Lists;
Iterator<Element> myIterator = //some iterator
List<Element> myList = Lists.newArrayList(myIterator);
List<Element> myList = Lists.newArrayList(myIterator);
数组转list
String[] split = s.split(",");
List<String> resultList= new ArrayList<>(Arrays.asList(split));
List<String> resultList= new ArrayList<>(Arrays.asList(split));
快速获取List集合
List<String> list = Arrays.asList("1", "2", "3");
list 转map,key重复问题
Map<Integer, String> map = listDuplicateKey.stream()
.collect(Collectors.toMap(User::getId, User::getName, (oldValue, newValue) -> newValue));
.collect(Collectors.toMap(User::getId, User::getName, (oldValue, newValue) -> newValue));
List集合转List<List>
List<List<Integer>> lists = Lists.partition(numList, 1000);
List集合分页
List<GoodsDTO> list = goodsList.stream().skip((request.getPageNo() - 1) * request.getPageSize()).limit(request.getPageSize())
.collect(Collectors.toList());
.collect(Collectors.toList());
0 条评论
下一页