"A List is ordered. This allows a List to maintain an order as objects are added and removed from the collection. This is not the same as sorted! Sorted objects form a hierarchy based on how the objects compare to each other, such as alphabetical sorting." I really can't understand the meaning of "ORDERED".... I can add a element in any position by using the add(int index, Object element) method.... so it is not ordered, is it?
Originally posted by Renata fonseca: I really can't understand the meaning of "ORDERED".... I can add a element in any position by using the add(int index, Object element) method.... so it is not ordered, is it?
It is ordered... The order of the list remains in exactly the order that you placed it in. whether you used the list.add(Object) places the object at the end of the list or the list.add(int, Object)places the object at a particular point in the list When you call list.iterator() you will retrieve the objects in exactly the order you placed them in, whereas in a set object, you're not guaranteed what order you can retrieve the objects:
output: Ordered List: one two three four five ------------- Unordered Set: one two five four three
Originally posted by Renata fonseca: Could you explain me when I'd use ordered and unordered interface?
hmmmm I'm awful at coming up with examples... but I'll do my best (realize I'm stretching here, but work with me...) Ordered Collection --> Say you want to hold a set of directions in a collection (for some reason)... you want those directions to be retrieved in the EXACT same order as they were stored
If someone were to follow those directions.. it would be devestating if they weren't returned in the same order -- who knows where they'd end up! Unordered collection --> So.. you finally get to where you're going, and now you pull out your handy-dandy grocery set (note: not a grocery list, a grocery set):