Implement 2 variations of the Equals method for a class in Java

public class Foo{ private final int A; private final int B; public boolean equals(Object o){ //type check omitted return A==oA && B==oB; } } 

I want to have the following .equals() method like this

 public boolean equals(Object o){ return A==oA; } 

The Foo object is first created with field A, B, then I want to send them to Set<E> , which used the 2nd equality method () to compare only field A.

I know that I can create new objects that have only field A, but the overhead will be large. Any tips?

+7
source share
4 answers

Using composition, you can create a FooWrapper class that provides a custom implementation of equals and instead adds this to the set:

 public class FooWrapper { public final Foo foo; //constructor omitted public boolean equals(Object other) { //type check omitted here too return foo.A == other.foo.A; } //hashCode omitted, but you need that too } 

Unfortunately, with the Java Collections API, there is no way to tell the collection to use a custom equals calculation other than the method above or a subclass and override of equals() .

Edit:

It seems to me that instead you can use Map<Integer, Foo> instead, using foo.A as the key and foo as the value (and thereby restricting it to unique A values). I could not tell you if this is suitable without additional context.

+6
source

In the case of Object , only one implementation of equal values ​​can be implemented, since it overrides the inherited method from java.lang.Object . I think you need to decide what equality means for Foo - is A the only significant field; if so just use it. Your alternative is to wrap Foo and provide your own equals method for the WrappedFoo object. Then you can save WrappedFoo to Set .

From a design point of view, it seems that there should be two objects: one, which is a value class for A and which is a value class for B. You can use composition if you need one object to represent A and B.

0
source

I wrote a pairing scheme for this:

The base interface is Matcher (similar to Comparable): http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/util/Matcher.html

MatchableObject is a class that applies a matching strategy to an object: http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/util/MatchableObject.html

Matches is a utility class for wrapping / reversing MatchableObjects: http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/util/Matchers.html

The library is Open Source. You may find this useful.

Homepage:

http://www.softsmithy.org

Download:

http://sourceforge.net/projects/softsmithy/files/softsmithy/

Maven:

 <dependency> <groupid>org.softsmithy.lib</groupid> <artifactid>lib-core</artifactid> <version>0.1</version> </dependency> 
0
source

If the alternative method is equal only for this particular set, I would suggest using a TreeSet (or another SortedSet) instead of a HashSet so that you can use the TreeSet(Comparator) constructor.

0
source

All Articles