小编给大家分享一下如何判断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<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; }
看完了这篇文章,相信你对“如何判断List和Map是否相等并合并List中相同的Map”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
分享文章:如何判断List和Map是否相等并合并List中相同的Map
链接地址:https://www.cdcxhl.com/article2/igosoc.html
成都网站建设公司_创新互联,为您提供用户体验、建站公司、营销型网站建设、Google、静态网站、品牌网站制作
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联