Data Collections

Many real-life applications require a collection of related data. For example:

Often, we want the things stored in a collection (its elements) to be ordered and accessed in a particular way. For example, a printer queue has new jobs added to the end of a queue, and the first job in the queue is the next to be sent to the printer.

Java provides many built-in collection classes that are used to structure data in collections. Each class defines how the elements in the collection are ordered and accessed. The figure below shows four possible ways of structuring a collection of elements.

The figure shows four possible collections of data a variable myData could reference. In the first collection, the data is represented as several unconnected blobs. In the second the data is represented as blobs connected in a circle. The third shows the data represented as blobs connected in a straight line, and the fourth represents data as blobs connected in a tree structure, with a single blob at the top, which connects to two blobs underneath, each of which connects to blobs below that.

One of the simplest kinds of collection in Java is the list, an indexed collection in which elements are kept in the order in which they are added. A list can grow or shrink as elements are added or removed and it can hold repeated elements.

Other useful Collection classes include the set, which is an unordered collection which cannot hold duplicate elements, and the map, which maps a key to a value. Each of these collections can also grow and shrink, as needed.

The Java Collection classes can only be used for collections of object types, although Java has mechanisms that can convert primitive types into equivalent object types in order to overcome this. An array (which is not part of the Collection classes) is a fixed-size indexed collection that can be used to store primitive types as well as object types).

Java compared with Python

Python has fewer built-in collections than Java, but it does also have a set and a map (called a 'dictionary' in Python).

There are several varieties of list in Java, but the ArrayList is similar to a List in Python, although the syntax used is different. Java can only hold data of a single data type in an ArrayList object.

The following code shows how a Java ArrayList and a Python List can be used to produce the same outcomes. Don't worry if you don't understand exactly how each part of the Java code works; this is just to give you a flavour:

Java

ArrayList<String> nameList; //declare an ArrayList of String objects

nameList = new ArrayList<>(); //assign an ArrayList to nameList

nameList.add("John"); //add four elements to nameList

nameList.add("Amel");

nameList.add("Mo");

nameList.add("John");

String aName = nameList.get(0); //copy element at index 0 to aName

System.out.print(aName); //print aName (output is "John")

nameList.remove("John"); //remove the first instance of "John"

aName = nameList.get(0); //copy element at index 0 to aName

System.out.print(aName); //print aName (output is "Amel")

Python

nameList = []

nameList.append('John')

nameList.append('Amel')

nameList.append('Mo')

nameList.append('John')

aName = nameList[0]

print(aName)

nameList.remove('John')

aName = nameList[0]

print(aName)

Test your understanding

  1. A Java list is an ordered collection. True or false?
  2. A Java set can contain duplicates. True or false?
  3. Write a fragment of Java code that gets the element at index 1 of myList and assigns it to a variable myString, of type String. You can assume that myList has been properly declared as an ArrayList with elements that are of type String and that an element exists at index 1.