Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 列表(ArrayList)比较_Java_Collections - Fatal编程技术网

Java 列表(ArrayList)比较

Java 列表(ArrayList)比较,java,collections,Java,Collections,例如,我有两个列表,让我们列出newList和List oldList 1) 纽雷克 -->通过查找newList参数中不在oldList参数中的所有对象,为(newRec)生成对象列表 2) 新更新&& 3) 旧的更新 -->通过查找newList和oldList参数中存在但具有不同描述(xxx不匹配)的所有对象,生成要更新的对象的(“newUpdate”)和(“oldUpdate”)列表 4) 奥尔德雷克 -->通过查找oldList参数中不在newList参数中的所有对象,为(oldRe

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

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

  • 2) 新更新&&

  • 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;
   }
public Response maintainFieldDescriptions(列表newDescList,
列表(列表)
{
尝试
{
列表写入=新建ArrayList();
List updatesNew=newarraylist();
List updatesOld=new ArrayList();
列表删除=新建ArrayList();
if(newDescList!=null&&newDescList.size()>0)
{
对于(int i=0;i0)
{
对于(int j=0;j0)
{
对于(int i=0;i
在BarcodeFieldDesc类中

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

1)仅在newList中的对象列表

List newRec = new ArrayList(newList);
newRec.removeAll(oldList);
2) 你所说的“不同描述”是什么意思?“描述”是你放在列表中的对象的属性吗?在这种情况下,只是

List newUpdate = new ArrayList(newList);
newUpdate.removeAll(newRec);
-->提供新列表中的对象列表,这些对象也在旧列表中。这是您想要的吗

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

3) 仅oldList中的对象列表

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

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

1:

List<Object> newRec = new ArrayList<Object>();
for (Object obj : newList) {
    if (! oldList.contains(obj)) {
        newRec.add(obj);
    }
}
List newRec=newarraylist();
用于(对象对象对象:新建列表){
如果(!oldList.contains(obj)){
新增记录(obj);
}
}
2:

//注意:这假设“MyObject”有一个equals()实现
//忽略“描述”字段
List newUpdate=newarraylist();
List oldUpdate=new ArrayList();
对于(MyObject对象:新建列表){
if(oldList.contains(obj)){
MyObject oldObj=oldList.get(oldList.indexOf(obj));
如果(!oldObj.getDescription().equals(obj.getDescription())){
newUpdate.add(obj);
oldUpdate.add(oldObj);
}
}
}
3:

List oldRec=new ArrayList();
对于(对象对象对象:旧列表){
如果(!newList.contains(obj)){
旧记录添加(obj);
}
}

你为什么不给我们看看你为这个家庭作业问题尝试了哪些代码,也许我们可以告诉你哪里出了问题。那么你到目前为止完成了什么?你在哪里遇到了问题?问题是什么?@Howard实际上我无法建立第二个列表,即newUpadte和oldUpdate@vinod听起来更像是你在演歌剧java集合框架提供了诸如开箱即用的交叉点之类的操作@Howard给我一个想法,这样我就可以用集合来解决这类问题。。
//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);
        }
    }
}
List<Object> oldRec = new ArrayList<Object>();
for (Object obj : oldList) {
    if (! newList.contains(obj)) {
        oldRec.add(obj);
    }
}