Compare two Java Collections using Comparator instead of equals()

后端 未结 4 1244
臣服心动
臣服心动 2021-02-12 21:09

Problem Statement

I have two Collections of the same type of object that I want to compare. In this case, I want to compare them based on an attribute that does not fa

相关标签:
4条回答
  • 2021-02-12 21:25

    I'm not sure this way is actually better, but it is "another way"...

    Take your original two collections, and create new ones containing an Adapter for each base object. The Adapter should have .equals() and .hashCode() implemented as being based on Name.calculateWeightedRank(). Then you can use normal Collection equality to compare the collections of Adapters.

    * Edit *

    Using Eclipse's standard hashCode/equals generation for the Adapter. Your code would just call adaptCollection on each of your base collections, then List.equals() the two results.

    public class Adapter {
    
        public List<Adapter> adaptCollection(List<Name> names) {
            List<Adapter> adapters = new ArrayList<Adapter>(names.size());
    
            for (Name name : names) {
                adapters.add(new Adapter(name));
            }
    
            return adapters;
        }
    
    
        private final int name;
    
        public Adapter(Name name) {
            this.name = name.getWeightedResult();
        }
    
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + name;
            return result;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Adapter other = (Adapter) obj;
            if (name != other.name)
                return false;
            return true;
        }
    
    }
    
    0 讨论(0)
  • 2021-02-12 21:28

    You could use the Guava Equivalence class in order to decouple the notions of "comparing" and "equivalence". You would still have to write your comparing method (AFAIK Guava does not have it) that accepts an Equivalence subclass instead of the Comparator, but at least your code would be less confusing, and you could compare your collections based on any equivalence criteria.

    Using a collection of equivance-wrapped objects (see the wrap method in Equivalence) would be similar to the Adapter-based solution proposed by sharakan, but the equivalence implementation would be decoupled from the adapter implementation, allowing you to easily use multiple Equivalence criteria.

    0 讨论(0)
  • 2021-02-12 21:28

    You can use new isEqualCollection method added to CollectionUtils since version 4. This method uses external comparsion mechanism provided by Equator interface implementation. Please, check this javadocs: CollectionUtils.isEqualCollection(...) and Equator.

    0 讨论(0)
  • 2021-02-12 21:30

    EDIT: Removed old answer.

    Another option that you have is creating an interface called Weighted that could look like this:

    public interface Weighted {
        int getWeightedRank();
    }
    

    Then have your Name class implement this interface. Then you could change your method to look like this:

     public <T extends Weighted> boolean weightedEquals(Collection<T> col1, Collection<T> col2)
    {
        if (col1 == null)
          return col2 == null;
         if (col2 == null) 
          return false;
    
      if (col1.size() != col2.size())
          return false;
    
      Iterator<T> i1 = col1.iterator(), i2 = col2.iterator();
    
      while(i1.hasNext() && i2.hasNext()) {
          if (i1.next().getWeightedRank() != i2.next().getWeightedRank()) {
              return false;
          }
      }
    
      return true;
    }
    

    Then as you find additional classes that need to be weighted and compared you can put them in your collection and they could be compared with each other as well. Just an idea.

    0 讨论(0)
提交回复
热议问题