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.
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).
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:
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")
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)