In Java I want to store elements in ascending order by size. Before insertion I need to treat all the smaller elements. So I start enumerating the list treating smaller elements, and when I meet a larger element, I want to insert the new node before this larger element. What data structure would be the good choice? ArrayList
may not be a good choice as it shifts elements to the right when inserting in the middle. I looked at the API of LinkedList
, but I do not find methods for inserting before/after a given node in the list. There is an add
method that "inserts the specified element at the specified position in this list", but it requires the position as an integer. So does it mean that the Java implementation traverses the list from the beginning until it finds the specified position? It seems quite expensive. I have the position (a node) where I want to insert, just some references should be changed correctly to include the new node in the list. Any suggestions?
Thanks,
Jabba
Update: Thanks everyone for the answers, I could solve the problem with LinkedList using ListIterator. Then I made a comparative test and I got some surprising results. So, the goal is to maintain elements in a list/array in a sorted way. For this I compared four alternatives: (1) Vector, (2) ArrayList, (3) LinkedList, and (4) MyLinkedList, this is my own implementation (very simple). For the test I created an array with 1000 elements and I filled it with random integers between 1 and 100. Then I added these elements in each container in a loop. When this array was added 30 times: MyLinkedList (2.7 sec), LinkedList (6.6 sec), ArrayList (6.9 sec), Vector (7.5 sec). Adding the array 100 times: MyLinkedList (80.7 sec), ArrayList (82.5 sec), Vector (92.7 sec), LinkedList (176.3 sec). Adding the array 200 times: ArrayList (304.3 sec), Vector (332.7 sec), MyLinkedList (381.2 sec), LinkedList (610.4 sec). Conclusion: ArrayList is always faster than Vector. Since it's not synchronised, it's not very surprising. Though the difference is not too significant. If the list is not too large (up to 100,000 elements in my case), I got the best performance with an own implementation. Surprisingly LinkedList performs quite poor. As the size of the list grows, LinkedList is worse and worse. Final conclusion: if the list is not too big, you can have the best performance with an own implementation. If the size of the list is very big, use ArrayList. This result surprises me because ArrayList has to move lots of elements while a linked list only has to change some references. But as ArrayList is based upon an array, despite the high number of shifts it can outperform a linked list.
Extra info: In all four cases, when inserting a new element, I enumerate the list from the beginning until I find a larger element, then I insert the new node before it. I do this because the application needs to process the smaller elements. That's why I don't do binary search on Vector and ArrayList.