java - How can I find-and-return an object in a Dequeue? -
i'm using double-ended queue (java.util.dequeue) , want find object in queue , return it.
i'm using contains()
method check if queue contains object, don't know how actual instance of object found. instance being searched not same instance, i'm overriding equals()
method test equality of subset of class variables.
if it's not possible using dequeue should use instead? need push objects onto either end of list , remove them start. , able search object , instantiation.
its little unclear me if wish remove remove object searching for, but, 1 possible solution iterator and, well, iterate though list , manually equality check.
iterator<yourclass> = yourdeque.iterator(); yourclass foundinstance = null; while(it.hasnext()) { yourclass obj = it.next(); if(obj.equal(theinstanceyouaresearchingfor)) { foundinstance = obj; break; } } if(foundinstance != null) { yourdeque.remove(foundinstance); // if wish remove }
Comments
Post a Comment