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;
                }
           
                  }
             
           
LVL 20
chaitu chaituAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.