List、Set、Map判斷兩個對象相等的標準
/** * 根據特定規格,判斷兩個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; }
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VeVb武林網的支持。
新聞熱點
疑難解答
圖片精選