开发者

List (ArrayList) comparison

example I have two lists lets List newList, and List oldList,

  • 1) newRec --> Build a list of objects to (newRec) by finding all objects in the newList parameter that are not in the oldList parameter.

  • 2) newUpdate &&

  • 3) oldUpdate --> Build ("newUpdate") and ("oldUpdate") lists of objects to update by finding all objects that exist in both, the newList and oldList parameters, but have different descriptions (xxx does not match).

  • 4) oldRec --> Build a list of objects to (oldRec) by finding all objects in the oldList parameter that are not in the newList parameter.

so i finally i will get four list which are newRec, newUpdate, oldUpdate, oldRec ....

kindly help me.. Thanks In advance

please refer my method..

public Response maintainFie开发者_StackOverflow中文版ldDescriptions(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;
   }

which is in BarcodeFieldDesc class ..

so here, if newList and OldList has element, then it's not creating the newUpdate and 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);
    }
}


1) list of objects in newList only

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

2) What do you mean by "different descriptions"? Is "description" a property of the objects you are putting in the list? In that case, just

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

--> gives a list of objects in newList that are also in oldList. Is this what you want?

If yes, you can build oldUpdate the same way (after building next list, oldRec )

3) list of objects in oldList only

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

--

For it to work, you need to implement equals() correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜