登陆

map三种赋值及map的get方法使用、Map集合一般用到的方法、map方法-处理请求参数和常用初始化方式

admin 2022-11-25 3人围观 ,发现0个评论

map三种赋值及map的get方法使用

一、map三种赋值

map String—>String
map String–>Object
map String–>List<对象>
1、
Map<Long, User> userMap = userRes.getData().stream().collect(Collectors.toMap(User::getId, i -> i));
List userChannelList = userChannelRemoteService.listAll().getData();

2、
Map<String, String> userChannelMap = userChannelList.stream()
.collect(Collectors.toMap(UserChannel::getChannelCode, UserChannel::getChannelName));

3、
if (CollectionUtil.isNotEmpty(sysDataDictList)) {
Map<String, List< SysDataDict>> map = sysDataDictList.stream()
.collect(Collectors.groupingBy(SysDataDict::getDictGroup));
map.forEach((key, value) -> {
itemConfigRespList.add(getCollectionItemConfigResp(key, value));
});
}

二、map的get方法使用

public static void main(String[] args) { Map<Integer, Object> map = new HashMap<Integer, Object>(); map.put(1,111); map.put(2,222); String s = "1"; Object o = map.get(s); sout(0) }   复制代码

Map集合一般用到的方法

public Set keySet()
将Map所有的key封装到一个Set集合
set…

public V get(Object key)
根据key获取Map对应的value
get…

public Set<Map.Entry<K,V>> entrySet()
获取所有的键值对对象集合

public Collection values()
将Map中所有的value封装到一个Collection体系的集合中

public static void main(String[] args) { 	Map<Integer, Object> map = new HashMap<Integer, Object>(); 	map.put(1,111); 	map.put(2,222); 	String s = "1"; 	Object o = map.get(s); 	sout(0); 	o = map.get(Long.parseLong(s)); 	sout(o); 	o = map.get(Integer.parseInt(s)); 	sout(o); }   复制代码

null null 111

 map方法-处理请求参数和常用初始化方式 --------------------  复制代码

private Map<String, String> getGeoHashHeatMapDataMap(String categoryInfo) { List categoryList = JSONObject.parseArray(categoryInfo, KeyValueData.class); Map<String, String> map = categoryList.stream() .collect(Collectors.toMap(KeyValueData::getK, KeyValueData::getV)); return map; }

List<UserCustomerServiceRecord> customerServiceRecordList =                     userCustomerServiceRemoteService.listStaffName().getData();             Map<String, UserCustomerServiceRecord> customerServiceRecordMap = customerServiceRecordList.stream()                     .collect(Collectors.toMap(UserCustomerServiceRecord::getStaffId, i -> i));  ```  分组map  ``` Map<String, List<SysDataDict>> dictGroupMap = dictGroupList.stream()                     .collect(Collectors.groupingBy(SysDataDict::getDictGroup));             Map<String, List<CollectShopCompareResp.ShopTemplateDetailResp>> detailMap = openShopDetailList.stream()                     .collect(Collectors.groupingBy(CollectShopCompareResp.ShopTemplateDetailResp::getDictGroup));  ```  list转成map map转成list 数据结构处理 --------------------------  ``` //将modelPropertyList数据结构处理成shopList                 List<CollectionItemContentResp> child = item.getChild();                 Map<String, String> childMap = child.stream()                         .collect(Collectors.toMap(CollectionItemContentResp::getDataKey,                                 CollectionItemContentResp::getValue));                 List<BunkSaveReq> shopList = new LinkedList<>();                 Iterator<String> iterator = childMap.keySet().iterator();                 while (iterator.hasNext()) {                     String key = iterator.next();                     BunkSaveReq bunkSaveReq = new BunkSaveReq();                     bunkSaveReq.setKey(key);                     bunkSaveReq.setValue(childMap.get(key));                     shopList.add(bunkSaveReq);                 }  ```  list转成map   map转成list  开发尽量用map,提升代码复杂能力  文章知识点与官方知识档案匹配,可进一步学习相关知识  > 本文使用 [文章同步助手](https://juejin.cn/post/6940875049587097631) 同步 复制代码
请发表您的评论
请关注微信公众号
微信二维码
不容错过
Powered By Z-BlogPHP