java - Avoid Duplicate Records - HashSet -
i'm trying multiple objetcs of dto arraylist
through for
loop. once objects added, avoiding duplicates i'm converting arraylist
hashset
avoid duplicate records.
code snippet -
.... arraylist list = new arraylist(); for( .... ){ class factory = dto.getclass(); object bean = factory.newinstance(); list.add(bean); } return new arraylist(new hashset(list));
but returned arraylist
has duplicates, want avoid.
even if 2 instances of dto contain same values, considered different hashset. considered equal, dto1.equals(dto2)
(and dto2.equals(dto1)
) must return true. must override object.equals()
method in dto in order specify when 2 dto instances equal. , when override equals()
, must override hashcode()
. 2 equal objects must have same hashcode.
read documentation of 2 methods, , use ide generate 2 methods if don't know how implement them.
Comments
Post a Comment