Java Collections
Java Collections
Introduction
A collection is a data structure with a group of elements. Here is the hierarchy of Java collection framework:

All the classes shown in the above picture are interfaces, and java.util.Collection and java.util.Map are the two main “root” interfaces of Java collection classes.
java.lang.Iterable
As shown in the collection framework, java.util.Collection is a sub-interface of java.lang.Iterable:
1 | public interface Collection<E> extends Iterable<E> |
java.util.Iterator is an indepent interface. However, in Iterable there is a method called iterator, which can return an Iterator object:
1 | // iterator is an instance method, which |
Therefore, all sub-interfaces of Iterable(such as List and Set) can use Iterator. For example:
1 | import java.util.*; |
The output of the above code is:
1 | I like water |
The Iterable interface also provides a forEach method, which is a higher-order method:
1 | // forEach method is an instance method, which |
Similarly, all sub-interfaces of Iterable(such as List and Set) can use forEach:
1 | import java.util.*; |
The output of the above code is:
1 | I |
java.util.Collection
There are different kinds of collections in Java, which are defined as interfaces:
A
Listis a collection where the elements are stored in a particular order.A
Setis a collection of distinct elements(no duplicates) in no particular order.A
SortedSetis aSetwhose elements can be enumerated in sorted order.A
Queueis like aList, which only allows insertions/deletions at the end points.
Each interface above has one or more implementations.
The List interface has two implementations:
ArrayList: Array
List, which uses array to implement thelist. All the elements of an arraylist are stored in consecutive locations of memory. The time complexity of accessing arbitrary elements isO(1), and the time complexity of insertion/deletion isO(n). ArrayList is a resizable array, which has a growth policy.LinkedList: Linked
List, which uses linkedlist to implement thelist. The elements are stored in nodes and linked to each other via references. The time complexity of accessing arbitrary elements isO(n), and the time complexity of insertion/deletion isO(1).
The Set interface also has two implementations:
TreeSet: Tree
Set, which uses red-black tree(a kind of AVL tree) to implementset. TreeSet is sorted, therefore it only accept objects that implementComparableinterface. The operations of TreeSet, such as searching or insertion/deletion, are inO(log_n).HashSet: Hash
Set, which uses hash table to implementset. HashSet requires the element type to havehashCodefunctions. Normally, the operaions like searching or insertion/deletion are inO(1). But when the hash table gets relatively full, it would degenerate to O(n). HashSet also wastes memory space.
java.util.Map
A Map is a collection of entries. Every entry has two things: the key that we can search for, and the value that is associated with the key.
1 | // There are two type parameters to the Map type: |
Note that in a map, the number of values may not be same as the number of keys, i.e., the number of values could be fewer than the number of the keys. The number of entries is the same as the number of keys.
There are several implementations of Map:
TreeMap<K, V>: Tree
Map, which uses tree(O(log_n)) to implementmap. TreeMap is actually SortedMap. The key typeKshould beComparable, while the value typeVneeds not beComparable.HashMap<K, V>:Hash
Map, which uses hash table(O(1)) to implementmap, and the key typeKneeds to havehashCodefunctions.
Maps are not collections, however, they are also iterable. Maps have an operation that returns their entries as a set:
1 | Set<Map.Entry<K, V>> entrySet (); |
Therefore, we can firstly obtain the entrySet of a map, then iterate the set using a forEach operator. For example:
1 | import java.util.*; |
The output of the above code is:
1 | 2016205027 : CC Huang |
Besides, Maps also have their own forEach operators:
1 | void forEach(BiConsumer<K,V> action) { |
Therefore, we can iterate a map using forEach:
1 | import java.util.*; |
The output of the above code is:
1 | 2016205027 : CC Huang |