Besides having APIs for GUIs, another attraction of Java is that it has an API for representing collections of values. This API, which was new with the Java 2 Platform, is known as the Collections API.
The designers of the Collections API have decided that there are three main ways in which we will want to represent a collection of values:
One of the benefits of using a List is that it allows duplicates, i.e., it allows the same value to appear more than once in the collection. This may be important. For example, if you are representing a collection of CDs, it may be that you have the same CD more than once. Or it may be that you are some kind of collector; perhaps you collect beermats. In this case, you will often have duplicates because these allow you the possibility of swopping one of your duplicates with another collector.
The other benefit of using a List is that it allows values to be ordered in any way you like. For example, suppose you want to represent a mailbox as a collection of messages. The user might want to add a message to this mailbox at some particular position in the mailbox, or they might want to delete a particular message from the mailbox. Or, if you have a queue of people, you will want insertions to be made at the tail of the queue whereas deletions are to be made at the head of the queue. For both of these examples, a List could be used.
For the List interface, the API provides two classes that implement the interface. They are called ArrayList and LinkedList. The class ArrayList should be used if you want to make random accesses to the values of a collection, e.g., for a collection of messages in a mailbox, you will want to access each individual message: you might want to access the 5th message, then the 2nd, then the 7th, and so on. This is a typical situation in which ArrayList would be used.
The other class that implements the List interface is called LinkedList. This should be considered if you want to make frequent insertions and deletions from a list. If insertions/deletions dominate the activities that are performed on the List, then a LinkedList should be considered (instead of an ArrayList). In practice, it seems that the implementation of ArrayList is very good as it is often as fast or better than LinkedList in situations where. intuitively, LinkedList should be faster.
However, if your collection has no duplicates or you do not want such flexibility about ordering, you may want to consider representing the collection using a Set. For the Set interface, a class called HashSet is provided. This gives a fast implementation of:
Unlike the List, it is not possible to control the order in which values are stored in a Set. However, it may be that the values being added to a Set have a natural order. This is an ordering that is based on comparing the values of the collection. For example, for a set of strings, the collection may be ordered in alphabetical order; for a set of people, the collection may be ordered by alphabetical order of the name field of each person; and so on.
For this kind of collection, the designers of the Collections API have provided a subinterface of Set called SortedSet, and a class called TreeSet that implements the SortedSet interface. So, for an object that is of the TreeSet class, the method that iterates through the elements of the set produces the values in this natural order.
Finally, for some collections, a particular part of each value in the collection in some way identifies the value: it is called the key. The distinguishing feature of the Map interface is that it permits us to represent a mapping from keys to values. It could be used to represent a dictionary, a mapping from words to meanings. Or a database that, given a person's name, delivers the personal details of that person.
With the database, it may not be important for the values to be ordered: we may have no requirement to go through the thousands of people in the database in some order. Instead, we just want the values of the collection to be stored as efficiently as possible. For such a collection, the Collections API provides a class called HashMap (that implements the Map interface).
However, in the case of the dictionary, we may want the values of the collection to be sorted by the order of the words, as this will allow us easily to output the dictionary. There is a subinterface of the Map interface called SortedMap, and a class called TreeMap that implements this interface.
The preceding paragraphs summarize the overall design of a large part of the Collections API, and also briefly indicate the situations in which you might use the various interfaces and classes. In this Guide, we will just be looking at Lists.