Some material adapted from: http://java.sun.com/docs/books/tutorial/collections/index.html
A collection groups multiple elements into a single unit. Examples include the following:
- A poker hand is a collection of cards.
- A mail folder is a collection of individual messages.
- A telephone directory is a collection of name, number pairs.
The Java Collections Framework provides efficient implementation of many common data structures and algorithms. Rather than using simple arrays (which can be tedious to maintain) to store data, a programmer can use the classes provided in the java.util
ArrayLists provide a way to maintain a list of items. Unlike an array, an ArrayList allows the programmer to insert items at and remove items from arbitrary positions without having to worry about moving the remaining items in the list. An ArrayList can also grow or shrink dynamically.
Some of the key operations of an ArrayList are as follows:
- add - you may add an object to the end of the list, or at an arbitrary position. Like an array, the first position of an ArrayList is position 0.
- clear - removes all elements from the list.
- contains - given an object, contains returns true of the ArrayList contains the object and false otherwise.
- get - given an index, returns the object at that position in the ArrayList.
- indexOf - given an object, returns the position of the object in the ArrayList.
- isEmpty - returns true if the ArrayList is empty and false otherwise.
- remove - you may remove a particular object, or the object at a particular index.
- set - given an index and an object, replaces the current object at the specified index with the new object.
- size - returns the number of objects currently in the ArrayList.
An example that declares and initializes a new ArrayList then adds two Strings to the list follows:
The example above uses slightly different syntax than we've seen in previous examples. In particular, the <String>
is new. The classes of the collections framework are generic types
. An ArrayList can hold a list of any type of object. The <String>
allows us to specify the type of object we wish to store in a particular list. The results are that only objects of the correct type can be added to the collection and when objects are removed they need not be cast to the appropriate type.
The example demonstrates three ArrayLists. The first contains Strings and the second and third contain Integers. Also, notice that, although a collection must store a reference, not a primitive type, Java will automatically convert from int to Integer and vice versa.
Static Methods of the Collections Class
The Collections class provides several static methods that operate on ArrayLists and other collections. Two of the most notable methods are sort
. Calling the sort method will sort a given ArrayList, and calling shuffle will arrange the elements of an ArrayList in random order. An example follows:
A Map provides a way to stored data that provides efficient access to a value
given a key
that maps to that value. A good example of this is a search engine. Google, for example, maps the words you enter in your search to a list of the web pages that contain those words. To support this functionality, the HashMap class provides the following methods:
- get - takes an object representing a key and returns the corresponding value.
- put - takes a key and value pair (both objects) and inserts them into the HashMap.
- clear - removes all items from the HashMap.
- isEmpty - returns true if there are no items in the HashMap.
- remove - takes as input an object representing a key and removes the corresponding value from the HashMap.
- keySet - returns a Set of all of the keys in the HashMap.
- values - returns a collection of all of the values in the HashMap.
Like ArrayLists, HashMaps are generic types. Therefore, when declaring or instantiating a HashMap, you should specify the types of both the keys and the values. In the example above, I have created a HashMap that maps Strings to ArrayLists of Strings. This would be appropriate if you wished to store a mapping from a word to a list of filenames where that word occurred.
You might also wish to create a HashMap of HashMaps.
The example above demonstrates how you might store data to make it efficient to access the ISBN number of a book given an author and title. It also demonstrates how to list all books by a given author.