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

发布时间:2021-07-19 11:10:00 作者:小新
来源:亿速云 阅读:359

小编给大家分享一下如何判断List和Map是否相等并合并List中相同的Map,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

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

看完了这篇文章,相信你对“如何判断List和Map是否相等并合并List中相同的Map”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. Java中List、Set、Map区别
  2. java中map和list的区别是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

list map

上一篇:Python中有哪些万用公式

下一篇:python中的EasyOCR库是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》