I have a vector of entries. Each entry is an instance of this class:
public class Entry implements Comparable<Entry>{
private String _key;
private CustomSet _value;
[...]
@Override
public int compareTo(Entry a) {
return this._key.compareTo(a._key);
}
}
The vector is declared as shown below:
Vector<Entry> entries = new Vector<Entry>();
After that, the vector is populated. Then I want to check if certain key is somewhere in the vector. So I do this:
Entry sample = new Entry(key, new CustomSet());
if (entries.contains(sample)) {
// do something
}
This seems not to work. Why? How can I get it to work?
P.S. CustomSet is another user defined class, irrelevant from my point of view
You have to redefine the
equalsmethod in yourEntryclass, because that’s whatcontainsrelies in to determine if an element belongs to a collection, as the docs say:In this case
oiscontain‘s method parameter.