Comparing two Collections in Java

前端 未结 7 1471
孤独总比滥情好
孤独总比滥情好 2020-12-15 23:30

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 c

相关标签:
7条回答
  • 2020-12-16 00:15
    public static boolean isEqualCollection(java.util.Collection a,
                                            java.util.Collection b)
    

    Returns true if the given Collections contain exactly the same elements with exactly the same cardinalities.

    That is, iff the cardinality of e in a is equal to the cardinality of e in b, for each element e in a or b.

    Parameters:

    • the first collection, must not be null
    • the second collection, must not be null

    Returns: true iff the collections contain the same elements with the same cardinalities.

    0 讨论(0)
  • 2020-12-16 00:18
    return collection1.toString().equals(collection2.toString());
    
    0 讨论(0)
  • 2020-12-16 00:21

    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);
    
    0 讨论(0)
  • 2020-12-16 00:21

    If not worried about cases like (2,2,3), (2,3,3):

    static <T> boolean equals(Collection<T> lhs, Collection<T> rhs) {
        return lhs.size( ) == rhs.size( ) && lhs.containsAll(rhs)  && rhs.containsAll(lhs);
    }
    
    0 讨论(0)
  • 2020-12-16 00:23

    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;
    }
    
    0 讨论(0)
  • 2020-12-16 00:28

    Slightly updated one considering null values:

    static <T> boolean equals(Collection<T> lhs, Collection<T> rhs) {
        boolean equals = false;
        if(lhs!=null && rhs!=null) {
           equals = lhs.size( ) == rhs.size( ) && lhs.containsAll(rhs)  && rhs.containsAll(lhs);
        } else if (lhs==null && rhs==null) {
           equals = true;
        }
     return equals;
    }
    
    0 讨论(0)
提交回复
热议问题