java - Erasing duplicates in ArrayList<SearchResults> -


i have pojo class searchresults, contains 4 strings (title, number, date, status) , getter , setter methods it.

in class populate arraylist<searchresults> results, there way can go through list results , erase elements have duplicate number?

i've tried populating new arraylist first passing results linkedhashset didn't work.

 arraylist<searchresults> nodup;   nodup = new arraylist<searchresults>(new linkedhashset<searchresults>(results)); 

i've tried doing .remove(indexof()) didn't work either.

if(nodup.contains(new searchresults("-1","","",""))){nodup.remove(nodup.indexof(new searchresults("-1","","","")));} 

any suggestions?

edit: equals() method in searchresults (wonr refers number)

    @override public boolean equals(object object){     if(object == null){         return false;     }     if(getclass() != object.getclass()){         return false;     }     searchresults result = (searchresults) object;     if((this.wonr == null) ? (result.wonr == null): this.wonr.equals(result.wonr)){         return false;     }     return true; } 

the suggestions implementing hashcode , equals possible options, single number value truly define means these objects equivalent in general case? if not, defining equals , hashcode way seems hack.

without altering definition of equivalence, if in just case want elminiate values same number value, there other approaches can try. didn't give api searchresult class, i'll assume there's accessible field named number.

one quick way use treeset defines idea of equivalence based on underlying comparison operation. write custom comparator looks @ number field , you're go:

java 8

list<searchresult> allresultswithduplicates = // ... populated list comparator<searchresult> comparator =     (left, right) -> integer.compare(left.number, right.number); set<searchresult> uniquenumbers = new treeset<>(comparator); uniquenumbers.addall(allresultswithduplicates); 

as jb nizet mentioned, if searchresult class has getnumber accessor method can use function reference , eliminate lambda expression defining comparator:

comparator<searchreult> comparator = comparator.comparing(searchresult::getnumber); 

java 5-7

in earlier versions of java must implement comparator class yourself. plugs code given above in same way. example assumes there int getnumber() accessor method on searchresult class:

comparator<searchresult> comparator =      new comparator<searchresult>() {         @override         public int compare(searchresult sr1, searchresult sr2) {             // optional support null arguments left             // exercise reader.             return integer.compare(sr1.getnumber(), sr2.getnumber());         }     }; 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -