I have created the Event class. As you can see, both hashCode and equals methods use only the id field of type long.
public class Event {
private long id;
private Map<String, Integer> terms2frequency;
private float vectorLength;
@Override
public long hashCode() {
return this.id;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Event other = (Event) obj;
if (id != other.id)
return false;
return true;
}
I will store the objects of this class in the HashSet Collection.
Set<Event> events = new HashSet<Event>();
Since for the hash computation only the field of the type long I'd like to retrieve the elements from the events hashset by computing the hash of the id. E.g.:
events.get(3);
Is it possible or should I use the hashMap for it:
Map<Long, Event> id2event = new HashMap<Long, Event>();
?
getClass() != obj.getClass()but yet you cast toSimpleEventand notEvent.Float.floatToIntBits()on alongis also weird. ThehashCodeformula also doesn't make much sense (just adding 31?)if (Float.floatToIntBits(id) != Float.floatToIntBits(other.id))the same asif (id != other.id)?