Java Collection Framework Tutorial with Examples

Java Collection framework tutorial with examples will help you understand how to use the collection classes in an easier way. A collection in Java is an object that stores a group of objects as a single unit. Java collection framework provides several interfaces and classes to help group the objects as a unit.

The Java collection framework has two main interfaces, the Collection interface, and the Map interface. Several other classes and interfaces implement and extend these two main interfaces to make the collection framework.

Here is the pictorial representation of the Java Collection Framework hierarchy.

(click image to open it in a new window)

Java Collection Framework Cheat Sheet

As you can see from the above collection framework hierarchy, the Collection interface and Map interface are at the top of the hierarchy.

Collection Interface

The Collection interface is the root interface and provides common methods like add, remove, clear, contains, equals, hashcode, and iterator.

List interface

The List interface extends the Collections interface. The List represents index based ordered collection of the objects. The elements contained in the List are ordered and can be inserted, accessed or searched based on their index. The list may contain duplicate elements. The main classes implementing the List interface are ArrayList, and LinkedList.

Set interface

The Set interface extends the Collection interface and represents a collection that does not contain any duplicate elements (it can only have one null element as well). The main classes implementing the Set interface are TreeSet, HashSet and LinkedHashSet.

Queue Interface

The Queue interface extends Collection interface and represents a collection that is usually ordered by FIFO (first in first out) order. The main classes implementing the Set interface are LinkedList, ArrayDeque and PriorityQueue.

Map Interface

The map interface is a root interface and allows storing the key value pairs. The map does not allow duplicate keys. Map interface does not guarantee the order of the elements, however, some implementations like TreeMap does. The main classes implementing the Set interface are Hashtable, HashMap, TreeMap and LinkedHashMap.

Utility Classes

In addition to the above mentioned main interfaces and classes, there are two utility classes that are part of the Java collection framework.

Java Collections and Arrays Utility classes

1. Collections class

The Collections class contains static utility methods that either accepts or returns the collection. The collection class provides many useful methods for shuffling, reversing, sorting and searching collection objects.

2. Arrays Class

Similar to the Collections class, the Arrays class contains static utility methods for manipulating arrays. The Arrays class provides many useful methods for sorting, searching, copying and filling the arrays.

Java Collection Examples

List Examples

ArrayList Examples

LinkedList Examples

Comparable Examples

Comparator Examples

HashMap Examples

LinkedHashMap Examples

HashSet Examples

Iterator Examples

ListIterator Examples

TreeMap Examples

TreeSet Examples

Arrays Examples

References:
Java Collection interface Javadoc
Java Map interface Javadoc

Please let me know if you liked the Java Collection framework tutorial with examples in the comments section below.

About the author

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *