开发:随笔记录之 判断list和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;
     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值