有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java列表(ArrayList)比较

例如,我有两个列表,让我们列出newList和List oldList

  • 1)newRec -->;通过查找newList参数中不在oldList参数中的所有对象,为(newRec)生成对象列表

  • 2)最新更新和&

  • 3)旧版本更新 -->;通过查找newList和oldList参数中都存在但具有不同描述(xxx不匹配)的所有对象,生成(“newUpdate”)和(“oldUpdate”)要更新的对象列表

  • 4)oldRec -->;通过查找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列表


共 (2) 个答案

  1. # 1 楼答案

    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);
        }
    }
    
  2. # 2 楼答案

    1)仅新列表中的对象列表

    List newRec = new ArrayList(newList);
    newRec.removeAll(oldList);
    

    2)你所说的“不同描述”是什么意思?“描述”是您要放入列表的对象的属性吗?那样的话,就

    List newUpdate = new ArrayList(newList);
    newUpdate.removeAll(newRec);
    

    >;给出newList中同时也在oldList中的对象的列表。这就是你想要的吗

    如果是,您可以以相同的方式构建oldUpdate(在构建下一个列表oldRec之后)

    3)仅oldList中的对象列表

    List oldRec = new ArrayList(oldList);
    oldList.removeAll(newList);
    

    要使其工作,您需要正确地实现equals()