判断List和Map是否相等并合并List中相同的Map

网友投稿 325 2023-01-17


判断List和Map是否相等并合并List中相同的Map

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 src, Map 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)

* 如果找到则返回这个list和keyMap相同Map的下标,否则返回-1

*/

private static int getEqualsMap(List> list, Map keyMap, String[] samekey) {

int equalsIndex = -1;

for (int i = 0; i < list.size(); i++) {

Map tempMap = list.get(i);

if (isEquals(tempMap, keyMap, samekey)) {

equalsIndex = i;

}

}

return equalsIndex;

}

/**

* 合并List中相同的Map

* @param list

* @return

*/

public static List> combineList(List> list, String[] samekey,String combinekey) {

List> retList = new ArrayList>();

for (int i =http:// 0; i < list.size(); i++) {

Map 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;

}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:石油研发管理平台(石油研发管理平台官网)
下一篇:spring循环注入异常问题的解决方案
相关文章

 发表评论

暂时没有评论,来抢沙发吧~