Gunnlaugur
Gunnlaugur

Reputation: 341

Java PriorityQueue and Comparable interface

Hi there folks

I've been looking at how to implement and use Java PriorityQueue.
The queue I need has to be able to compare priority of two different type of objects.
I found and read this PriorityQueue article, but there is no mention if it's possible to compare two different type of objects.

Do you know if it's possible?

Thanks
Gunnlaugur

Upvotes: 0

Views: 2561

Answers (2)

Alex Abdugafarov
Alex Abdugafarov

Reputation: 6422

Since Object is the most general class and is superclass of everything, you may implement your own Comparator <Object>, declare your queue as PriorityQueue <Object> and pass your comparator to queue's constructor.

Upvotes: 1

PaoloVictor
PaoloVictor

Reputation: 1306

According to Java 1.6's API, you can provide a Comparator to the PriorityQueue constructor. Also, you can let the queue objects' classes implement the Comparable interface, which defines a compareTo method that is invoked to compare the objects.

Upvotes: 2

Related Questions