java - Custom Class Used in A List/HashSet -
i have custom class looks below:
public class customclass(){ private int keycode; private boolean up; private string totrigger; public string gettrigger(); public boolean up(); public int keycode(); }
whats best way performance have list of unique values?
i thinking of hashset how make sure unique instances of customclass?
what need override? equals , hashcode? make sure hash set of customclass have unique instances?
thanks
if a.equals(b) true there hashcode() must same.
override both .equals() , .hashcode() in custom class.
use same fields of custom class calculate hashcode used check equality in .equals().
yes it'll make sure there unique instances of customclass in hash-set. go it.
Comments
Post a Comment