| Author |
LinkedList and Comparator
|
Lio Liov
Ranch Hand
Joined: Mar 21, 2012
Posts: 33
|
|
I figure it out !!!
Thanks
I made a
priorityqueue with comparator and now I am trying to convert it to Linked list with comparator
Is it possible at all and How can I do it
Thas how I wass creating the priority queue with comparator .How can I create new LinkedList with comparator
Thanks
|
 |
dennis deems
Ranch Hand
Joined: Mar 12, 2011
Posts: 808
|
|
Lio Liov wrote:How can I create new LinkedList with comparator
You can't.
Maintaining a natural ordering is not one of the jobs that LinkedList is best suited for. LL will NOT impose an ordering by itself, so you would have to sort it every time the contents changed. If you need that functionality there are better data structures to use.
|
 |
Lio Liov
Ranch Hand
Joined: Mar 21, 2012
Posts: 33
|
|
Thanks
I think I did it
It might not be very efficient but works
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 6109
|
|
Dennis Deems wrote:
Lio Liov wrote:How can I create new LinkedList with comparator
You can't.
Well...
Not saying its a good idea, but it IS-A LL, and is uses a Comparator to maintain a sorted order.
A better approach to a SortedList would be involve Preferring Composition Over Inheritance.
Maintaining a natural ordering is not one of the jobs that LinkedList is best suited for. [...] If you need that functionality there are better data structures to use.
Absolutely.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4901
|
|
Lio Liov wrote:Thanks
I think I did it
It might not be very efficient but works
Yup. That'll do it, and it's very clear - but you're right, it won't be quick.
An alternative, if your list is likely to get large is:
(basically a dump-sort-and-reload, which will minimize the sort portion).
But I wouldn't bother unless you find performance is an issue.
HIH
Winston
[Edit] There is also a Skip List, which will maintain an order. Unfortunately, the only implementations provided by the standard libraries are as a java.util.Set or java.util.Map. It's possible that someone like Apache has a List implementation though.
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12950
|
|
Winston Gutkowski wrote:[Edit] There is also a Skip List, which will maintain an order. Unfortunately, the only implementations provided by the standard libraries are as a java.util.Set or java.util.Map. It's possible that someone like Apache has a List implementation though.
The standard library has java.util.concurrent.ConcurrentSkipListMap and java.util.concurrent.ConcurrentSkipListSet
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
dennis deems
Ranch Hand
Joined: Mar 12, 2011
Posts: 808
|
|
Lio Liov wrote:It might not be very efficient but works
Winston Gutkowski wrote:
Yup. That'll do it, and it's very clear - but you're right, it won't be quick.
A greater concern to me would be remembering, everywhere something is added to the list, that it needs to be sorted again.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
Winston Gutkowski wrote:An alternative, if your list is likely to get large is:
(basically a dump-sort-and-reload, which will minimize the sort portion).
Collections.sort returns void, so you should extract creating the copy to before the sorting:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 6109
|
|
Dennis Deems wrote:
Lio Liov wrote:It might not be very efficient but works
Winston Gutkowski wrote:
Yup. That'll do it, and it's very clear - but you're right, it won't be quick.
A greater concern to me would be remembering, everywhere something is added to the list, that it needs to be sorted again.
Which would be why one might define a SortedList that delegates to another List, which in turn does a binary-search-and-insert on add(), and then use that SortedList everywhere you'd otherwise have to remember to sort on add(). (This would also alleviate some of the performance hit from add();sort();
|
 |
 |
|
|
subject: LinkedList and Comparator
|
|
|