The Java API contains numerous classes for managing collections (or sets) of objects. A collection allows us to easily model a large number of data values or objects. There are four main types of collections which are available through various API packages; firstly lists. A list is an ordered collection of objects, we’d use this where the order elements appear in a list is meaningful (such as modeling a queue, or ordered list of items). A set is an unordered collection of objects, in which each element may only appear once (as per the normal rules of Set Theory). A stack is a collection type where elements are only added and removed from the front of the list, for example maintaining a list of recent events where the topmost item is always the most recent. Finally, a map is a collection of data pairings, where an element from a data collection is paired with an element from a key collection – for example pairing works (keys) to their definitions (data).
ArrayList
An ArrayList is a Java collection object that allows elements of any object type (except primitive types – which have to be added with wrapper classes) to be stored in it. It is declared with a variable size, and expands “behind the scenes” as more elements are added.
Below is a code example using an ArrayList object to store String elements.
Example
import java.util.ArrayList;
ArrayList<String> aList;
aList = new ArrayList<String>;
aList.add("Hello");
aList.add("World");
aList.add("From Paul!");
for (String anItem: aList){
System.out.println(anItem);
}
In the example, three strings are added into the array one by one, and then a for-loop is run on the array, in turn printing each element of the array to System.out.
Iterators
An iterator is another method of processing each element in a collection, an iterator object is able to repeatedly call the ‘next’ object out of a collection, as demonstrated in the code example below:
Example
import java.util.ArrayList;
import java.util.Iterator;
ArrayList<String> aList;
aList = new ArrayList<String>;
aList.add("Hello");
aList.add("World");
aList.add("From Paul!");
Iterator<String> anIter;
anIter = aList.iterator;
while (anIter.hasNext() ){
String aNote = anIter.next();
System.out.println(aNote);
}
Arrays
A “normal” array, as opposed to an ArrayList is also an ordered collection of object elements, however it is a core component of the Java language, not an additional package like ArrayList and Iterator. Arrays are fixed size, meaning that they must be declared with the maximum number of elements they can store. An array is essentially a collection of “containers” for objects, whether the “container” is in use or not it has to exst. Ultimately an ArrayList works by using a series of arrays, but that level of complexity is abstracted in normal use.
Example
int[] smallArray = new int[8];
smallArray[0] = 1;
smallArray[1] = 2;
System.out.println(smallArray[1]);
Some Other Collection Types…
There are many different collection types available in various Java packages, some other key ones are described below:
- HashSet. A HashSet is a collection type in Java (java.util.HashSet) which implements a set data type, where an element may only exist once within a set, and elements in a set are in no particular order.
- HashMap. A HashMap is a collection type in Java (java.util.HashMap) which implements a map data type, where two sets of data are stored as pairs. Elements are ‘put’ into the collection in pairs, and from either of the values from a pair, the other value can be looked up with a ‘get’ operation.
References:
- Stephan Jamieson – IP/PDS-2 – Block 4 Concept Summary
- Stephan Jamieson – IP/PDS-2 – Block 5 Concept Summary
- Steven Bradley – Using Collections Lecture Slides