This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes LinkedList and Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "LinkedList and Comparator" Watch "LinkedList and Comparator" New topic
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
    
    6

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: 7552
    
  18

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?
Articles by Winston can be found here
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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: 19655
    
  18

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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();
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: LinkedList and Comparator
 
Similar Threads
how to print objects from linked list?
Problem with generics and LinkedList
linked lists
question from nikos blog(generics)
Generics Lower and Upper Bound