判断List和Map是否相等并合并List中相同的Map
作者:执笔记忆的空白 时间:2022-12-29 02:11:10
List、Set、Map判断两个对象相等的标准
List:通过equals()方法比较返回true即可。
HashSet:先比较两个对象hashCode()方法返回的值是否相等,如果不相等就认为两个对象是不相等的,如果两个对象的hashCode相等就继续调用equals()方法进一步判断两个对象是否相等,如果equals()方法返回true认为两个对象相等,返回false认为两个对象不相等。
TreeSet:两个对象通过compareTo(Object obj)方法比较是否返回0:如果返回0,则认为相等,否则不相等。
HashMap、HashTable:(1)两个key通过equals()方法比较返回true,两个key的hashCode值也相等;(2)value与另外一个对象通过equals()方法比较返回true即可。
TreeMap:两个key值通过compareTo()方法返回0,TreeMap即认为这两个key是相等的。
/**
* 根据特定规格,判断两个Map是否相等
*/
private static boolean isEquals(Map<String, String> src, Map<String, String> dest, String[] samekey) {
boolean equals = true;
StringBuffer sbf_src = new StringBuffer();
StringBuffer sbf_dest = new StringBuffer();
for (int i = 0; i < samekey.length; i++) {
sbf_src.append(src.get(samekey[i]));
sbf_dest.append(dest.get(samekey[i]));
}
if (sbf_src.toString().equals(sbf_dest.toString())) {
equals = true;
} else {
equals = false;
}
return equals;
}
/**
* 获得list中有没有相同的keyMap(待需找的map)<br>
* 如果找到则返回这个list和keyMap相同Map的下标,否则返回-1
*/
private static int getEqualsMap(List<Map<String, String>> list, Map<String, String> keyMap, String[] samekey) {
int equalsIndex = -1;
for (int i = 0; i < list.size(); i++) {
Map<String, String> tempMap = list.get(i);
if (isEquals(tempMap, keyMap, samekey)) {
equalsIndex = i;
}
}
return equalsIndex;
}
/**
* 合并List中相同的Map
* @param list
* @return
*/
public static List<Map<String, String>> combineList(List<Map<String, String>> list, String[] samekey,String combinekey) {
List<Map<String, String>> retList = new ArrayList<Map<String, String>>();
for (int i = 0; i < list.size(); i++) {
Map<String, String> tempMap = list.get(i);
int equalsIndex = getEqualsMap(retList, tempMap, samekey);
if (-1 == equalsIndex) {
retList.add(tempMap);
} else {
String custSrc = retList.get(equalsIndex).get(combinekey);
int custSrcInt = Integer.parseInt(custSrc.substring(0, custSrc.length() - 1));
String custTemp = tempMap.get(combinekey);
int custTempInt = Integer.parseInt(custTemp.substring(0, custTemp.length() - 1));
String destCust = (custSrcInt + custTempInt) + custSrc.substring(custSrc.length() - 1);
retList.get(equalsIndex).put(combinekey, destCust);
}
}
return retList;
}
来源:https://blog.csdn.net/moneyshi/article/details/41081313
标签:list,map,相等,合并
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java中@SuppressWarnings注解用法详解
2023-09-20 23:11:40
java内存优化的方法总结
2022-12-01 22:25:20
springboot整合企微webhook机器人发送消息提醒
2023-10-10 01:29:08
![](https://img.aspxhome.com/file/2023/2/64222_0s.jpg)
解析Java中未被捕获的异常以及try语句的嵌套使用
2022-10-18 20:03:48
Flutter Widgets MediaQuery控件屏幕信息适配
2023-06-29 04:48:21
![](https://img.aspxhome.com/file/2023/2/120092_0s.jpg)
SSM框架+Plupload实现分块上传大文件示例
2023-06-01 01:58:21
![](https://img.aspxhome.com/file/2023/8/63048_0s.png)
重写hashCode()和equals()方法详细介绍
2023-11-24 16:13:33
![](https://img.aspxhome.com/file/2023/8/59198_0s.png)
java中SynchronizedList和Vector的区别详解
2023-08-23 10:13:12
Spring整合CXF webservice restful实例详解
2023-03-20 09:58:21
![](https://img.aspxhome.com/file/2023/3/70293_0s.jpg)
android使用PullToRefresh实现下拉刷新和上拉加载
2023-08-06 11:06:58
![](https://img.aspxhome.com/file/2023/5/85455_0s.png)
Java基础题新手练习(二)
2022-03-10 00:11:57
![](https://img.aspxhome.com/file/2023/5/69705_0s.jpg)
java实现双色球彩票游戏
2022-06-29 12:39:45
![](https://img.aspxhome.com/file/2023/2/66652_0s.jpg)
Java实现简易界面通讯录
2023-02-09 20:12:59
![](https://img.aspxhome.com/file/2023/2/71912_0s.jpg)
C# 运用params修饰符来实现变长参数传递的方法
2023-07-21 22:27:55
解决springboot URL带有斜杠的转义字符百分之2F导致的400错误
2022-05-08 07:27:32
![](https://img.aspxhome.com/file/2023/3/75703_0s.png)
java 如何判断是否是26个英文字母
2023-11-29 15:51:59
java8 集合 多字段 分组 统计个数代码
2022-12-07 21:03:34
![](https://img.aspxhome.com/file/2023/5/72325_0s.jpg)
详解java接口(interface)在不同JDK版本中的变化
2022-07-18 03:19:16
mybatis查询返回Map<String,Object>类型的讲解
2022-12-25 02:07:38
![](https://img.aspxhome.com/file/2023/4/59534_0s.png)
java并发编程专题(五)----详解(JUC)ReentrantLock
2023-12-01 04:04:09