java - Removing duplicate entities -
i have entity in java follows:
reason{ long id; long userid; long resid; }
i have list of reason in instances contains same userid , resid different ids. can 1 please help me create list of reason not contains same userid , resid former one.
thanks.
there 2 ways of doing this. 1 is, have go on elements once, , in each time go on other elements well, check each 1 against each other.
a more efficient way of doing copying items set, in case have override equals()
method , compare ids. this:
reason{ long id; long userid; long resid; @override public boolean equals(object obj) { homecoming obj instanceof reason && ((reason) obj).userid == this.userid && ((reason) obj).resid == resid; } }
then set in set , in list:
set<customer> noduplicates = new hashset<>(resonlist); resonlist.clear(); resonlist.addall(noduplicates);
your first list should not contain duplicates.
java
No comments:
Post a Comment