d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Priority Queue In Java
Add Reply New Topic New Poll
Member
Posts: 5,299
Joined: Jul 4 2009
Gold: 2,632.57
Nov 19 2014 09:13am
When implementing a priority queue in Java, what do you have to do to get the items to insert in order? Will it automatically place the objects in order based on their compareTo methods, or do I have to do something special when constructing the priority queue?
ex. this is what I have now:
private PriorityQueue<Student> queue = new PriorityQueue<>();
where Student is Comparable

Thanks for any help.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 19 2014 07:18pm
did you read the documentation? it's the second sentence in the javadoc.

https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html

This post was edited by carteblanche on Nov 19 2014 07:19pm
Member
Posts: 5,299
Joined: Jul 4 2009
Gold: 2,632.57
Nov 19 2014 08:42pm
Quote (carteblanche @ Nov 19 2014 08:18pm)
did you read the documentation? it's the second sentence in the javadoc.

https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html


I looked at it, but I don't understand the jargon. I have no idea what it means by "Natural Ordering," and I saw it talking about Comparator, but didn't know if that was something other than the object's compareTo method. That's why I posted here, because I need someone to simplify it =/
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 19 2014 08:57pm
Quote (furbyjs @ Nov 19 2014 09:42pm)
I looked at it, but I don't understand the jargon. I have no idea what it means by "Natural Ordering," and I saw it talking about Comparator, but didn't know if that was something other than the object's compareTo method. That's why I posted here, because I need someone to simplify it =/


if you noticed, 'natural ordering' is a link that refers to Comparable interface, which is where you get compareTo from.

Comparator is an alternative to Comparable. Comparable works great for objects if you only need one type of sort or if its sort order is intuitive/obvious. eg: String, Integer, etc. but what if you have an object like an Item which has itemId, itemcode, itemdescription, and it's tied to different stores? you might want it sorted in many different ways depending on how it's used, so you'd use Comparators for it.
Member
Posts: 5,299
Joined: Jul 4 2009
Gold: 2,632.57
Nov 20 2014 07:03am
Quote (carteblanche @ Nov 19 2014 09:57pm)
if you noticed, 'natural ordering' is a link that refers to Comparable interface, which is where you get compareTo from.

Comparator is an alternative to Comparable. Comparable works great for objects if you only need one type of sort or if its sort order is intuitive/obvious. eg: String, Integer, etc. but what if you have an object like an Item which has itemId, itemcode, itemdescription, and it's tied to different stores? you might want it sorted in many different ways depending on how it's used, so you'd use Comparators for it.


Thanks, that's exactly what I was wanting to hear.
Go Back To Homework Help Topic List
Add Reply New Topic New Poll