java - checking whether an object is present in a List of Objects on the basis of some member variable -


suppose have defined list

private blockingqueue<mydelayed> delayedids = new delayqueue<>(); 

class mydelayed like: private class mydelayed implements delayed {

    private string myid;     private long creationtime;      mydelayed (string myid) {        this.myid= myid;        this.creationtime = system.currenttimemillis();     }      string getmyid() {         return this.myid;     }      @override     public long getdelay(timeunit unit) {       //todo     }      @override     public int compareto(delayed o) {         //todo     }   } 

now suppose want add object of class mydelayed in delayedids list. can using add function.

but if want add obbject in list if list not contain object of class mydelayed has same myid attribute trying insert.

obviously delayedids .contains(new mydelayed(myid)) not work.

is there easy way check thing ? missing ?

you write , compare every element in list see if contains id. if @ point find matching 1 return true, if loop finished having found none returns false.

public boolean contains(string id){     (mydelayed md : delayedids){          if(md.getmyid().equals(id)){             return true;         }     }     return false; } 

now check before adding like:

if(!contains(mynewobject.getmyid())){     delayedids.add(mynewobject) } 

also, i'd suggest rename delayedids delayedids in order follow coding standards (see variables).


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -