Map
- entrySet遍历,在键和值都需要时使用(最常用)
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 2);
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
}
- key遍历
Map<Long, OtaTask> result = new LinkedHashMap<>();
for (Long taskId : result.keySet()) {}
3.map根据key排序
Map<Long, OtaTask> result = new LinkedHashMap<>();
// 倒叙
tasksMap.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByKey())).
forEachOrdered(x -> result.put(x.getKey(), x.getValue()));
// 正序
tasksMap.entrySet().stream().sorted(Map.Entry.comparingByKey()).
forEachOrdered(x -> result.put(x.getKey(), x.getValue()));
对象转map
import cn.hutool.core.bean.BeanUtil;
Map map = new HashMap();
BeanUtil.copyProperties(queryRecordForm, map);
标签:Map,map,entrySet,getValue,result,new,工具
From: https://www.cnblogs.com/cherychina/p/17309618.html