Fundamentals 14 min read

Master Java Collections: From Lists to Queues and Sets Explained

This article provides a comprehensive overview of Java's collection framework, detailing the core interfaces Collection and Map, their CRUD operations, and the characteristics, APIs, and performance trade‑offs of List, Set, Queue, Deque, Vector, and related implementations.

macrozheng
macrozheng
macrozheng
Master Java Collections: From Lists to Queues and Sets Explained

Java Collections Overview

Java collections, also known as containers, are derived from two main interfaces: Collection and Map. A Collection stores single elements, while a Map stores key‑value pairs.

The learning goals are to understand the relationship between interfaces and classes, become familiar with common APIs, choose appropriate data structures for different scenarios, and be able to discuss their source‑code design in interviews.

Collection Interface and CRUD Operations

The Collection interface defines the typical CRUD methods:

Create : add(E e), addAll(Collection<? extends E> c) Read : contains(Object o), containsAll(Collection<?> c) Update : not directly provided; modify by removing and adding.

Delete : remove(Object o), removeAll(Collection<?> c) Other useful methods: isEmpty(), size(),

toArray()

Code Examples

boolean add(E e);
boolean addAll(Collection<? extends E> c);
boolean remove(Object o);
boolean removeAll(Collection<?> c);
boolean contains(Object o);
boolean containsAll(Collection<?> c);
boolean isEmpty();
int size();
Object[] toArray();

List

List is an ordered, duplicate‑allowing collection. The two main implementations are ArrayList and LinkedList. Choosing between them depends on functionality and efficiency.

Key API and time‑complexity comparison: add(E e): O(1) for both. add(int index, E e): O(n) for both (need to locate index). remove(int index): O(n) for both (shift elements or locate node). remove(Object o): O(n) for both (search then remove). set(int index, E e): O(1) for ArrayList, O(n) for LinkedList. get(int index): O(1) for ArrayList, O(n) for LinkedList.

Guidelines:

Prefer ArrayList for read‑heavy scenarios and when random access is needed.

Use ArrayList for tail‑end insertions/deletions in large data sets.

If complexities are similar, choose ArrayList for lower overhead.

Vector

Vector is a legacy synchronized list implementation that extends AbstractList. It is rarely used today because its synchronization incurs performance penalties.

Queue & Deque

Queue follows FIFO semantics, while Deque supports insertion and removal at both ends.

Queue API has two variants: one that throws exceptions (e.g., add(e), remove(), element()) and one that returns special values (e.g., offer(e), poll(), peek()). Use the same variant consistently.

Deque provides similar pairs of methods for the front and rear, with exception‑throwing and value‑returning versions.

All these operations run in O(1) amortized time.

Implementation Choices

For a standard FIFO queue, use LinkedList or ArrayDeque.

For a priority queue, use PriorityQueue.

For a stack (LIFO), use ArrayDeque rather than the legacy Stack class.

When choosing between ArrayDeque and LinkedList for a regular queue, ArrayDeque is generally faster and uses memory more efficiently, while LinkedList may be needed only for compatibility with older Java versions (pre‑Java 6).

Set

Set is an unordered, duplicate‑free collection. Common implementations:

HashSet : Backed by a hash map, offers O(1) basic operations, no ordering.

LinkedHashSet : Combines hash map with a linked list to preserve insertion order while keeping O(1) performance.

TreeSet : Uses a red‑black tree, provides sorted order with O(log n) operations.

Internally, a Set stores its elements as keys in a backing HashMap with a constant placeholder object as the value.

Summary

The Java collection framework offers a rich set of data structures, each with distinct characteristics and performance trade‑offs. Understanding when to use ArrayList, LinkedList, ArrayDeque, HashSet, TreeSet, and other implementations is essential for writing efficient Java code and succeeding in technical interviews.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaCollectionsListQueueSet
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.