map的查询时间复杂度为1,为了提高效率经常会将list转为map进行查询
list转map
第一种:id为key,实体为value
Map<String, SalesProductinfo> stringListMap = selectiveFather.stream().collect(Collectors.toMap(SalesProductinfo::getId, account -> account));
多字段为key,实体为value
Map<String, HumanManpowerPipelineExcel> humanManpowerPipelineExcelMap =
humanManpowerPipelineExcelList.stream().collect
(Collectors.toMap(account-> convertDate(account.getEvaluationDate())+","+account.getDepartmentId(), account -> account));
第二种:id为key,name为value
Map<String, String> stringListMap = globalconstList.stream().collect(Collectors.toMap(Globalconst::getInteriOrid, Globalconst::getConstDisplayName));
第三种:id为key,list为value
globalConstList.stream()
.collect(Collectors.toMap(globalConst -> globalConst.getGlobalConst().toUpperCase(),
globalConst -> Lists.newArrayList(globalConst),
(List<Globalconst> newValueList, List<Globalconst> oldValueList) -> {
oldValueList.addAll(newValueList);
return oldValueList;
}));
第四种:多字段为key,list为value
Map<String,List<ResourcesModuleRank>> map = resourcesModuleRanks.stream().collect(Collectors.toMap(
p->{return p.getModule() + p.getTechLevel();},
p->{
List<ResourcesModuleRank> peo = new ArrayList<>();
peo.add(p);
return peo;
}));
第五种:list转单字段list
List<String> userIds = selective1.stream().map(ResourcesWorkloadCalculate::getModuleId).collect(Collectors.toList());
第六种:单字段为key,list为value
Map<String, List<ResourcesUserFactor> > stringsListMap = userFactorList .stream().collect(Collectors.groupingBy(ResourcesUserFactor::getUserAccount));
list转map分组
第一种:按照单字段分组
Map<String, List<ResourcesWorkloadCalculate>> groupByMap = selective.stream().collect(Collectors.groupingBy(ResourcesWorkloadCalculate::getModuleId,ResourcesWorkloadCalculate::getWorkType));
第二种:按照多字段分组
Map<String, List<ResourcesWorkloadCalculate>> groupByMap =selective.stream().collect(Collectors.groupingBy(o->convertDate(o.getEffectiveDate())+","+o.getWorkType()+","+o.getModuleId()));
第三种: 按照单字段分组,统计个数
Map<String, Long> map = selective.stream().collect(Collectors.groupingBy(ResourcesModuleRank::getModule, Collectors.counting()));
list转map特殊操作
不同实体的list之间转化,相同属性的赋值
List<ResourcesInfoCapacityExcel> resourceInfoExcels = resourcesInfoCapacities.stream().map(
todo -> new ResourcesInfoCapacityExcel(todo.getModelName(), todo.getExistingCapacity(), todo.getForecastWorkload(), todo.getDifferenceWorkload(), todo.getMonth()
)).collect(Collectors.toList());
循环map
for(String key:map.keySet()){//keySet获取map集合key的集合 然后在遍历key即可
String value = map.get(key).toString();//
System.out.println("key:"+key+" vlaue:"+value);
}
非stram
相同key将值相加,不同key put进map
Map<String, Double> foreWorkLoad = new HashMap<>();
foreWorkLoad.merge(key, resourcesWorkloadCalculate1.getWorkLoad(), (a, b) -> a + b);
list转map,相同key的list相加
Map<String, List<ResourcesInfo>> resourcesInfosListMap =new HashMap<>();
for (ResourcesInfo resourcesInfo:
resourcesInfos) {
String key=resourcesInfo.getModel() + "," + workDateMap.get(resourcesInfo.getMonth());
List<ResourcesInfo> moduleRanks = resourcesInfos.stream()
.filter(e -> resourcesInfo.getModel() == null || resourcesInfo.getModel().equals(e.getModel())
).filter(e -> resourcesInfo.getMonth() == null || resourcesInfo.getMonth().equals(e.getMonth())
).collect(Collectors.toList());
if (resourcesInfosListMap.get(key)==null){
resourcesInfosListMap.put(key,moduleRanks);
}else {
moduleRanks.addAll(resourcesInfosListMap.get(key));
resourcesInfosListMap.put(key,moduleRanks);
}
}
map转list
将Map转换为List的过程通常涉及将Map中的元素(键值对)转换为List的元素。具体如何转换取决于你希望List中包含什么:是仅包含键(keys)、仅包含值(values),还是包含键值对本身。以下是几种常见的转换方式:
将Map的键转换为List:
java
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
List<String> keys = new ArrayList<>(map.keySet());
System.out.println(keys); // 输出:[one, two, three]
将Map的值转换为List:
List<Integer> values = new ArrayList<>(map.values());
System.out.println(values); // 输出:[1, 2, 3]
将Map的键值对转换为List中的Entry对象:
List<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
System.out.println(entries); // 输出:[one=1, two=2, three=3] (这是Entry对象的toString表示)
如果你想要一个更简单的List,其中只包含值,并且每个键值对只被转换为一个元素(而不是Entry对象),你可以使用Java 8的Stream API来实现:
List<Integer> simpleList = map.values().stream().collect(Collectors.toList());
System.out.println(simpleList); // 输出:[1, 2, 3]
或者,如果你想要一个包含键和值的简单List(每个键值对转换为两个元素),你可以这样做:
List<Object> flatList = new ArrayList<>();
flatList.addAll(map.keySet());
flatList.addAll(map.values());
System.out.println(flatList); // 输出:[one, two, three, 1, 2, 3]
请注意,这种将键和值分开添加到List中的方法会丢失键值对之间的关联。如果键值对的关联很重要,那么最好使用包含Entry对象的List。
选择哪种转换方式取决于你的具体需求和你想要如何处理Map中的数据。
汇总
Map<String, String> map = new HashMap<>();
List<String> map= new ArrayList(map.keySet());
List<String> result2 = new ArrayList(map.values());
List<String> result3 = map.keySet().stream()
.collect(Collectors.toList());
List<String> result4 = map.values().stream()
.collect(Collectors.toList());
List<String> result5 = map.values().stream()
.filter(x -> !"apple".equalsIgnoreCase(x))
.collect(Collectors.toList());