1

I have two lists List listOne, List listTwo, and i want to compare both and if both are same i want to add the list item into some other list ol1 else add to ol2.

Here the ElementRangeIndex is a bean class which contains some string values.

While comparing the two lists it needs to compare each string value from the bean.

I have used the below code to but contains adding duplicate values since both lists have different objects.

public static Map<Integer, List<ElementRangeIndex>> compareLists(List<ElementRangeIndex> listOne, List<ElementRangeIndex> listTwo) {
     boolean indicator = false;
     List<ElementRangeIndex> listOnes = new ArrayList<ElementRangeIndex>();

     List<ElementRangeIndex> listTwos = new ArrayList<ElementRangeIndex>();
     List<ElementRangeIndex> listThree = new ArrayList<ElementRangeIndex>();

     Map<Integer, List<ElementRangeIndex>> map = new HashMap<Integer, List<ElementRangeIndex>>();
    if (listOne!= null && listTwo!=null && listOne.size() == listTwo.size()) {

        for (ElementRangeIndex listTwoData : listTwo) {
            for (ElementRangeIndex listOneData : listOne) {
/* if (listOneData.getNamespaceUri().equals(listTwoData.getNamespaceUri())
                         && listOneData.getCollation().equals(listTwoData.getCollation())
                         && listOneData.getScalarType().equals(listTwoData.getScalarType())
                         && listOneData.getLocalname().equals(listTwoData.getLocalname())) {*/
                if ((listOneData.getNamespaceUri().hashCode()== listTwoData.getNamespaceUri().hashCode())
                        && (listOneData.getCollation().hashCode() == listTwoData.getCollation().hashCode())
                        && (listOneData.getScalarType().hashCode() == listTwoData.getScalarType().hashCode())
                        && (listOneData.getLocalname().hashCode() == listTwoData.getLocalname().hashCode())) {

                    listOnes.add(listOneData);

                    if(listTwos.contains(listOneData))
                        listTwos.remove(listOneData);

                    if(listTwos.contains(listTwoData))
                        listTwos.remove(listTwoData);

                    if(listThree.contains(listOneData))
                        listThree.remove(listOneData);

                    if(listThree.contains(listTwoData))
                        listThree.remove(listTwoData);


                }else{
                        if(!listOnes.contains(listOneData))
                        if(!listTwos.contains(listOneData))
                                listTwos.add(listOneData);
                    if(!listOnes.contains(listTwoData))
                        if(!listThree.contains(listTwoData))
                                listThree.add(listTwoData);

                }

            }
            }
        map.put(1,listOnes);
        map.put(2, listTwos);
        map.put(3, listThree);

        }
    return map;
}

My aim is to add similar list items into one list(listOnes), left only to other list(listTwos) and right to other list(listThree).

Thanks, Arjun

Arjs
  • 45
  • 1
  • 9
  • Use Apache Commons Collections' class `CollectionUtils` which provides methods to extract the elements contained in both collections or in only one. Use a `LinkedHashSet` or something similar to retain insertion order while rejecting duplicates (if you need that). – Thomas Jan 28 '16 at 13:23
  • http://stackoverflow.com/questions/2762093/java-compare-two-lists – balaaagi Jan 28 '16 at 13:28

1 Answers1

0

If you need to do the split yourself, I'd probably do something like this:

  1. Create a copy of list and name it leftOnly. This will contain elements only present in list one.

  2. Create a copy of list two and name if rightOnly. This will contain elements only present in list two.

  3. Create an empty list intersectList which will contain elements present in both lists.

  4. Up to now leftOnly might contain too many elements so we'll need to filter those. To do that we use an iterator to iterate over each element and check whether it is contained in rightOnly as well. If it is we remove the element from leftOnly and rightOnly and add it to intersectList.

To speed up that process (contains and remove on lists are linear operations) you might make leftOnly and rightOny be of type LinkedHashSet which allows for faster operations but would not allow duplicates (using duplicates in the result would break the whole logic anyways).

Thomas
  • 87,414
  • 12
  • 119
  • 157