I have a java class called MeterReading
with two fields String MeterName
and int MeterLevel
.
Now, if the MeterLevel
is between 0 and 3, that is Low, when between 3 and 6, Medium and from 6 above is High. How would I pass in that class MeterReading
to a priority queue? Is this possible?
You have to implements
Comparable
interface of your class. The trick is adding a method to get thelevelClass
(LOW, MEDIUM, HIGH) that is used in thecompareTo
method.The implementation of
compareTo
can be also the following if you need the opposite priority.To add a
MeterReading
to aPriorityQueue
only add it