Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   3. Collections

3. Collections
Last updated: 28 January 2013.

A collection is a data structure that contains multiple elements of the same type. The Java platform provides a set of interfaces and classes that implement them in order to address different sorts of data collections. Those interfaces, the classes that implement them and the algorithms that are used in the implementing classes are known as the Java collections framework (JCF).


The interfaces

The core interfaces of the Java collections framework are Collection and Map from the package java.util as you can see in the following picture:

picture showing the JCF interfaces

The above picture shows the hierarchy of the Java collections framework interfaces. For example, the interface BlockingQueue inherits Queue which inherits Collection. Likewise, the interface BlockingDeque inherits both interfaces BlockingQueue and Deque.

Each interface defines a particular type of collection. For example, the interface List is suitable for manipulating an ordered list of elements that can be accessed by their position in the list. Furthermore, the interface List allows duplicate elements. On the contrary, the interface Set does not allow duplicate elements and those elements cannot be accessed by their position in the set.

Therefore, depending on their needs, it is up to developers to choose the class that implements the right interfaces. An important point to note is that the interface Collection inherits the interface Iterable, which allows an object that implements the interface Collection to be the target of the for-each-statement.

The interface Map, although it is included in the Java collections framework, is not a true collection since it maps keys to values. Nonetheless, an object that implements the interface Map can be manipulated as a collection since Map contains a method named values that returns an object of type Collection.


The implementing classes

The table below lists the classes that implement the interfaces of the Java collections framework:

Class Implemented interfaces General purpose
Vector List Synchronized, resizable and ordered array of elements. The order is the insertion order into the array.
ArrayList List Unsynchronized, resizable and ordered array of elements. The order is the insertion order into the array.
ArrayDeque Deque Unsynchronized, resizable and ordered double ended queue. The order is the insertion order into the queue.
LinkedList List, Deque Unsynchronized, resizable and ordered array of elements with double ended queue operations. The order is the insertion order into the array.
HashSet Set Unsynchronized, resizable and unordered collection that does not allow duplicate elements.
LinkedHashSet Set Unsynchronized, resizable and ordered collection the does not allow duplicate elements. The order is the insertion order into the collection.
TreeSet NavigableSet Unsynchronized, resizable and ordered collection that does not allow duplicate elements. The order is the natural ordering of the elements or the order provided by an object that implements the interface Comparator.
Hashtable Map Synchronized, resizable and unordered data structure that maps keys to values.
HashMap Map Unsynchronized, resizable and unordered data structure that maps keys to values.
LinkedHashMap Map Unsynchronized, resizable and ordered data structure that maps keys to values. The order is the insertion order of the keys into the data structure.
TreeMap NavigableMap Unsynchronized, resizable and ordered data structure that maps keys to values. The order is the natural ordering of the keys or the order provided by an object that implements the interface Comparator.


About performance

An unsynchronized collection performs better than its synchronized counterpart because synchronization incurs performance overhead. For example, the operations on an ArrayList object are faster than the same operations on a Vector object. Likewise, a Hashmap object performs better than a HashTable object.

Additionnally, an unordered collection performs better than its ordered counterpart. For example, the operations on a HashMap instance are faster than the same operations on a LinkedHashMap or TreeMap instance.

The next tutorials will show you how to use the classes ArrayList, HashSet and HashMap. To avoid cluttering these tutorials, the other classes are not discussed since they are used in similar ways. So please refer to the Java API documentation for further details.



You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   3. Collections
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   3. Collections  >   3.1. The class ArrayList

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7