1

I am comparing two arraylist (Contacts .java is an pojo class). First Arraylist contains some Items and second arraylist contains some Items. By comparing two list, if both list contains same element it should not be added and else added to first list. But I cannot do it. Below is my code. Help will be appreciated.

public void insertmanualandxmldata()
{
    mContacts = storage.getarraylist(); // Arraylist
    if(mContacts != null)
    {
        for(int i=0; i<mContacts.size(); i++)
        {
            ContactVO mShareddata = mContacts.get(i);
            //mParsedDataSetList arraylist
            for(int j=0; j<mParsedDataSetList.size(); j++)
            {
                ContactVO mXmldata = mParsedDataSetList.get(j);
                if(mShareddata.getNumber().contains(mXmldata.getNumber()))
                {
                    mContacts.add(mXmldata);
                }
            }
        }
        storage.savearraylist(mContacts);
    }
    else
    {
        storage.savearraylist(mParsedDataSetList);
    }
}
Andreas
  • 5,393
  • 9
  • 44
  • 53
Narendra Kumar
  • 551
  • 5
  • 16

5 Answers5

2
  1. Implement Comparable

    private class ContactsVO implements Comparable<ContactsVO>{
    
    int number;
    
    @Override
    public int compareTo(ContactsVO that)
    {
        if (this.number> that.number)
            return 1;
        else if (this.number< that.number)
            return -1;
        else
            return 0;
    
    }
    
    }
    

And your logic.. Add contents of list 2 to list 1. while adding we have to compare if list 1 already has that item.

       for(int j=0; j < mParsedDataSetList.size(); j++)
        {
            ContactVO mXmldata = mParsedDataSetList.get(j);
            boolean exists = false;

            for(int i=0; i< mContacts.size(); i++)
            {
                ContactVO mShareddata = mContacts.get(i);

                if(mShareddata.comprareTo(mXmldata) == 0)
                {
                   exists = true;
                   break; 
                }
            }
            if(!exists)
            {
                mContacts.add(mXmldata);
            }
        }
Suresh Sajja
  • 552
  • 3
  • 8
1

You could implement a class that extends ArrayList and create a Comparator like this:

public static Comparator<T> Comp = new Comparator<T>(){
    public int compare(Type e1, Type e2){
        return (e1.getSomething().compareTo(e2.getSomething()));
    }
};

If you have never used comparators before here is a good tutorial https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

What comparators basically do is to provide a criteria for comparing elements.

Darwin57721
  • 177
  • 1
  • 14
1

There are two approaches by implementing comparator or comparable.
For your requirement I would suggest you can implements Comparable in your ContactVO class. And override compareTo method.

private class ContactsVO implements Comparable<ContactsVO> {

    private Integer number;

    // Remaining attributes and their getter setter.

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    @Override
    public int compareTo(ContactsVO compareWith) {
        if (this.getNumber() > compareWith.getNumber())
            return 1;
        else if (this.getNumber() < compareWith.getNumber())
            return -1;
        else
            return 0;
    }
}

If comparing attribute (in our case is number) implements Comparable then we can rewrite compareTo method as

@Override
public int compareTo(ContactsVO compareWith) {
    return this.getNumber().compareTo(compareWith.getNumber());
}

Note: Some basic data types such as Integer, String implements Comparable.

Naman Gala
  • 4,670
  • 1
  • 21
  • 55
0

Here contactList1 and contactList2 is your two list of POJO class Contacts

Set<Contacts> contactList3 = new HashSet<Contacts>(contactList1);

contactList3.addAll(contactList2);
ArrayList<Contacts> newList = new ArrayList<Contacts>(contactList3);

System.out.println("New List :"+newList);
Avinash Kumar Pankaj
  • 1,700
  • 2
  • 18
  • 27
0

Try to use LinkedHashSet which will not allow duplicates :

ArrayList arrayList1 = new ArrayList();
ArrayList arrayList2 = new ArrayList();

ArrayList arrayList3 = new ArrayList();
arrayList3.addAll(arrayList1);
arrayList3.addAll(arrayList2);

HashSet hashSet = new HashSet();
hashSet.addAll(arrayList3);
arrayList3.clear();
arrayList3.addAll(hashSet);

Note : when you required to maintain ordering of you list item use LinkedHashSet instead of HashSet.

Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67