java - 列表(ArrayList)比较

标签 java collections

示例我有两个列表让 List newList 和 List oldList,

  • 1) 新记录 --> 通过查找 newList 参数中但不在 oldList 参数中的所有对象,构建要 (newRec) 的对象列表。

  • 2) newUpdate &&

  • 3) 旧更新 --> 通过查找 newList 和 oldList 参数中都存在但具有不同描述(xxx 不匹配)的所有对象,构建要更新的 ("newUpdate") 和 ("oldUpdate") 对象列表。

  • 4) 旧记录 --> 通过查找 oldList 参数中不在 newList 参数中的所有对象,构建 (oldRec) 的对象列表。

所以我最终会得到四个列表,分别是 newRec、newUpdate、oldUpdate、oldRec ....

请帮助我.. 提前致谢

请引用我的方法..

public Response maintainFieldDescriptions(List<BarcodeFieldDesc> newDescList,
         List<BarcodeFieldDesc> oldDescList)
   {

      try
      {
         List<BarcodeFieldDesc> writes = new ArrayList<BarcodeFieldDesc>();
         List<BarcodeFieldDesc> updatesNew = new ArrayList<BarcodeFieldDesc>();
         List<BarcodeFieldDesc> updatesOld = new ArrayList<BarcodeFieldDesc>();
         List<BarcodeFieldDesc> deletes = new ArrayList<BarcodeFieldDesc>();

         if ( newDescList != null && newDescList.size() > 0 )
         {

            for ( int i = 0; i < newDescList.size(); i++ )
            {
               BarcodeFieldDesc temp = newDescList.get(i);
               boolean handled = false;

               if ( oldDescList != null && oldDescList.size() > 0 )
               {
                  for ( int j = 0; j < oldDescList.size(); j++ )
                  {
                     BarcodeFieldDesc temp2 = oldDescList.get(j);
                     if ( temp.getKey().equals(temp2.getKey()) )
                     {
                        handled = true;
                        // Keys match
                        if ( !temp.toString().equals(temp2.toString()) )
                        {
                           // Difference found
                           updatesNew.add(temp);
                           updatesOld.add(temp2);
                        }
                     }
                     else
                     {
                        // Keys do not match
                     }
                  }

               }
               if ( !handled )
               {
                  writes.add(temp);
               }
            }

         }

         if ( oldDescList != null && oldDescList.size() > 0 )
         {
            for ( int i = 0; i < oldDescList.size(); i++ )
            {
               BarcodeFieldDesc temp = oldDescList.get(i);
               boolean handled = false;
               for ( int j = 0; j < newDescList.size(); j++ )
               {
                  BarcodeFieldDesc temp2 = newDescList.get(j);
                  if ( temp.getKey().equals(temp2.getKey()) )
                  {
                     handled = true;
                  }
                  else
                  {
                     // Keys do not match
                  }
               }
               if ( !handled )
               {
                  deletes.add(temp);
               }
            }
         }

 public String getKey()
       {
          String result = "";
          result = result + StringUtil.pad(getFDPART(), 3, ' ', 'L');
          result = result + StringUtil.pad(getFDPROF(), 10, ' ', 'L');
          result = result + StringUtil.pad(getFDOTFT(), 20, ' ', 'L');
          result = result + StringUtil.pad(getFDLNGC(), 2, ' ', 'L');
          return result;
       }

   public String toString()
   {
      String result = "";
      result = result + StringUtil.pad(getFDPART(), 3, ' ', 'L');
      result = result + StringUtil.pad(getFDPROF(), 10, ' ', 'L');
      result = result + StringUtil.pad(getFDOTFT(), 20, ' ', 'L');
      result = result + StringUtil.pad(getFDLNGC(), 2, ' ', 'L');
      result = result + StringUtil.pad(getFDDESC(), 32, ' ', 'L');
      return result;
   }

位于 BarcodeFieldDesc 类中..

所以在这里,如果 newList 和 OldList 有元素,那么它不会创建 newUpdate 和 oldUpdate List..

最佳答案

1:

List<Object> newRec = new ArrayList<Object>();
for (Object obj : newList) {
    if (! oldList.contains(obj)) {
        newRec.add(obj);
    }
}

2:

//NOTE:  this assumes that 'MyObject' has an equals() implementation 
//       that ignores the 'description' field 
List<MyObject> newUpdate = new ArrayList<MyObject>();
List<MyObject> oldUpdate = new ArrayList<MyObject>();
for (MyObject obj : newList) {
    if (oldList.contains(obj)) {
        MyObject oldObj = oldList.get(oldList.indexOf(obj));
        if (! oldObj.getDescription().equals(obj.getDescription())) {
            newUpdate.add(obj);
            oldUpdate.add(oldObj);
        }
    }
}

3:

List<Object> oldRec = new ArrayList<Object>();
for (Object obj : oldList) {
    if (! newList.contains(obj)) {
        oldRec.add(obj);
    }
}

关于java - 列表(ArrayList)比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5368448/

相关文章:

java - Android:相同的代码行无法在单独的方法中工作

java - 使用递归的数字模式

java - Java 中字母的值

Java Set 获取重复项

由于 ArrayList 在主构造函数中出现 JavaFX 异常

java - 你如何在java中初始化一个字符串?

Java - 什么可以替代 Google Guava Ranges?

.net - COM Interop 的泛型集合有哪些替代方案?

python - Python 中的函数 "/"参数

java - 即使无法联系发现服务器,是否有办法启动 Discovery 客户端 Spring 应用程序?