Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two Collections in Java

I have two Collections in a Java class.The first collection contains previous data, the second contains updated data from the previous collection.

I would like to compare the two collections but I'm not sure of the best way to implement this efficiently.Both collections will contain the same amount of items.

Based then on the carType being the same in each collection I want to execute the carType method.

Any help is appreciated

like image 420
damien535 Avatar asked Nov 03 '10 08:11

damien535


People also ask

Can be used to compare two collections?

A double bar graph can be used to compare two collections of data.

How do I compare two lists in Java?

Java provides a method for comparing two Array List. The ArrayList. equals() is the method used for comparing two Array List. It compares the Array lists as, both Array lists should have the same size, and all corresponding pairs of elements in the two Array lists are equal.

Can you compare two Arraylists in Java?

You can compare two array lists using the equals() method of the ArrayList class, this method accepts a list object as a parameter, compares it with the current object, in case of the match it returns true and if not it returns false.


3 Answers

Difficult to help, because you didn't tell us how you like to compare the (equal-size) collections. Some ideas, hoping one will fit:

Compare both collections if they contain the same objects in the same order

Iterator targetIt = target.iterator();
for (Object obj:source)
  if (!obj.equals(targetIt.next()))
    // compare result -> false

Compare both collections if they contain the same objects in the any order

for (Object obj:source)
  if (target.contains(obj))
    // compare result -> false

Find elements in other collection that has changed

Iterator targetIt = target.iterator();
for (Object obj:source)
  if (!obj.equals(targetIt.next())
    // Element has changed

Based on your comment, this algorithm would do it. It collects all Cars that have been updated. If the method result is an empty list, both collections contain equal entries in the same order. The algorithm relies on a correct implementation of equals() on the Car type!

public List<Car> findUpdatedCars(Collection<Car> oldCars, Collection<Car> newCars)
  List<Car> updatedCars = new ArrayList<Car>();
  Iterator oldIt = oldCars.iterator();
  for (Car newCar:newCars) {
    if (!newCar.equals(oldIt.next()) {
      updatedCars.add(newCar);
    }
  }
  return updatedCars;
}
like image 116
Andreas Dolk Avatar answered Oct 20 '22 00:10

Andreas Dolk


From the set arithmetics, the sets A and B are equal iff A subsetequal B and B subsetequal A. So, in Java, given two collections A and B you can check their equality without respect to the order of the elements with

boolean collectionsAreEqual = A.containsAll(B) && B.containsAll(A);
like image 40
RedXIII Avatar answered Oct 20 '22 00:10

RedXIII


  • Iterate over the first collection and add it into a Map<Entity, Integer> whereby Entity is the class being stored in your collection and the Integer represents the number of times it occurs.
  • Iterate over the second collection and, for each element attempt to look it up in the Map - If it exists then decrement the Integer value by one and perform any action necessary when a match is found. If the Integer value has reached zero then remove the (Entity, Integer) entry from the map.

This algorithm will run in linear time assuming you've implemented an efficient hashCode() method.

like image 43
Adamski Avatar answered Oct 20 '22 00:10

Adamski