Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

HOw to delete duplicate values in two arraylists

i have one senario is i will get two Arraylists of HouseDocumentCharges      objects
one is breakBulkHouseModel.getViewChargesList() and breakBulkHouseModel.getChargesList();
breakBulkHouseModel.getChargesList() can come null in some cases;breakBulkHouseModel.getViewChargesList() doesn't comes null;Say  breakBulkHouseModel.getViewChargesList()  have charge codes 044,045,162,in breakBulkHouseModel.ChargesList() i have charge codes 045,162,166;
final o/p i want is HouseDocumentCharges object  which is having 045 chargecode in chargelist not in viewchargeList;i dont want other charge codes HouseDocumentCharges object;
i sense that some flaw in this logic;
i have written logic for that;Is this fine;
 for(int i=0;i<breakBulkModel.getShipmentList().size();i++)
            {
                    BreakBulkHouseModel  breakBulkHouseModel = (BreakBulkHouseModel)breakBulkModel.getShipmentList().get(i);
                    System.out.println("breakBulkHouseModel.documentId="+breakBulkHouseModel.documentId);        

                  if(breakBulkHouseModel.getViewChargesList()!=null)
                   {
                        for(Iterator k=breakBulkHouseModel.getViewChargesList().iterator();k.hasNext();)
                        {
                                HouseDocumentCharges      viewCharges      =      (HouseDocumentCharges)k.next();
                             if(breakBulkHouseModel.getChargesList()!=null)
                             {
                                for(Iterator j=breakBulkHouseModel.getChargesList().iterator();j.hasNext();)
                                {
                                     HouseDocumentCharges      charges      =      (HouseDocumentCharges)j.next();
                                    if(charges.chargeId.equals(viewCharges.chargeId) || !charges.chargeId.equals("045"))
                                    {
                                        j.remove();
                                        continue;
                                    }
                                           
                                }
                             }                            
                        }
                        if(breakBulkHouseModel.getChargesList() != null)
                        breakBulkHouseModel.getViewChargesList().addAll(breakBulkHouseModel.getChargesList());
                   }


                               
for(int j=0;j<breakBulkHouseModel.getViewChargesList().size();j++)
                {
                    houseDocumentCharges =(HouseDocumentCharges)breakBulkHouseModel.getViewChargesList().get(j);
                    if(houseDocumentCharges.chargeId.equals("044"))
                    continue;
                                        totalAmt             += houseDocumentCharges.paymentAmount * houseDocumentCharges.localRevenueConversion;
                }
           
                  }
             
           
0
chaitu chaitu
Asked:
chaitu chaitu
  • 3
  • 2
1 Solution
 
petmagdyCommented:
I will be better if HouseDocumentCharges has equals object like this:

public boolean equals(Object obj)
{
 if(obj instanceof java.lang.String)
 {
     String value = (String) obj;
    return chargeId.equals(value);
 }
 else if(obj instanceof urFullPackage.HouseDocumentCharges )
 {
   HouseDocumentCharges h = (HouseDocumentCharges) obj;
    // Compare the full object, ur choice to how to do this
 }
}

now ur code will look simpler like this (in short):
ArrayList sourceArray = breakBulkHouseModel.getViewChargesList();
ArrayList destArray = breakBulkHouseModel.getChargesList();

//if any of the 2 ArraysList equals null don't continue

Iterator sourceItr = sourceArray.iterator();
while(sourceItr.hasnext())
{
 HouseDocumentCharges h = (HouseDocumentCharges) itr.next();
 int index = destArray.indexOf(h.chargeId);
 while(index != -1)
 {
   destArray.remove(index);
   index = destArray.indexOf(h.chargeId);
 }
}

0
 
petmagdyCommented:
also ur HouseDocumentCharges equals method  if this is right to u can be:

public boolean equals(Object obj)
{
 if(obj instanceof java.lang.String)
 {
     String value = (String) obj;
    return chargeId.equals(value);
 }
 else if(obj instanceof urFullPackage.HouseDocumentCharges )
 {
   HouseDocumentCharges h = (HouseDocumentCharges) obj;
    return  chargeId.equals(h.chargeId);

 }
}

and hence u can pass HouseDocumentCharges in ArraList.indexOf() directly
0
 
objectsCommented:
if(breakBulkHouseModel.getViewChargesList()!=null)
{
   breakBulkHouseModel.getChargesList().removeAll(breakBulkHouseModel.getViewChargesList());
}

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
chaitu chaituAuthor Commented:
sorry for replying late

as objects mentioned i cant remove all getViewChargesList in my senario;

if u find charge code 045 both lists only delete in viewchargelist;

as petmagdy post is concenrned

ArrayList sourceArray = breakBulkHouseModel.getViewChargesList();
ArrayList destArray = breakBulkHouseModel.getChargesList();

Iterator sourceItr = sourceArray.iterator();
while(sourceItr.hasnext())
{
 HouseDocumentCharges h = (HouseDocumentCharges) itr.next();
 int index = destArray.indexOf(h.chargeId);
 while(index != -1)
 {
   destArray.remove(index);
   index = destArray.indexOf(h.chargeId);
 }
}

what r u doing is ur removing charge codes in chargeList;

0
 
petmagdyCommented:
ok then just do the opposite just exchange the variables in my code
0
 
objectsCommented:
Hmm, that seems far more complicated than you need.
But if you're happy :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now