List转map
1.普通转换【重复、为空时会报异常】
Map<Long, ProtocalItemDO> protocalMap = listDO.stream().collect(Collectors.toMap(ProtocalItemDO::getId, protocal -> protocal));
2.重载toMap方法转换【重复、为空时不报异常,后面的值会覆盖前面的值】
重复
Map<Long, ProtocalItemMaterielDO> materierMap = listDO.stream().collect(Collectors.toMap(ProtocalItemMaterielDO::getMaterielId , (p) -> p,(oldValue,newValue)->newValue));
为空
Map<Integer, String> locMap = locationInfos.parallelStream().collect(HashMap::new, (m, v) -> m.put(v.getLocNo(), v.getLocChar()), HashMap::putAll);
List<Integer> taxs = changeParamList.stream().map(item->Integer.valueOf(
item.getFeatures().substring(1))
).collect(Collectors.toList());
3.抽取两个属性
Map<Long, String> stringMap = companyModels.parallelStream().collect(Collectors.toMap(CompanyModel::getId,
CompanyModel::getName));
List集合转字符串
List -> String
List<String> skuIds = new ArrayList<>();
String skus = skuIds.stream().collect(Collectors.joining(","));
String -> List
List<String> result = Arrays.asList(str.split(","));
List<Long> ids = Arrays.asList(param.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());