Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java List Sorting: Is there a way to keep a list permantly sorted automatically like TreeMap?

In Java you can build up an ArrayList with items and then call:

Collections.sort(list, comparator); 

Is there anyway to pass in the Comparator at the time of list, creation like you can do with TreeMap?

The goal is to be able add an element to the list and instead of having it automatically appended to the end of the list, the list would keep itself sorted based on the Comparator and insert the new element at the index determined by the Comparator. So basically the list might have to re-sort upon every new element added.

Is there anyway to achieve this in this way with the Comparator or by some other similar means?

like image 860
Dave L. Avatar asked Feb 04 '11 22:02

Dave L.


People also ask

Does tree set automatically sort?

Once you add back the object, the treeset will once again sort it.

Which Java set data structure sorts the data automatically?

There are two data structures in the standard Java collections package that sort automatically: the interfaces SortedSet and SortedMap , which are implemented by TreeSet and TreeMap , respectively.

Does list in Java preserve order?

List maintains insertion order of elements, means any element which is inserted before will go on lower index than any element which is inserted after. Set in Java doesn't maintain any order.

Does set sort automatically Java?

sort() and Arrays. sort() APIs. Objects that implement this interface will be automatically sorted when put in a sorted map (as keys) or sorted set (as elements).

How to sort a list in Java?

We can use the following methods to sort the list: Java Stream interface provides two methods for sorting the list: Stream interface provides a sorted () method to sort a list. It is defined in Stream interface which is present in java.util package.

How to sort a list in Java stream interface?

Java Stream interface provides two methods for sorting the list: sorted() method. Stream interface provides a sorted() method to sort a list. It is defined in Stream interface which is present in java.util package. It returns a stream sorted according to the natural order. If the elements are not comparable, it throws java.lang ...

How do I sort objects in a treeset?

By default, objects added are sorted in their natural order, that is, based on their implementation of the Comparable::compareTo interface method. Alternatively, you can pass a Comparator implementation to determine the sort. The TreeSet is a commonly used implantation of SortedSet. You can also find others.

What is the use of sorted method in Java?

The sorted () method used to sort the list of objects or collections of the objects in the ascending order. If the collections of the objects are comparable then it compares and returns the sorted collections of objects; otherwise it throws an exception from java.lang.ClassCastException.


2 Answers

You can change the behaviour of ArrayList

List<MyType> list = new ArrayList<MyType>() {     public boolean add(MyType mt) {          super.add(mt);          Collections.sort(list, comparator);          return true;     } };  

Note: a PriorityQueue is NOT a List, if you didn't care what type of collection it was, the simplest would be to use a TreeSet, which is just like a TreeMap but is a collection. The only advantage PriorityQueue has is to allow duplicates.

Note: resorting is not very efficient for large collections, Using a binary search and inserting an entry would be faster. (but more complicated)

EDIT: A lot depends on what you need the "list" to do. I suggest you write a List wrapper for an ArrayList, LinkedList, PriorityQueue, TreeSet, or one of the other sorted collections and implement the methods which will actually be used. That way you have a good understanding of the requirements for the collection and you can make sure it works correctly for you.

EDIT(2): Since there was so much interest in using binarySearch instead. ;)

List<MyType> list = new ArrayList<MyType>() {     public boolean add(MyType mt) {         int index = Collections.binarySearch(this, mt);         if (index < 0) index = ~index;         super.add(index, mt);         return true;     } }; 
like image 95
Peter Lawrey Avatar answered Sep 20 '22 17:09

Peter Lawrey


Everyone is suggesting PriorityQueue. However, it is important to realize that if you iterate over the contents of a PriorityQueue, the elements will not be in sorted order. You are only guaranteed to get the "minimum" element from the methods peek(), poll(), etc.

A TreeSet seems to be a better fit. The caveats would be that, as a Set, it can't contain duplicate elements, and it doesn't support random access with an index.

like image 27
erickson Avatar answered Sep 19 '22 17:09

erickson