As in the subject: Is equals() affects vector add() remove() methods behaviour.
I have got Vector<T> v;
1.Does the remove() method will work correct if I will not redefine equals() or hashCode() function in type T?
2.Does the add() method will work correct if I will not redefine equals() or hashCode() function in type T?
equals()forT,Vector.removewill use the defaultequals()implementation, which is object identity (==).Vector.add()does not useequals(). It will happily add duplicates.In either case,
Vectordoes not usehashCodeat all, so the implementation ofhashCode(or lack thereof) will not affect the operation ofVectorat all. However, if you redefineequals()for your element type, you must redefinehashCode()for other collection structures to work properly.P.S. You should probably be using
ArrayListinstead ofVector. From the docs forVector:Even when a thread-safe implementation is needed, you are usually better off providing your own synchronization. The single-function-call synchronization offered by
Vectoris usually at the wrong granularity.