konsolas
konsolas

Reputation: 1091

Picking elements from priorityqueue that fit criteria

I have an instance of PriorityQueue<SomePlayerObject>. It has been constructed with a Comparator which imposes an ordering that is inconsistent with equals: it orders SomePlayerObject based on the time the player has been online.

SomePlayerObject has a method: boolean isValid().

I want to poll() from the PriorityQueue the first element (according to the custom comparator) that fits the criteria that isValid returns true.

Is this possible?

Upvotes: 0

Views: 691

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198103

The only thing you could really sanely do here is just keep calling the normal poll() method until it isValid(). That's almost certainly the way you should do it.

You could conceivably call queue.removeIf(e -> !e.isValid()) in Java 8, but that's not going to be any more efficient than the previous approach.

Upvotes: 2

Related Questions