java - Can we override the intersection method of Guava to compare objects? -
i have tried compare sets of type string using sets.intersection method() in guava. works fine. want know method should implement compare 2 objects? have overridden compareto() method sets.intersection treat similar objects different. can advice please?
thanks.
given implementation of sets.intersection()
:
public static <e> setview<e> intersection(final set<e> set1, final set<?> set2) { //... return new setview<e>() { //... @override public boolean contains(object object) { return set1.contains(object) && set2.contains(object); } @override public boolean containsall(collection<?> collection) { return set1.containsall(collection) && set2.containsall(collection); } }; }
i'd have implement whatever methods needed make contains()
, containsall()
work set
s pass in, because work delegated set
s pass in.
so hashset
s equals()
, hashcode()
, , treeset
s compareto()
if implement comparable
or compare()
if use comparator
(probably want still override equals()
, hashcode()
keep things consistent outside map
too).
Comments
Post a Comment