Java Collection Framework
Java Collection Framework
In this tutorial, you will learn about the Java Collection Framework ( JCF ). When you work with data in Java, you often need to store, retrieve, and manipulate groups of objects efficiently. Java provides a powerful framework called the Collections Framework to make this task easier and more organized.
What are Java Collections?
A collection in Java is an object that groups multiple elements into a single unit. Java Collections Framework provides a set of interfaces and classes to handle such groupings of data with ease.
The Collection interface is the root interface in the Java collection hierarchy. A collection represents a group of objects, called elements. This interface is used to pass collections around and manipulate them where general processing is required. You can think of it like containers that hold objects—just like boxes that can store items like books, pens, or toys.
Main Interfaces in Java Collections
The main collection interfaces in the Java collection framework are as follows. All the interfaces are in
java.util package.
Interface | Description |
---|---|
Collection | Root interface for all collections (except maps).
import java.util.Collection; |
List | An ordered collection that allows duplicates.
import java.util.List; |
Set | A collection that doesn’t allow duplicates.
import java.util.Set; |
Queue | A collection designed for holding elements before processing.
import java.util.Queue; |
Map | An object that maps keys to values (not part of Collection, but part of the framework).
import java.util.Map; |
Set
A Set is a collection that contains no duplicate elements. We can use the Set collection when the objects in the collection do not contain duplicates for example playing cards.
public interface Set<E> extends Collection<E>
List
A List is an ordered collection also known as a sequence. Unlike Set, the list can contain duplicate elements in the collection.
public interface List<E> extends Collection<E>
Queue
A Queue collection is designed for holding elements prior to processing with some operations.
public interface Queue<E> extends Collection<E>
Map
A Map is used to map keys to values. A map cannot contain duplicate keys; and each key can map to at most one value.
public interface Map<K, V>
Classes in Java Collections
The main Classes in Java Collections are as follows:
Class | Implements | Description |
---|---|---|
ArrayList | List | Resizable array implementation. |
LinkedList | List, Deque | Elements are doubly-linked. |
HashSet | Set | Stores unique elements; no order. |
TreeSet | Set | Sorted set of unique elements. |
HashMap | Map | Key-value pair storage. |
TreeMap | Map | Sorted key-value pairs. |
Java Tutorials
Java Tutorial on this website: