如何判斷List和Map是否相等并合并List中相同的Map

小編給大家分享一下如何判斷List和Map是否相等并合并List中相同的Map,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的阜平網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

List、Set、Map判斷兩個對象相等的標(biāo)準(zhǔn)

  • List:通過equals()方法比較返回true即可。

  • HashSet:先比較兩個對象hashCode()方法返回的值是否相等,如果不相等就認(rèn)為兩個對象是不相等的,如果兩個對象的hashCode相等就繼續(xù)調(diào)用equals()方法進一步判斷兩個對象是否相等,如果equals()方法返回true認(rèn)為兩個對象相等,返回false認(rèn)為兩個對象不相等。

  • TreeSet:兩個對象通過compareTo(Object obj)方法比較是否返回0:如果返回0,則認(rèn)為相等,否則不相等。

  • HashMap、HashTable:(1)兩個key通過equals()方法比較返回true,兩個key的hashCode值也相等;(2)value與另外一個對象通過equals()方法比較返回true即可。

  • TreeMap:兩個key值通過compareTo()方法返回0,TreeMap即認(rèn)為這兩個key是相等的。

/**
   * 根據(jù)特定規(guī)格,判斷兩個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的下標(biāo),否則返回-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”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

分享文章:如何判斷List和Map是否相等并合并List中相同的Map
鏈接地址:http://bm7419.com/article2/igosoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、建站公司、營銷型網(wǎng)站建設(shè)Google、靜態(tài)網(wǎng)站、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營