Reputation: 21
Alright, I need to be pointed in the right direction. I already have a hash code and an equals method with over ride.
@Override
public int hashCode() {
return type.hashCode() ^ type2.hashCode() ^ type3.hashCode() ; }
@Override
public boolean equals(Object obj) {
if (!(obj instanceof MyObject)) {
return false;
}
MyObject mdc = (MyObject) obj;
return mdc.type.equals(type) && mdc.type2.equals(type2) && mdc.type3.equals(type3);
}
now what if i want to only compare the first and second type and not the third? Can I have two hash and equal methods for later use?
What I want to do is, if type 1 and type2 matches, delete them from the current arraylist and add them to a new list, showing which ones were the same type. This is different from the hash code shown above.
Is there another way within an array, I can see which combination of type1 and type2 appears multiple times?
Ex:
Shooting Halo appears twice, they need to be removed and added to another list, but problem is that I already have a hashcode for previous use.
Edit: I want to thank everyone for their ideas. I have solved this problem by creating another object class with similar getters and setters but with a newer hash code and it worked. Next time I will use a better logic method. My main goal on this post was to know if there was any way to have multiple hashcode and method in one class which apparently isn't possible. :)
Upvotes: 2
Views: 2219
Reputation: 11140
It sounds to me like your real problem isn't the equals, hashcode thing...its trying to handle the case where some Collection already has one of the thing you are trying to put a second one into and trying to detect this with an equals, hashcode thing that is aware of more than just the two objects being compared. I guess I'm on the "rethink your logic" train here and I believe the collection that you are trying to build is more of aMap<String,List<MyObject>>
than the Set<MyObject>
I think you are currently struggling with.
If the data structure you are trying to shove MyObject instances into was Map<String,List<MyObject>>
, the MyObject equals/hashCode question falls away entirely as the default handling of String regarding equals() and hashcode() would just work.
In your example this ends up like...
[ "Shooting Halo", [ MyObject("Shooting Halo"), MyObject("Shooting Halo") ],
[ "Shooting CallofDuty", [ MyObject("Shooting CallofDuty") ],
[ "Racing Forza", [ MyObject("Racing Forza") ]
Once the collection is built, you can parse it and figure out what to do with the Keys that have multiple Values. If you go this path, look to the Google Guava Library for a Multimap as it makes this use case trivial. http://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained
Upvotes: 0
Reputation: 66667
You can't have two methods with same signature in same class, so its not possible (This is common principle for java classes/methods, not for just equals
and hashcode
methods).
As Dennis Meng
commented, you need to re-think your design if you come across such scenario.
Upvotes: 7
Reputation: 54094
now what if i want to only compare the first and second type and not the third?
Sounds to me that your requirements drive to create different classes that can compare properly the objects. So one class knows that it should compare only type1 and type2 while the other knows that it should compare all 3 (perhaps an hierarchy of objects?)
Upvotes: 2
Reputation: 597372
In one class, there is no way. You can subclass your class and override hashCode()
and equals(..)
, and use instances of one class in one case, and the other - in another case. But this whole scenario looks like a candidate for disasters, so reconsider whether you really need it.
In the case when you just want to define multiple sorting orders, use an external Comparator
instance.
Upvotes: 1
Reputation: 824
You can't have two methods with similar signatures. Whatever logic you are mentioning you can put in the existing equals method and return the results accordingly
Upvotes: 0