| Author |
Collections - Retrieval Order
|
Glen Divers
Ranch Hand
Joined: Jan 21, 2010
Posts: 61
|
|
Hi
I've an area of an application where it is *essential* that when iterating over a collection, I retrieve the elements in the order in which they were added. I've looked at the Javadoc of things like ArrayList, LinkedList and the various Iterator classes, but the Javadocs seem to make no mention of retrieval order. Is there a collection type that definately guarentees the retrieval order. (Obviously if I sort the collection or remove elements then all bets are off). I've done some testing with ArrayList and it appears that, with the level of testing I've done, the answer is "Yes" elements are returned in the order in which they are added, but I'd like to be sure.
(background)
I'm recieving a binary stream (byte[]) over a socket. The stream can consist of a single String Value, a single integer value, multiple string/integer sequences in any order, or repeating sequences made up of multiple string/integer sequences. Currently I'm splitting the stream to create a list of values. These are then being displayed in a web-app where the user may change any of the individual elements and I then need to reconstruct the byte[] to send back. Hence the need to ensure that the individual values are written back in the order in which they were received.
Regards
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
When callind the add method of a List, the element is appended to the end of the list. When iterating over it, you will start from the beginning down to the end of the list. So, yes, when you iterate over a list the elements will go out in the order they went in.
Also, you might want to check Stack and Queue.
|
JDBCSupport - An easy to use, light-weight JDBC framework -
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
List iterations guarantee index order. As Sebastian said, using the non-indexed add method will add elements to the end, and you will get insertion order. The indexed add method allows you to insert elements in the middle.
java.util.Stack is evil and should be utterly destroyed. While a good idea it is badly designed - it extends Vector. This gives it the following drawbacks:
- it is synchronized, even if you don't need it
- it gives you access to any element. Besides The pop, push and peek methods all other Vector methods like remove and add are still there
As the Javadoc for Stack says, Deque is a better replacement.
Besides List, Queue and Deque there is one collection that offers full unmodifiable insertion order: LinkedHashSet. Unlike LinkedHashMap this class does not allow the access of elements to modify the order, so all that remains is insertion order. You may prefer this if you also want to disallow duplicates.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
MohanKumar Soundararajan
Greenhorn
Joined: Nov 22, 2008
Posts: 3
|
|
Rob Prime wrote:Besides List, Queue and Deque there is one collection that offers full unmodifiable insertion order: LinkedHashSet. Unlike LinkedHashMap this class does not allow the access of elements to modify the order, so all that remains is insertion order. You may prefer this if you also want to disallow duplicates.
LinkedHashMap can be used as an unmodifiable insertion-order object by using the default constructor. It also can be used as an access-order object through a special constructor
|
 |
ravindra patil
Ranch Hand
Joined: Sep 01, 2006
Posts: 196
|
|
Yep . i think go for LinkdHashMap or LinkedHashSet , you will get objects how you have inserted them .means in insertion order
correct me if i'm wrong
|
 |
 |
|
|
subject: Collections - Retrieval Order
|
|
|