How to compare two Collections for “equivalence” based on fields from different Java classes?

我怕爱的太早我们不能终老 提交于 2020-12-29 02:58:58

问题


Given any two classes, e.g. ClassA and ClassB below:

class ClassA {
    private int intA;
    private String strA;
    private boolean boolA;
    // Constructor
    public ClassA (int intA, String strA, boolean boolA) {
        this.intA = intA; this.strA = strA; this.boolA = boolA;
    } // Getters and setters etc. below...
}

class ClassB {
    private int intB;
    private String strB;
    private boolean boolB;
    // Constructor
    public ClassB (int intB, String strB, boolean boolB) {
        this.intB = intB; this.strB = strB; this.boolB = boolB;
    } // Getters and setters etc. below...
}

And any two different Collection types, one with ClassA elements and the other with ClassB elements, e.g:

List<Object> myList = Arrays.asList(new ClassA(1, "A", true),
                                    new ClassA(2, "B", true));
Set<Object> mySet = new HashSet<Object>(
                      Arrays.asList(new ClassB(1, "A", false),
                                    new ClassB(2, "B", false)));

What's the simplest way of telling whether the two Collections are "equivalent"(*) in terms of a specified subset of fields?

(*) The word "equivalent" is used rather then "equal" since this is contextual - i.e. such "equivalence" may be defined differently in another context.

Worked example from above: Suppose we specify that intA and strA should match with intB and strB respectively (but the boolA / boolB values can be ignored). This would make the two collection objects defined above be considered equivalent - but if an element were added to or removed from one of the collections then they no longer would be.

Preferred solution: The method used should be generic for any Collection type. Ideally Java 7 as am confined to using this (but Java 8 may be of additional interest to others). Happy to use Guava or Apache Commons but would prefer not to use more obscure external libraries.


回答1:


Here's a Java 8 version using lambdas and higher-order functions. It's probably possible to convert this to Java 7 using anonymous inner classes instead of lambdas. (I believe most IDEs have a refactoring operation that does this.) I'll leave that as an exercise for interested readers.

There are actually two distinct problems here:

  1. Given two objects of different types, evaluate them by examining respective fields of each. This differs from "equals" and "compare" operations which are already defined by the JDK library APIs, so I'll use the term "equivalent" instead.

  2. Given two collections containing elements of those types, determine if they are "equals" for some definition of that term. This is actually quite subtle; see the discussion below.

1. Equivalence

Given two objects of types T and U we want to determine whether they're equivalent. The result is a boolean. This can be represented by a function of type BiPredicate<T,U>. But we can't necessarily examine the objects directly; instead, we need to extract respective fields from each object and evaluate the results of extraction against each other. If the field extracted from T is of type TR and the field extracted from U is of type UR, then the extractors are represented by the function types

Function<T, TR>
Function<U, UR>

Now we have extracted results of type TR and UR. We could just call equals() on them, but that's unnecessarily restrictive. Instead, we can provide another equivalence function that will be called to evaluate these two results against each other. That's a BiPredicate<TR,UR>.

Given all this, we can write a higher-order function that takes all of these functions and produces and equivalence function for us (wildcards included for completeness):

static <T,U,TR,UR> BiPredicate<T,U> equiv(Function<? super T, TR> tf,
                                          Function<? super U, UR> uf,
                                          BiPredicate<? super TR, ? super UR> pred) {
    return (t, u) -> pred.test(tf.apply(t), uf.apply(u));
}

It's probably a common case for the results of field extraction to be evaluated using equals(), so we can provide an overload for that:

static <T,U> BiPredicate<T,U> equiv(Function<? super T, ?> tf,
                                    Function<? super U, ?> uf) {
    return (t, u) -> equiv(tf, uf, Object::equals).test(t, u);
}

I could have provided another type variable R as the result type of both functions, to ensure they're the same type, but it turns out this isn't necessary. Since equals() is defined on Object and it takes an Object argument, we don't actually care what the function return types are, hence the wildcards.

Here's how to use this to evaluate the OP's example classes using just the string fields:

ClassA a = ... ;
ClassB b = ... ;
if (equiv(ClassA::getStrA, ClassB::getStrB).test(a, b)) {
    // they're equivalent
}

As a variation, we might also want a primitive specialization in order to avoid unnecessary boxing:

static <T,U> BiPredicate<T,U> equivInt(ToIntFunction<? super T> tf,
                                       ToIntFunction<? super U> uf) {
    return (t, u) -> tf.applyAsInt(t) == uf.applyAsInt(u);
}

This lets us construct equivalence functions based on a single field. What if we want to evaluate equivalence based on multiple fields? We can combine an arbitrary number of BiPredicates by chaining the and() method. Here's how to create a function that evaluates equivalence using the int and String fields of the classes from the OP's example. For this, it's probably best to store the function in a variable separately from using it, though this can probably all be inlined (which I think will make it unreadable):

BiPredicate<ClassA, ClassB> abEquiv =
    equivInt(ClassA::getIntA, ClassB::getIntB)
        .and(equiv(ClassA::getStrA, ClassB::getStrB));

if (abEquiv.test(a, b)) {
    // they're equivalent
}

As a final example, it's quite powerful to be able to provide an equivalence function for the field extraction results when creating an equivalence function for two classes. For example, suppose we want to extract two String fields and consider them equivalent if the extracted strings are equals, ignoring case. The following code results in true:

equiv(ClassA::getStrA, ClassB::getStrB, String::equalsIgnoreCase)
    .test(new ClassA(2, "foo", true),
          new ClassB(3, "FOO", false))

2. Collection “Equality”

The second part is to evaluate whether two collections are "equals" in some sense. The problem is that in the Collections Framework, the notion of equality for is defined such that a List can only be equal to another List, and a Set can only be equal to another Set. It follows that a Collection of some other type can never be equal to either a List or a Set. See the specification of Collection.equals() for some discussion of this point.

This is clearly at odds with what the OP wants. As suggested by the OP, we don't really want "equality," but we want some other property for which we need to provide a definition. Based on the OP's examples, and some suggestions in other answers by Przemek Gumula and janos, it seems like we want the elements in the two collections to somehow be in one-for-one correspondence. I'll call this a bijection which might not be mathematically precise, but it seems close enough. Furthermore, the correspondence between each pair of elements should be equivalence as defined above.

Computing this is a bit subtle, since we have our own equivalence relation. We can't use many of the built-in Collections operations, since they all use equals(). My first attempt was this:

// INCORRECT
static <T,U> boolean isBijection(Collection<T> c1,
                                 Collection<U> c2,
                                 BiPredicate<? super T, ? super U> pred) {
    return c1.size() == c2.size() &&
           c1.stream().allMatch(t -> c2.stream()
                                       .anyMatch(u -> pred.test(t, u)));
}

(This is essentially the same as given by Przemek Gumula.) This has problems, which boil down to the possibility of more than one element in the one collection corresponding to a single element in the other collection, leaving elements unmatched. This gives strange results if given two multisets, using equality as the equivalence function:

{a x 2, b}    // essentially {a, a, b}
{a, b x 2}    // essentially {a, b, b}

This function considers these two multisets to be a bijection, which clearly isn't the case. Another problem occurs if the equivalence function allows many-to-one matching:

Set<String> set1 = new HashSet<>(Arrays.asList("foo", "FOO", "bar"));
Set<String> set2 = new HashSet<>(Arrays.asList("fOo", "bar", "quux"));

isBijection(set1, set2, equiv(s -> s, s -> s, String::equalsIgnoreCase))

The result is true, but if the sets are given in the opposite order, the result is false. That's clearly wrong.

An alternative algorithm is to create a temporary structure and remove elements as they're matched. The structure has to account for duplicates, so we need to decrement the count and only remove the element when the count reaches zero. Fortunately, various Java 8 features make this pretty simple. This is quite similar to the algorithm used in the answer from janos, though I've extracted the equivalence function into a method parameter. Alas, since my equivalence function can have nested equivalence functions, it means I can't probe the map (which is defined by equality). Instead, I have to search the map's keys, which means the algorithm is O(N^2). Oh well.

The code, however, is pretty simple. First, the frequency map is generated from the second collection using groupingBy. Then, the elements of the first collection are iterated, and the frequency map's keys are searched for an equivalent. If one is found, its occurrence count is decremented. Note the return value of null from the remapping function passed to Map.compute(). This has the side effect of removing the entry, not setting the mapping to null. It's a bit of an API hack, but it's quite effective.

For every element in the first collection, an equivalent element in the second collection must be found, otherwise it bails out. After all elements of the first collection have been processed, all elements from the frequency map should also have been processed, so it's simply tested for being empty.

Here's the code:

static <T,U> boolean isBijection(Collection<T> c1,
                                 Collection<U> c2,
                                 BiPredicate<? super T, ? super U> pred) {
    Map<U, Long> freq = c2.stream()
                          .collect(Collectors.groupingBy(u -> u, Collectors.counting()));
    for (T t : c1) {
        Optional<U> ou = freq.keySet()
                             .stream()
                             .filter(u -> pred.test(t, u))
                             .findAny();
        if (ou.isPresent()) {
            freq.compute(ou.get(), (u, c) -> c == 1L ? null : c - 1L);
        } else {
            return false;
        }
    }

    return freq.isEmpty();
}

It's not entirely clear whether this definition is the correct one. But it seems intuitively to be what people want. It's fragile, though. If the equivalence function isn't symmetric, isBijection will fail. There are also some degrees of freedom aren't accounted for. For example, suppose the collections are

{a, b}
{x, y}

And a is equivalent to both x and y, but b is only equivalent to x. If a is matched to x, the result of isBijection is false. But if a were matched to y, the result would be true.

Putting it Together

Here's the OP's example, coded up using the equiv(), equivInt(), and isBijection functions:

List<ClassA> myList = Arrays.asList(new ClassA(1, "A", true),
                                    new ClassA(2, "B", true));

Set<ClassB> mySet = new HashSet<>(Arrays.asList(new ClassB(1, "A", false),
                                                new ClassB(2, "B", false)));

BiPredicate<ClassA, ClassB> abEquiv =
    equivInt(ClassA::getIntA, ClassB::getIntB)
        .and(equiv(ClassA::getStrA, ClassB::getStrB));

isBijection(myList, mySet, abEquiv)

The result of this is true.




回答2:


Another possible solution is writing a simple comparing method with a predicate (so you can explicitly specify the condition for two classes to be similar on your terms). I created this in Java 8:

<T, U> boolean compareCollections(Collection<T> coll1, Collection<U> coll2, BiPredicate<T, U> predicate) {

    return coll1.size() == coll2.size()
            && coll1.stream().allMatch(
            coll1Item -> coll2.stream().anyMatch(col2Item -> predicate.test(coll1Item, col2Item))
    );
}

As you can see, it compares the size and then checks if every element in collection has a counterpart in the second collection (it's not comparing order though). It's in Java 8, but you can port it to Java 7 by implementing a simple BiPredicate code, allMatch and anyMatch (one for-loop for each of them should be sufficient)

Edit: Java 7 code:

<T, U> boolean compareCollections(Collection<T> coll1, Collection<U> coll2, BiPredicate<T, U> predicate) {

        if (coll1.size() != coll2.size()) {
            return false;
        }

        for (T item1 : coll1) {
            boolean matched = false;
            for (U item2 : coll2) {
                if (predicate.test(item1, item2)) {
                    matched = true;
                }
            }

            if (!matched) {
                return false;
            }
        }
        return true;

    }}
interface BiPredicate <T, U> {
    boolean test(T t, U u);
}

Here's a usage example.




回答3:


There's no very easy way.

The most generic that would work with regular Java collections would be to create a wrapper class that would take either ClassA or ClassB as input, and then override equals/hashcode as defined by you.

In some cases you can abuse Comparator, but that would limit you to TreeMap/TreeSet. You can also implement equals() method to work so that classA.equals(classB); returns true, but that can cause tricky bugs if you're not being careful. It can also result in interesting situations where a.equals(b) and b.equals(c) but !a.equals(c).

Some library (Guava?) also had a Comparator style mechanism for equality testing, but that would work only with the library's collections.




回答4:


Apache Commons Lang has EqualsBuilder#reflectionEquals(Object, Object):

This method uses reflection to determine if the two Objects are equal.

It uses AccessibleObject.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly. Non-primitive fields are compared using equals().

Transient members will be not be tested, as they are likely derived fields, and not part of the value of the Object.

Static fields will not be tested. Superclass fields will be included.

So this should cover your use case. Obvious disclaimer: it uses reflection ;)

EDIT: This, of course, assumes fields have same names, not types. In latter case one can inspect source code and adjust it to their use case.




回答5:


A combination of two existing answers: The generic version of a wrapper class Kayaman suggested (Just a List). Using ArrayList::equals as predicate for Przemek Gumula approach.

I added a Builder to make it a bit nicer to use:

    StructureEqual<ClassA, ClassB> struct = StructureEqual.<ClassA, ClassB>builder()
            .field(ClassA::getIntA, ClassB::getIntB) // Declare what fields should be checked
            .field(ClassA::getStrA, ClassB::getStrB)
            .build();

    System.out.println(struct.isEqual(myList, mySet));

The actual code:

public class StructureEqual<A, B> {
    private List<EqualPoint<A, B>> points;

    public StructureEqual(List<EqualPoint<A, B>> points) {
        this.points = points;
    }

    private List<Object> sampleA(A a) {
        return points.stream().map(p -> p.getAPoint().apply(a)).collect(Collectors.toList());
    }

    private List<Object> sampleB(B b) {
        return points.stream().map(p -> p.getBPoint().apply(b)).collect(Collectors.toList());
    }

    public boolean isEqual(Collection<A> as, Collection<B> bs) {
        Set<List<Object>> aSamples = as.stream().map(this::sampleA).collect(Collectors.toSet());
        Set<List<Object>> bSamples = bs.stream().map(this::sampleB).collect(Collectors.toSet());
        return aSamples.equals(bSamples);
    }

    private static class EqualPoint<PA, PB> {

        private final Function<PA, ?> aPoint;
        private final Function<PB, ?> bPoint;

        public <T> EqualPoint(Function<PA, T> aPoint, Function<PB, T> bPoint) {
            this.aPoint = aPoint;
            this.bPoint = bPoint;
        }

        Function<PA, ?> getAPoint() {
            return aPoint;
        }

        Function<PB, ?> getBPoint() {
            return bPoint;
        }
    }

    public static <BA, BB> Builder<BA, BB> builder() {
        return new Builder<>();
    }

    public static class Builder<BA, BB> {
        private List<EqualPoint<BA, BB>> points = new ArrayList<>();

        public <T> Builder<BA, BB> field(Function<BA, T> a, Function<BB, T> b) {
            points.add(new EqualPoint<>(a, b));
            return this;
        }

        public StructureEqual<BA, BB> build() {
            return new StructureEqual<>(Collections.unmodifiableList(points));
        }
    }

}



回答6:


What's the simplest way of telling whether the two Collections are equal in terms of a specified subset of fields?

Based on your description, your requirements of equality are:

  • The collections have equal sizes.
  • For each item1 in collection1, there exists item2 in collection2 such that item1.field_x is equal to item2.field_y, for multiple defined field_x-field_y pairs.

If we can assume that there are no duplicate elements in either collection, that is, then this the "simplest way" could be something like this:

    public boolean areEqual(Collection<ClassA> c1, Collection<ClassB> c2) {
        if (c1.size() != c2.size()) {
            return false;
        }
        OUTER:
        for (ClassA a : c1) {
            for (ClassB b : c2) {
                if (a.getIntA() == b.getIntB() && Objects.equals(a.getStringA(), b.getStringB())) {
                    continue OUTER;
                }
            }
            return false;
        }
        return true;
    }

This is a straightforward implementation of the requirements. But as it may compare each element with every other element in the other collection, it has very poor performance, O(n^2) where n is the size of the collection.

This may also not work if equal elements can appear multiple times in a collection:

    List<ClassA> list = new ArrayList<>(Arrays.asList(
        new ClassA(1, "A", true),
        new ClassA(1, "A", false),
        new ClassA(2, "B", true)
    ));
    Set<ClassB> set = new HashSet<>(Arrays.asList(
        new ClassB(1, "A", false),
        new ClassB(2, "B", false),
        new ClassB(2, "B", true)
    ));

Here ClassA(1, "A", true) and ClassA(1, "A", false) are considered equivalent in the first list, and new ClassB(2, "B", false) and new ClassB(2, "B", true) are considered equivalent in the second list. The above algorithm will find these two collections equal, which is incorrect.

It's possible to handle the case of duplicates, and at the same time improve time-complexity at the expense of using extra space:

  • Iterate over the first collection to build a map of counts of (int, String) tuples
  • Iterate over the second collection while checking the map of counts:
    • If the count of maps doesn't contain the corresponding (int, String) tuple, return false, as this means that an element doesn't have a matching pair
    • If of corresponding tuple exists, decrease its count
    • If the count reaches 0, from the tuple from the map
  • If the end of the loop is reached, that must mean that all items were matched (the map should be empty), so you can simply return true.

Implementation:

class FieldExtractingEqual {

    public boolean areEqual(Collection<ClassA> c1, Collection<ClassB> c2) {
        if (c1.size() != c2.size()) {
            return false;
        }

        Map<Tuple, Integer> counts = new HashMap<>();
        for (ClassA a : c1) {
            Tuple tuple = new Tuple(a.getIntA(), a.getStringA());
            Integer count = counts.get(tuple);
            if (count == null) {
                count = 0;
            }
            counts.put(tuple, count + 1);
        }

        for (ClassB b : c2) {
            Tuple tuple = new Tuple(b.getIntB(), b.getStringB());
            Integer count = counts.get(tuple);
            if (count == null) {
                return false;
            }
            if (count == 1) {
                counts.remove(tuple);
            } else {
                counts.put(tuple, count - 1);
            }
        }

        return true;
    }

    private static class Tuple {
        private final Object[] values;

        public Tuple(Object... values) {
            this.values = values;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Tuple tuple = (Tuple) o;

            return Arrays.equals(values, tuple.values);
        }

        @Override
        public int hashCode() {
            return Arrays.hashCode(values);
        }
    }
}

Some assertj tests to verify the implementation:

List<ClassA> myList = new ArrayList<>(Arrays.asList(
    new ClassA(1, "A", true),
    new ClassA(1, "A", true),
    new ClassA(2, "B", true)
));
Set<ClassB> mySet = new HashSet<>(Arrays.asList(
    new ClassB(1, "A", false),
    new ClassB(1, "A", true),
    new ClassB(2, "B", false)
));

FieldExtractingEqual comp = new FieldExtractingEqual();
assertThat(comp.areEqual(myList, mySet)).isTrue();

myList.add(new ClassA(3, "X", true));
mySet.add(new ClassB(3, "Y", true));
assertThat(comp.areEqual(myList, mySet)).isFalse();

As a further improvement, it's possible to make the implementation of FieldExtractingEqual generic, so that it can take arbitrary Collection<A> and Collection<B> parameters, and pairs of extractors to create tuples from A and B. Here's one way to implement that:

interface FieldExtractor<T, V> {
    V apply(T arg);
}

class GenericFieldExtractingEqual<T, U> {
    private final List<FieldExtractor<T, ?>> extractors1;
    private final List<FieldExtractor<U, ?>> extractors2;

    private GenericFieldExtractingEqual(List<FieldExtractor<T, ?>> extractors1, List<FieldExtractor<U, ?>> extractors2) {
        this.extractors1 = extractors1;
        this.extractors2 = extractors2;
    }

    public boolean areEqual(Collection<T> c1, Collection<U> c2) {
        if (c1.size() != c2.size()) {
            return false;
        }

        Map<Tuple, Integer> counts = new HashMap<>();
        for (T a : c1) {
            Tuple tuple = newTuple1(a);
            Integer count = counts.get(tuple);
            if (count == null) {
                count = 0;
            }
            counts.put(tuple, count + 1);
        }

        for (U b : c2) {
            Tuple tuple = newTuple2(b);
            Integer count = counts.get(tuple);
            if (count == null) {
                return false;
            }
            if (count == 1) {
                counts.remove(tuple);
            } else {
                counts.put(tuple, count - 1);
            }
        }

        return true;
    }

    private Tuple newTuple1(T a) {
        return new Tuple(extractors1.stream().map(x -> x.apply(a)).toArray());
    }

    private Tuple newTuple2(U b) {
        return new Tuple(extractors2.stream().map(x -> x.apply(b)).toArray());
    }

    private static class Tuple {
        private final Object[] values;

        public Tuple(Object... values) {
            this.values = values;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Tuple tuple = (Tuple) o;

            return Arrays.equals(values, tuple.values);
        }

        @Override
        public int hashCode() {
            return Arrays.hashCode(values);
        }
    }

    public static class Builder<T, U> {
        List<FieldExtractor<T, ?>> extractors1 = new ArrayList<>();
        List<FieldExtractor<U, ?>> extractors2 = new ArrayList<>();

        <V> Builder<T, U> addExtractors(FieldExtractor<T, V> extractor1, FieldExtractor<U, V> extractor2) {
            extractors1.add(extractor1);
            extractors2.add(extractor2);
            return this;
        }

        GenericFieldExtractingEqual<T, U> build() {
            return new GenericFieldExtractingEqual<>(new ArrayList<>(extractors1), new ArrayList<>(extractors2));
        }
    }
}

Example usage and some assertj tests:

GenericFieldExtractingEqual<ClassA, ClassB> comp2 = new GenericFieldExtractingEqual.Builder<ClassA, ClassB>()
    .addExtractors(ClassA::getIntA, ClassB::getIntB)
    .addExtractors(ClassA::getStringA, ClassB::getStringB)
    .build();
assertThat(comp2.areEqual(myList, mySet)).isTrue();

myList.add(new ClassA(3, "X", true));
mySet.add(new ClassB(3, "Y", true));
assertThat(comp2.areEqual(myList, mySet)).isFalse();

That is, you build an GenericFieldExtractingEqual instance from pairs of extractors, for example:

    .addExtractors(ClassA::getIntA, ClassB::getIntB)

The first parameter is an object that extracts a field in the first class, and the second parameter is an object that extracts the corresponding field in the second class. You add as many extractor pairs as you want to compare for the equality condition.

Although I used the Java8 writing style ClassA::getIntA for compactness, it's easy (but lengthy) to convert to FieldExtractor implementations:

    .addExtractors(
        new FieldExtractor<ClassA, Integer>() {
            @Override
            public Integer apply(ClassA arg) {
                return arg.getIntA();
            }
        },
        new FieldExtractor<ClassB, Integer>() {
            @Override
            public Integer apply(ClassB arg) {
                return arg.getIntB();
            }
        }
    )

The same goes for the newTuple* utility methods.

Here's a runnable version on RexTester.




回答7:


Here is my answer:

 public class StackOverFlow {

static class Testy {

    int id;
    String name;

    public Testy(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 89 * hash + this.id;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Testy other = (Testy) obj;
        if (this.id != other.id || !this.name.equals(other.name)) {
            return false;
        }
        return true;
    }

}

static class AnotherTesty {

    int id;
    String name;

    public AnotherTesty(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 41 * hash + this.id;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final AnotherTesty other = (AnotherTesty) obj;
        if (this.id != other.id || !this.name.equals(other.name)) {
            return false;
        }
        return true;
    }

}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    List<Object> list = Arrays.asList(new Testy(5, "test"), new AnotherTesty(5, "test"));
    Set<Object> set = new HashSet<>(Arrays.asList(new Testy(5, "test"), new AnotherTesty(5, "test")));

    System.out.println(compareCollections(list, set, Testy.class, AnotherTesty.class));
}

private static boolean compareCollections(Collection<?> c1, Collection<?> c2, Class cls, Class cls2) {

    List<Object> listOfCls = c1.stream().filter(p -> cls.isInstance(p)).map(o -> cls.cast(o)).collect(Collectors.toList());
    List<Object> listOfCls2 = c1.stream().filter(p -> cls2.isInstance(p)).map(o -> cls2.cast(o)).collect(Collectors.toList());

    List<Object> list2OfCls = c2.stream().filter(p -> cls.isInstance(p)).map(o -> cls.cast(o)).collect(Collectors.toList());
    List<Object> list2OfCls2 = c2.stream().filter(p -> cls2.isInstance(p)).map(o -> cls2.cast(o)).collect(Collectors.toList());

    if (listOfCls.size() != list2OfCls.size()||listOfCls2.size() != list2OfCls2.size()) {
        return false;
    }

    boolean clsFlag = true, cls2Flag = true;
    for (int i = 0; i < listOfCls.size(); i++) {

        if (!listOfCls.get(i).equals(list2OfCls.get(i))) {
            clsFlag = false;
            break;
        }
    }
    for (int i = 0; i < list2OfCls2.size(); i++) {
        if (!listOfCls2.get(i).equals(list2OfCls2.get(i))) {
            cls2Flag = false;
            break;
        }
    }

    return clsFlag && cls2Flag;
}

}



回答8:


quick prototype:

package stackoverflow;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiFunction;

import org.junit.Test;

public class CompareTwoList {
    static class ClassA {
        int intA;
        String strA;
        boolean boolA;

        // Constructor
        public ClassA(int intA, String strA, boolean boolA) {
            this.intA = intA;
            this.strA = strA;
            this.boolA = boolA;
        } // Getters and setters etc. below...


    }

    static class ClassB {
        int intB;
        String strB;
        boolean boolB;

        // Constructor
        public ClassB(int intB, String strB, boolean boolB) {
            this.intB = intB;
            this.strB = strB;
            this.boolB = boolB;
        } // Getters and setters etc. below...

    }

    @FunctionalInterface
    private interface IncopatibeEqualsOperator<A, B> extends BiFunction<A, B, Boolean> {
    }

    @Test
    public void CompareListOfClassAAndclassBObjects() throws Exception {
        List<ClassA> myList = Arrays.asList(
                new ClassA(1, "A", true),
                new ClassA(2, "B", true));

        Set<ClassB> mySet = new HashSet<ClassB>(Arrays.asList(
                new ClassB(1, "A", false),
                new ClassB(2, "B", false)));

        // can be extract to separate file
        IncopatibeEqualsOperator<ClassA, ClassB> equalsOperatorFlavor1 = (ClassA o1, ClassB o2) -> {
            // custom logic here
            return o1.intA == o2.intB &&
                    java.util.Objects.equals(o1.strA, o2.strB);
        };

        boolean areEquals = areEquals(myList, mySet, equalsOperatorFlavor1);

        assertThat(areEquals, is(true));
    }

    // Add in utility class
    private <A, B> boolean areEquals(Collection<A> o1, Collection<B> o2, IncopatibeEqualsOperator<A, B> comparator) {
        if (o1.size() == o2.size()) { // if size different; they are not equals
            for (A obj1 : o1) {
                boolean found = false; // search item of o1 into o2; algorithm
                                       // can be improve
                for (B obj2 : o2) {
                    if (comparator.apply(obj1, obj2)) { // call custom code of
                                                        // comparision
                        found = true;
                        break;
                    }
                }

                if (!found) {// if current element of o1 is not equals with any
                             // one return false
                    return false;
                }
            }
            return true;// all are matched
        }
        return false;
    }
}



回答9:


Make sure Class A and B have toString() methods.

ClassA

public class ClassA {
    private int intA;
    private String strA;
    private boolean boolA;
    // Constructor
    public ClassA (int intA, String strA, boolean boolA) {
        this.intA = intA; this.strA = strA; this.boolA = boolA;
    } //

    @Override
    public String toString()
    {
        return intA + " " + strA + " " + boolA;
    }
}

ClassB

public class ClassB {
    private int intB;
    private String strB;
    private boolean boolB;
    // Constructor
    public ClassB (int intB, String strB, boolean boolB) {
        this.intB = intB; this.strB = strB; this.boolB = boolB;
    } // Gett

    @Override
    public String toString()
    {
        return intB + " " + strB + " " + boolB;
    }
}

Main/Test

public class JavaApplication11 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        List<Object> myList = Arrays.asList(new ClassA(1, "A", true),
                                            new ClassA(2, "B", true));
        Set<Object> mySet = new HashSet<Object>(
                      Arrays.asList(new ClassB(1, "A", false),
                                    new ClassB(2, "B", false)));
        System.out.println("is equal: " + isEqual(myList, mySet));
    }

    static boolean isEqual(Object list, Object set)
    {
        System.out.println(list.toString());
        System.out.println(set.toString());
        String tempStringA = list.toString();
        tempStringA = tempStringA.replaceAll("true", "");
        tempStringA = tempStringA.replaceAll("false", "");

        String tempStringB = set.toString();
        tempStringB = tempStringB.replaceAll("true", "");
        tempStringB = tempStringB.replaceAll("false", "");


        return tempStringA.equals(tempStringB);
    }

}



回答10:


You should take the basic idea of EqualsBuilder but modified to your needs: Create some kind of a list with the members (or better getters) to compare, eg. a HashMap. Now iterate this map, search for the functions in class A with the key entry of the map. Next search for the function of class B with the value entry of the map. Call (invoke) both and compare the output.

HashMap<String,String> mymap=new HashMap<>();
mymap.put("intA","intB");
mymap.put("boolA","boolB");

for(Map.Entry<String,String> e:mymap.entrySet()) { 
  // names not ok, maybe take a bean helper class
  Method m1=a.getClass().getMethod("get"+e.getKey()); // or look for fields if you dont have getters
  Method m2=b.getClass().getMethod("get"+e.getValue());
  Object r1=m1.invoke(a);
  Object r2=m2.invoke(b);
  if (!r1.equals(r2)) 
     return false;
}

Sorry for no real code. Null checks have to be added!




回答11:


        public class Compare {

    public static void main(String[] args) {

        // TODO Auto-generated method stub
        Compare compare= new Compare();
        List<ClassA> myList = Arrays.asList(new ClassA(1, "A", false), new ClassA(2, "B", false));
        Set<ClassB> mySet = new HashSet<ClassB>(Arrays.asList(new ClassB(1, "A", false), new ClassB(2, "B", false)));

       System.out.println( compare.areEqual(myList,mySet));

    }
    public  boolean areEqual(Collection<ClassA> colA,Collection<ClassB> colB){
        boolean equal =false;
        if(colA.size()!=colB.size()){
            return equal;
        }
        Set<Integer> setA=new HashSet<Integer>();
        Set<Integer> setB= new HashSet<Integer>();
        for(ClassA obj : colA){
            setA.add(obj.hashCode());
        }
        for(ClassB obj : colB){
            setB.add(obj.hashCode());
        }
        if(setA.equals(setB)){
            equal=true;
        }
        return equal;
    }
}

class ClassA {
    private int intA;
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + intA;
        result = prime * result + ((strA == null) ? 0 : strA.hashCode());
        return result;
    }



    private String strA;
    private boolean boolA;

    // Constructor
    public ClassA(int intA, String strA, boolean boolA) {
        this.intA = intA;
        this.strA = strA;
        this.boolA = boolA;
    } // Getters and setters etc. below...


}

class ClassB {
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + intB;
        result = prime * result + ((strB == null) ? 0 : strB.hashCode());
        return result;
    }



    private int intB;
    private String strB;
    private boolean boolB;

    // Constructor
       public ClassB(int intB, String strB, boolean boolB) {
        this.intB = intB;
        this.strB = strB;
        this.boolB = boolB;
    } // Getters and setters etc. below...
}

Well i override the hash code method of both class to create hashcode on basis of int and str and a method to create to sets of Intergers , Integer being hash code of each class if you don't want even the hashcode to be overridden let me know will update for that as well




回答12:


May it help..

class ClassA {

        private int intA;
        private String strA;
        private boolean boolA;

        // Constructor
        public ClassA(int intA, String strA, boolean boolA) {
            this.intA = intA;
            this.strA = strA;
            this.boolA = boolA;
        } // Getters and setters etc. below...

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof ClassA) {
                ClassA obj2 = (ClassA) obj;
                return (this.intA == obj2.intA && this.strA.equals(obj2.strA) && this.boolA == obj2.boolA);
            } else {
                ClassB obj2 = (ClassB) obj;
                return (this.intA == obj2.intB && this.strA.equals(obj2.strB) && this.boolA == obj2.boolB);
            }

        }

        @Override
        public int hashCode() {
            int hash = 3;
            hash = 71 * hash + this.intA;
            hash = 71 * hash + Objects.hashCode(this.strA);
            hash = 71 * hash + (this.boolA ? 1 : 0);
            return hash;
        }
    }

    class ClassB {

        private int intB;
        private String strB;
        private boolean boolB;

        // Constructor
        public ClassB(int intB, String strB, boolean boolB) {
            this.intB = intB;
            this.strB = strB;
            this.boolB = boolB;
        } // Getters and setters etc. below...

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof ClassB) {
                ClassB obj2 = (ClassB) obj;
                return (this.intB == obj2.intB && this.strB.equals(obj2.strB) && this.boolB == obj2.boolB);

            } else {
                ClassA obj2 = (ClassA) obj;
                return (this.intB == obj2.intA && this.strB.equals(obj2.strA) && this.boolB == obj2.boolA);
            }

        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 79 * hash + this.intB;
            hash = 79 * hash + Objects.hashCode(this.strB);
            hash = 79 * hash + (this.boolB ? 1 : 0);
            return hash;
        }
    }

    public void test() {
        List<Object> myList = Arrays.asList(new ClassA(1, "A", true),
                new ClassA(1, "A", true));

        System.out.println(myList.get(0).equals(myList.get(1)));

    }



回答13:


Whereas for two single elements the equivalent comparison is unambiguously defined, for collections several variants of the equivalent comparison are possible. One aspect is whether to consider element ordering. Further when ordering is not significant, then the cardinality of equivalent elements (number of matches) might or might not be significant.

Therefore the proposal of using an EquivalenceComparisonBuilder on which together with the two collections and an EquivalenceComparator also the ComparisonType is configured - ComparisonType.ORDERING for strict ordering, ComparisonType.DUPLICATES for strict matches count and ComparisonType.SIMPLE for loose equivalence comparison, where it suffices that for each element in one collection is at least one equivalent element in another collection.

Please note that the implementation of EquivalenceComparator needs to consider null arguments if the collections might contains null elements.

package equivalence;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;

public class Equivalence {

    public static interface EquivalenceComparison<S, T> {
        boolean equivalent();
    }

    public static interface EquivalenceComparator<S, T> {
        boolean equivalent(S s, T t);
    }


    static public class EquivalenceComparisonBuilder<S, T> {

        enum ComparisonType {
            ORDERING, DUPLICATES, SIMPLE
        };

        private Collection<S> ss;
        private Collection<T> ts;
        private EquivalenceComparator<S, T> ec;
        private ComparisonType comparisonType;

        public EquivalenceComparisonBuilder<S, T> setCollections(Collection<S> ss, Collection<T> ts) {
            this.ss = ss;
            this.ts = ts;
            return this;
        }

        public EquivalenceComparisonBuilder<S, T> setEquivalenceComparator(EquivalenceComparator<S, T> ec) {
            this.ec = ec;
            return this;
        }

        public EquivalenceComparisonBuilder<S, T> setComparisonType(ComparisonType comparisonType) {
            this.comparisonType = comparisonType;
            return this;
        }

        public EquivalenceComparison<S, T> comparison() {
            if (comparisonType == null || ss == null || ts == null) {
                throw new NullPointerException();
            }
            switch (comparisonType) {
            case ORDERING:
                return new OrderingComparison<S, T>(ss, ts, ec);
            case DUPLICATES:
                return new DuplicatesComparison<S, T>(ss, ts, ec);
            case SIMPLE:
                return new SimpleComparison<S, T>(ss, ts, ec);
            default:
                throw new IllegalArgumentException("Unknown comparison type");
            }
        }

    }


    private static <S, T> EquivalenceComparator<T, S> mirrored(EquivalenceComparator<S, T> ec) {
        return new EquivalenceComparator<T, S>() {
            @Override
            public boolean equivalent(T t, S s) {
                return ec.equivalent(s, t);
            }
        };
    }


    private static class EquivalencePredicate<S, T> implements Predicate<T> {

        private S s;
        private EquivalenceComparator<S, T> equivalenceComparator;

        public EquivalencePredicate(S s, EquivalenceComparator<S, T> equivalenceComparator) {
            this.s = s;
            this.equivalenceComparator = equivalenceComparator;
        }

        @Override
        public boolean evaluate(T t) {
            return equivalenceComparator.equivalent(s, t);
        }
    }

    static private class OrderingComparison<S, T> implements EquivalenceComparison<S, T> {

        private Collection<S> ss;
        private Collection<T> ts;
        private EquivalenceComparator<S, T> ec;

        public OrderingComparison(Collection<S> ss, Collection<T> ts, EquivalenceComparator<S, T> ec) {
            this.ss = ss;
            this.ts = ts;
            this.ec = ec;
        }

        @Override
        public boolean equivalent() {
            if (ss.size() != ts.size()) {
                return false;
            }
            List<S> ssl = new ArrayList<S>(ss);
            List<T> tsl = new ArrayList<T>(ts);

            for (int i = 0; i < ssl.size(); i++) {
                S s = ssl.get(i);
                T t = tsl.get(i);
                if (!ec.equivalent(s, t)) {
                    return false;
                }
            }
            return true;
        }
    }

    static private class DuplicatesComparison<S, T> implements EquivalenceComparison<S, T> {

        private Collection<S> ss;
        private Collection<T> ts;
        private EquivalenceComparator<S, T> ec;

        public DuplicatesComparison(Collection<S> ss, Collection<T> ts, EquivalenceComparator<S, T> ec) {
            this.ss = ss;
            this.ts = ts;
            this.ec = ec;
        }

        @Override
        public boolean equivalent() {
            if (ss.size() != ts.size()) {
                return false;
            }

            for (S s : ss) {
                Collection<T> matchingTs = CollectionUtils.select(ts, new EquivalencePredicate(s, ec));
                if (matchingTs.size() == 0) {
                    return false;
                }

                T t = matchingTs.iterator().next();
                Collection<S> matchingSs = CollectionUtils.select(ss, new EquivalencePredicate(t, mirrored(ec)));

                if (matchingTs.size() != matchingSs.size()) {
                    return false;
                }
            }
            return true;
        }
    }

    static private class SimpleComparison<S, T> implements EquivalenceComparison<S, T> {

        private Collection<S> ss;
        private Collection<T> ts;
        private EquivalenceComparator<S, T> ec;

        public SimpleComparison(Collection<S> ss, Collection<T> ts, EquivalenceComparator<S, T> ec) {
            this.ss = ss;
            this.ts = ts;
            this.ec = ec;
        }

        @Override
        public boolean equivalent() {           
            for (S s : ss) {
                if (!CollectionUtils.exists(ts, new EquivalencePredicate(s, ec))) {
                    return false;
                }
            }
            for(T t :ts) {
                if (!CollectionUtils.exists(ss, new EquivalencePredicate(t, mirrored(ec)))) {
                    return false;
                }
            }
            return true;
        }

    }
}

Here are few test cases:

package equivalence;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.junit.Assert;
import org.junit.Test;

import equivalence.Equivalence.EquivalenceComparator;
import equivalence.Equivalence.EquivalenceComparisonBuilder;
import equivalence.Equivalence.EquivalenceComparisonBuilder.ComparisonType;

public class EquivalenceExample {

    static class A {

        private int ia;

        private String is;

        private long a;

        public A(int ia, String is, long a) {
            this.ia = ia;
            this.is = is;
            this.a = a;
        }

        public int getIa() {
            return ia;
        }

        public String getIs() {
            return is;
        }

        public long getA() {
            return a;
        }

    }

    static class B {

        private int ib;

        private String is;

        private long b;

        public B(int ib, String is, long b) {
            this.ib = ib;
            this.is = is;
            this.b = b;
        }

        public int getIb() {
            return ib;
        }

        public String getIs() {
            return is;
        }

        public long getB() {
            return b;
        }

    }

    static class ABEquivalenceComparator implements EquivalenceComparator<A, B> {

        static public ABEquivalenceComparator INSTANCE = new ABEquivalenceComparator();

        @Override
        public boolean equivalent(A a, B b) {
            return new EqualsBuilder().append(a.getIa(), b.getIb()).append(a.getIs(), b.getIs()).isEquals();
        }
    }

    @Test
    public void thatOrderingEquivalenceMatchesEquivalentElementsWhenInSameOrder() {
        List<A> as = Arrays.asList(new A(1, "1", 99l), new A(2, "2", 98l));
        LinkedHashSet<B> bs = new LinkedHashSet<B>(Arrays.asList(new B(1, "1", 99l), new B(2, "2", 99l)));

        Assert.assertTrue(new EquivalenceComparisonBuilder<A, B>().setCollections(as, bs)
                .setEquivalenceComparator(ABEquivalenceComparator.INSTANCE).setComparisonType(ComparisonType.ORDERING)
                .comparison().equivalent());
    }

    @Test
    public void thatOrderingEquivalenceDoesNotMatchEquivalentElementsWhenNotSameOrdering() {
        List<A> as = Arrays.asList(new A(1, "1", 99l), new A(2, "2", 98l));
        LinkedHashSet<B> bs = new LinkedHashSet<B>(Arrays.asList(new B(2, "2", 99l), new B(1, "1", 99l)));

        Assert.assertFalse(new EquivalenceComparisonBuilder<A, B>().setCollections(as, bs)
                .setEquivalenceComparator(ABEquivalenceComparator.INSTANCE).setComparisonType(ComparisonType.ORDERING)
                .comparison().equivalent());
    }

    @Test
    public void thatOrderingEquivalenceDoesNotMatchNonEquivalentElements() {
        List<A> as = Arrays.asList(new A(1, "1", 99l), new A(2, "2", 98l));
        LinkedHashSet<B> bs = new LinkedHashSet<B>(Arrays.asList(new B(1, "1", 99l), new B(1, "1", 99l)));

        Assert.assertFalse(new EquivalenceComparisonBuilder<A, B>().setCollections(as, bs)
                .setEquivalenceComparator(ABEquivalenceComparator.INSTANCE).setComparisonType(ComparisonType.ORDERING)
                .comparison().equivalent());
    }

    @Test
    public void thatDuplicatesEquivalenceMatchesEquivalentElementsRegardlessOrder() {
        List<A> as = Arrays.asList(new A(1, "1", 99l), new A(2, "2", 98l));
        LinkedHashSet<B> bs = new LinkedHashSet<B>(Arrays.asList(new B(2, "2", 99l), new B(1, "1", 99l)));

        Assert.assertTrue(new EquivalenceComparisonBuilder<A, B>().setCollections(as, bs)
                .setEquivalenceComparator(ABEquivalenceComparator.INSTANCE).setComparisonType(ComparisonType.DUPLICATES)
                .comparison().equivalent());
    }

    @Test
    public void thatDuplicatesEquivalenceDoesNotMatchesWhenElementsCardinlityDoNotMatch() {
        List<A> as = Arrays.asList(new A(1, "1", 99l), new A(2, "2", 98l), new A(1, "1", 99l));
        LinkedHashSet<B> bs = new LinkedHashSet<B>(
                Arrays.asList(new B(2, "2", 99l), new B(1, "1", 99l), new B(2, "2", 99l)));

        Assert.assertFalse(new EquivalenceComparisonBuilder<A, B>().setCollections(as, bs)
                .setEquivalenceComparator(ABEquivalenceComparator.INSTANCE).setComparisonType(ComparisonType.DUPLICATES)
                .comparison().equivalent());
    }

    @Test
    public void thatSimpleEquivalenceMatchesRegardlessEquivalentElementCardinality() {
        List<A> as = Arrays.asList(new A(1, "1", 99l), new A(2, "2", 98l), new A(1, "1", 99l));
        LinkedHashSet<B> bs = new LinkedHashSet<B>(
                Arrays.asList(new B(2, "2", 99l), new B(1, "1", 99l), new B(2, "2", 99l)));

        Assert.assertTrue(new EquivalenceComparisonBuilder<A, B>().setCollections(as, bs)
                .setEquivalenceComparator(ABEquivalenceComparator.INSTANCE).setComparisonType(ComparisonType.SIMPLE)
                .comparison().equivalent());
    }

    @Test
    public void thatSimpleEquivalenceMatchesRegardlessElementsCount() {
        List<A> as = Arrays.asList(new A(1, "1", 99l), new A(2, "2", 98l));
        LinkedHashSet<B> bs = new LinkedHashSet<B>(
                Arrays.asList(new B(2, "2", 99l), new B(1, "1", 99l), new B(2, "2", 99l)));

        Assert.assertTrue(new EquivalenceComparisonBuilder<A, B>().setCollections(as, bs)
                .setEquivalenceComparator(ABEquivalenceComparator.INSTANCE).setComparisonType(ComparisonType.SIMPLE)
                .comparison().equivalent());
    }

    @Test
    public void thatSimpleEquivalenceDoesMatchesWhenElementsDoNotMatch() {
        List<A> as = Arrays.asList(new A(1, "1", 99l), new A(2, "2", 98l));
        LinkedHashSet<B> bs = new LinkedHashSet<B>(Arrays.asList(new B(2, "2", 99l), new B(3, "3", 99l)));

        Assert.assertFalse(new EquivalenceComparisonBuilder<A, B>().setCollections(as, bs)
                .setEquivalenceComparator(ABEquivalenceComparator.INSTANCE).setComparisonType(ComparisonType.SIMPLE)
                .comparison().equivalent());
    }

    }


来源:https://stackoverflow.com/questions/40717638/how-to-compare-two-collections-for-equivalence-based-on-fields-from-different

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!