LISTS

 

Lists allow duplicates. (Sets don’t). Both Lists and Sets allow null elements.

 

■ The older Lists are Vector and Stack.  The newer ones are ArrayList (unordered, much like Vector) and LinkedList (also unordered, but with extra methods for operating on the list's ends, somewhat like Stack)

 

■ Note: There is no "sorted list" implementation for Lists, as TreeSet nicely provides for Set, where the items are automatically put into order for you.  See sorting collections

 

 

how Lists work

 

  Here is how methods operate on the ends and on the middle of a list.

 

When you build a list, think of making a stack of pancakes.  The very first pancakes go onto the bottom of the stack and they stay there.  If you casually remove pancakes or add more, you are dealing with the easily accessible pancakes from the top of the stack.  The last one you add is at the top.

 

 

That's how the Java API documentation (and this site) refers to a list - the top is the end, the  last item you added.  This is important because the top will not have index 0!  The item with index position 0 is always down at the bottom or start of a list. This makes all Java list processing LIFO (last-in first-out) by default. 

 

Creating First-in First-Out (FIFO) Lists.  For FIFO processing, you need use the methods shown above with “first” in their names.