A List is a kind of Collection that differs from a Set in that the elements of a List are ordered by something other than their value. You can insert elements in any position of a List you like; with a Set, you get no choice. The first position has the index value 0.

Also, Lists permit duplicate entries, i.e. elements for which equals() is true.

List has a bunch of implementations which you can check out at the link above, but the real workhorse is ArrayList, which is nothing more than a List of object instances of a given class and, possibly, its subclasses. (A close second is LinkedList, which provides a type of Iterator that allows you to move back and forth through the elements.)

Using an ArrayList

Constructors

ArrayList has three constructors. Here they are with examples.

ArrayList<Integer> integerList = new ArrayList<Integer>();

// ... or, at a higher level of abstraction...
List<Integer> integerList = new ArrayList<Integer>();

The simplest: This creates a new ArrayList with a capacity of 10 elements. (The JVM increases the capacity as needed when you add more elements. See the documentation for details on the management of capacity.)

Collection<Integer> myCollection = new HashSet<Integer>();
  ...
List<Integer> integerList = new ArrayList<Integer>(myCollection);

This creates an ArrayList initialized with the contents of the Collection provided. This is just the thing for converting a Set to a List. (The implementations of Set have a similar constructor for going the other way.)

int capacity = 25;
List<Integer> integerList = new ArrayList<Integer>(capacity);

This creates an empty ArrayList with an initial capacity equal to the value specified. Very efficient if you know how many elements you’re likely to need, but you’ll still do fine if you never use this constructor.

List Methods

Here are methods, some with differences from the descriptions for Collection, and some new.

boolean add(E e)
void add(int index, E element)

Inherited from Collection, for List, the first version of add() adds the new element to the end of the List.

The second version is specific to List: the element is added at the position specified. For example, myList.add(0, myElement) inserts myElement in the first position of myList, and existing elements are moved down to make room.

boolean addAll(Collection<? extends E>)
boolean addAll(int index, Collection<? extends E>)

Similar to the two versions of add() above, but all the elements in the Collection are added at either the end of the collection, or inserted at the specified position.

E get(int index)

Specific to List: retrieves the element at the specified position. (The first position is 0, the second is 1, etc.

int indexOf(Object o)
int lastIndexOf(Object o)

Specific to List: finds the position of the first (indexof()) or last (lastIndexOf()) occurrence of o in the List–i.e., the first one for which equals(o) is true. If there are none, -1 is returned.

E remove(int index)

Specific to List: removes and returns the element at the specified position.

E set(int index, E element)

Specific to List: returns the element originally at the specified position after replacing it with the element provided.

void sort(Comparator<? super E> c)

Specific to List: sorts the elements using the provided Comparator, or the elements’ natural order if c is null. The sort() method is new to Java 8; in earlier versions, you called the static method Collections.sort(). I’ve always been at a loss to understand why.

Exercise

You’ve seen how to sort a List in more than one way. Here’s your assignment.

  • Create a standard 52-card deck using the Card class, Rank enum, and Suit enum you see above.
  • Display the first dozen or so cards on the console in order to see how they’re arranged. You’ll want a method for this because we’re going to do it three times. Each card’s display should look something like “KING of CLUBS.”
  • Sort the deck in descending order by Rank and Suit by making this the natural order of class Card. (The example above shows sorting in ascending order by Rank only.) Call your display method to see how it turns out.
  • Shuffle the deck. For this you’ll need an instance of java.util.Random to get a random number to assign to each of the Cards (which the downloadable solution does when Card is created–but you can do it after the fact too); you’ll want to enhance the Card class for this purpose. Now, how would you use these values to shuffle the deck? Hint: it involves a Comparator.
  • Call your display method again to show the deck has been shuffled.

Extra credit: Use java.util.Date to get a totally unpredictable seed for your random numbers, so every shuffle looks different.

Download a possible solution here. Click here for a refresher on importing a project into Eclipse.

What You Need to Know

  • The List interface groups objects in a sequence.
  • List has several implementations of which ArrayList is the most frequently used.
  • List is a subinterface of Collection and inherits many of its methods, whose implementations are specific to List.
  • Lists can contain duplicate items–duplicate meaning that a.equals(b) is true.
  • Lists can be sorted according to “natural order” (that provided by the compareTo() method of the elements’ classes), or with a Comparator.

Next: Sets