使用HashSet<String>将map集合去重
来源:互联网 发布:逆战弹道优化 编辑:程序博客网 时间:2024/05/17 06:21
在前面的博客中讲过用map做小实体,接收数据。现在业务要求是:对实体集合去重;
一、总体思想:
使用HashSet<String>将map集合去重。
1、取出List<Entity> oldList中的userId,用HashSet<String>removalManagerIdList集合接收,自动去重;(可借鉴:利用HashSet对数组去重)
2、新建List<Entity> newList,遍历List<Entity> oldList 和 HashSet<String> removalManagerIdList,从List<Entity> oldList取出与HashSet<String> removalManagerIdList中对应的值,将新值保存到List<Entity> newList;
二、细节:
1、外循环:HashSet<String> removalManagerIdList,内循环:List<Entity> oldList;
2、问题:内循环中有重复值,一个外循环的userId每遍历一次内循环时,如果userId相同就会新建一个实体,这样仍然达不到去重的效果;解决方法:外循环找到userId相同的内循环实体时,给新实体赋值后,停止遍历内循环;
三、代码实现:
public void parseMap(List<AssetDeptManagerConfig> managerLists, AssetDeptManagerConfigVo assetDeptManagerConfigVo, List<HashMap<String, String>> mapList, String ManagerBizType) { // 使用HashSet<String>将map集合去重; HashSet<String> removalManagerIdList = new HashSet<>(); // 接收被去重的集合的userId for (HashMap<String, String> map : mapList) { removalManagerIdList.add(map.get("userId")); } // 遍历HashSet<String>,对每个managerId赋值(此时的removalManagerIdList中没有重复的userId) for (String removalManagerId : removalManagerIdList) { boolean equalTag = false; // 标识是否找到对应的userId for (HashMap<String, String> map : mapList) { if (map.get("userId").equals(removalManagerId)) { AssetDeptManagerConfig manager = new AssetDeptManagerConfig(); manager.setManagerId(map.get("userId")); manager.setManagerName(map.get("userName")); manager.setDeptId(assetDeptManagerConfigVo.getDeptId()); manager.setDeptName(assetDeptManagerConfigVo.getDeptName()); manager.setManagerBizType(ManagerBizType); manager.setManagerType(assetDeptManagerConfigVo.getManagerType()); manager.setOrgId(this.userService.getUser().getOrgId()); managerLists.add(manager); equalTag = true; // 找到userId,退出循环,进入下一个外循环; break; } } } }
四、其他方法:
为Entity重写equal方法:
set实体泛型对象去重(重写实体hashCode、equal方法)
阅读全文
0 0
- 使用HashSet<String>将map集合去重
- HashSet 集合 去重
- list<map<String ,object>> 去重,list map去重
- List<Map<String,Object>>去重,排序
- List<Map<String,Object>>去重,排序
- HashSet去重
- HashSet去重
- HashSet去重
- HashSet去重原理
- set hashSet 去重
- hashset去重
- HashSet去重原理
- hashset将list去重并拼接成字符串
- List<map<String,String>>根据选择的key去重
- java HashSet去重示例
- HashSet的去重问题
- HashSet添加对象去重
- JAVA集合框架之List HashSet去重及TreeSet排序详解
- 写论文,为什么要写参考文献?感觉完全不必要,把自己的研究成果说清楚不就行了?
- Angular 服务
- 使用CXF+spring+restful创建一个web的接口项目
- openVswitch(OVS)源代码分析之工作流程(flow流表查询)
- 兄弟连学Python(3)—python求职简历
- 使用HashSet<String>将map集合去重
- AngularJs添加删除排序Y
- 实验四
- 《揭秘Angular 2》-Typescript 函数
- Java 多线程学习笔记 4 多线程共享数据
- ※bzoj1009
- 文章标题
- 在程序中用new ClassPathXmlApplicationContext()获取Spring的上下文环境注意事项
- 20170621