Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A good Sorted List for Java

Tags:

java

sorting

I'm looking for a good sorted list for java. Googling around give me some hints about using TreeSet/TreeMap. But these components is lack of one thing: random access to an element in the set. For example, I want to access nth element in the sorted set, but with TreeSet, I must iterate over other n-1 elements before I can get there. It would be a waste since I would have upto several thousands elements in my Set.

Basically, I'm looking for some thing similar to a sorted list in .NET, with ability to add element fast, remove element fast, and have random access to any element in the list.

Has this kind of sorted list implemented somewhere? Thanks.

Edited

My interest in SortedList grows out of this problems: I need to maintains a list of many thousands object (and can grow up to many hundred of thousands). These objects will be persisted to database. I want to randomly select few dozens of element from the whole list. So, I tried to maintain a separated on-memory list that contains the primary keys (Long numbers) of all objects. I need to add/remove keys from the list when object is added / removed from database. I'm using an ArrayList right now but I'm afraid ArrayList would not suit it when the number of records grows. (Imagine you have to iterate over several hundred thousands of elements every time an object is removed from database). Back to the time when I did .NET programming, then I would use a sorted List (List is a .NET class that once Sorted property set to true, will maintain order of its element, and provide binary search that help remove/insert element very quick). I'm hoping that I can find some thing similar from java BCL but unluckily, I didn't find a good match.

like image 457
Phương Nguyễn Avatar asked Apr 18 '10 03:04

Phương Nguyễn


People also ask

Is there any sorted list in Java?

The sorted() Method in JavaThe stream class provides a method named as sorted() which sorts the list in natural order by comparing ASCII values as we discussed in the previous section. The sorted() method used to sort the list of objects or collections of the objects in the ascending order.

Why there is no TreeList in Java?

Tree by definition cannot contain duplicates. In List we can have duplicates, so there is no TreeList (i.e. no SortedList ). List maintains elements in insertion order. So if we want to sort the list we have to use java.

What is the most efficient way of sorting a list in Java 8?

Fastest = Collections. sort ; Most Readable = Stream ; Most memory efficient = Collections.


1 Answers

It seems that you want a list structure with very fast removal and random access by index (not by key) times. An ArrayList gives you the latter and a HashMap or TreeMap give you the former.

There is one structure in Apache Commons Collections that may be what you are looking for, the TreeList. The JavaDoc specifies that it is optimized for quick insertion and removal at any index in the list. If you also need generics though, this will not help you.

like image 165
Kevin Brock Avatar answered Sep 20 '22 11:09

Kevin Brock