Java Programs›Collections

📦Java Collections Programs

The Collections Framework is the toolkit you reach for in almost every real Java program. These examples show how to choose between lists, sets, maps and queues, and how to sort, search, count and iterate over them correctly.68 programs · each with full code, output and a step-by-step explanation

Most Asked in Interviews

🔍
Beginner
01beginner
ArrayDeque Example
Queue & Stack

Adds and removes elements from both ends of an ArrayDeque.

View Program →
02beginner
ArrayList Basic Operations
List

Adds, reads, and replaces elements in an ArrayList by index.

View Program →
03beginner
ArrayList Basics
List

Creates, modifies, and iterates over an ArrayList.

View Program →
04beginner
ArrayList Convert to Array
List

Converts an ArrayList into a plain array of the same type.

View Program →
05beginner
ArrayList Iteration
List

Walks through an ArrayList three ways: a for-each loop, an explicit Iterator, and an index-based loop.

View Program →
06beginner
ArrayList Remove Duplicates
List

Strips duplicate elements out of an ArrayList while keeping the original order.

View Program →
07beginner
ArrayList Reverse
List

Reverses the order of an ArrayList's elements in place.

View Program →
08beginner
ArrayList Search
List

Checks whether an ArrayList contains a value and finds its position.

View Program →
09beginner
ArrayList Sorting
List

Sorts an ArrayList of integers into ascending order.

View Program →
10beginner
Collection Frequency
Collections Utility

Counts how many times one specific value appears in a List using Collections.frequency().

View Program →
11beginner
Collection Maximum
Collections Utility

Finds the largest element in an unsorted list using Collections.max().

View Program →
12beginner
Collection Minimum
Collections Utility

Finds the smallest element in an unsorted List using Collections.min().

View Program →
13beginner
Collection Reverse
Collections Utility

Reverses the order of any List in place using Collections.reverse().

View Program →
14beginner
Collection Search
Collections Utility

Finds an element's index in a sorted list using Collections.binarySearch().

View Program →
15beginner
Collection Shuffle
Collections Utility

Randomly reorders a List using Collections.shuffle(), with a fixed seed for reproducible output.

View Program →
16beginner
Collection Swap
Collections Utility

Swaps the elements at two given positions in a List using Collections.swap().

View Program →
17beginner
Comparable Example
Comparator & Comparable

Implements Comparable on a custom class to define its natural ordering.

View Program →
18beginner
Convert List to Set
Set

Converts a List into a Set by passing it straight into a HashSet constructor.

View Program →
19beginner
Convert Map Keys to List
Map

Pulls a Map's keys out into a standalone List.

View Program →
20beginner
Convert Map Values to List
Map

Pulls every value out of a Map into a standalone List.

View Program →
21beginner
Convert Set to List
Set

Converts a Set into a List by passing it straight into an ArrayList constructor.

View Program →
22beginner
Count Character Frequency Using HashMap
Map

Uses a HashMap to tally how many times each character shows up in a string.

View Program →
23beginner
Find Common Elements in Lists
Conversions

Finds the elements shared between two Lists using retainAll().

View Program →
24beginner
Find Duplicate Elements Using HashMap
Map

Uses a HashMap to find every value that shows up more than once in an array.

View Program →
25beginner
Find First Non-Repeated Character Using HashMap
Map

Uses a HashMap to find the first character in a string whose total count is exactly one.

View Program →
26beginner
HashMap Example
Map

Stores key-value pairs in a HashMap and looks them up by key.

View Program →
27beginner
HashMap Iteration
Map

Walks through a HashMap's entries, keys, and values using entrySet(), keySet(), and values().

View Program →
28beginner
HashSet Example
Set

Adds elements to a HashSet and shows that duplicates are silently ignored.

View Program →
29beginner
Hashtable Example
Map

Stores key-value pairs in a legacy, synchronized Hashtable.

View Program →
30beginner
Immutable List
Immutability

Creates a genuinely immutable List using the List.of() factory method.

View Program →
31beginner
Immutable Map
Immutability

Creates a genuinely immutable Map using the Map.of() factory method.

View Program →
32beginner
Immutable Set
Immutability

Creates a genuinely immutable Set using the Set.of() factory method.

View Program →
33beginner
Iterator Example
Iterator

Walks a list with an explicit Iterator and removes matching elements safely during iteration.

View Program →
34beginner
LinkedHashMap Example
Map

Stores key-value pairs in a LinkedHashMap, which preserves insertion order.

View Program →
35beginner
LinkedHashSet Example
Set

Adds elements to a LinkedHashSet and shows they iterate back out in the same order they were added.

View Program →
36beginner
LinkedList Add First/Last
List

Builds up a LinkedList by inserting elements at both its front and back.

View Program →
37beginner
LinkedList Basic Operations
List

Adds, reads by index, and removes elements from a LinkedList.

View Program →
38beginner
LinkedList Queue
Queue & Stack

Uses a LinkedList through the Queue interface for first-in-first-out processing.

View Program →
39beginner
LinkedList Remove First/Last
List

Removes elements from both the front and back of a LinkedList.

View Program →
40beginner
LinkedList Reverse
List

Reverses the order of elements in a LinkedList.

View Program →
41beginner
Merge Two Lists
Conversions

Combines two Lists into a single List using addAll().

View Program →
42beginner
Queue Example
Queue & Stack

Uses the Queue interface's offer/poll/peek methods to process elements first-in-first-out.

View Program →
43beginner
Remove Duplicates Using Set
Set

Deduplicates a List by funneling it through a Set.

View Program →
44beginner
Remove Null Values
Conversions

Strips every null entry out of a List using removeIf().

View Program →
45beginner
Sort Objects Using Comparable
Comparator & Comparable

Sorts a List of custom objects using the natural ordering defined by Comparable.

View Program →
46beginner
Sort Objects Using Comparator
Comparator & Comparable

Sorts a list of custom objects using an external Comparator instead of natural ordering.

View Program →
47beginner
Stack Example
Queue & Stack

Uses the legacy Stack class for last-in-first-out access.

View Program →
48beginner
Vector Example
List

Uses the legacy synchronized Vector class as a resizable list.

View Program →
Intermediate
49intermediate
Collections Unmodifiable Example
Immutability

Wraps a mutable List in a read-only view using Collections.unmodifiableList(), and shows the view still reflects changes to the original.

View Program →
50intermediate
Count Word Frequency Using HashMap
Map

Uses a HashMap to tally how many times each word shows up in a sentence.

View Program →
51intermediate
Custom Comparator
Comparator & Comparable

Sorts a list of objects by one field, then breaks ties using a second field.

View Program →
52intermediate
Deque Example
Queue & Stack

Uses a single Deque both as a stack and as a queue in the same program.

View Program →
53intermediate
Fail-Fast Iterator
Iterator

Triggers ConcurrentModificationException by modifying a list directly while a for-each loop is iterating it.

View Program →
54intermediate
HashMap Sorting by Key
Map

Takes an existing HashMap and produces its entries sorted by key, after the fact.

View Program →
55intermediate
HashMap Sorting by Value
Map

Takes an existing HashMap and produces its entries sorted by value instead of by key.

View Program →
56intermediate
ListIterator Example
Iterator

Traverses a list forward and backward, and replaces an element mid-traversal, using ListIterator.

View Program →
57intermediate
PriorityQueue Example
Queue & Stack

Adds integers out of order and polls them back out in ascending order.

View Program →
58intermediate
Sort Set
Set

Produces a sorted list of a Set's elements, since a Set itself has no defined order.

View Program →
59intermediate
Stack Using Deque
Queue & Stack

Uses ArrayDeque's push/pop/peek methods to get stack behavior without the legacy Stack class.

View Program →
60intermediate
TreeMap Example
Map

Stores key-value pairs in a TreeMap, which keeps them sorted by key automatically.

View Program →
61intermediate
TreeSet Example
Set

Adds integers out of order to a TreeSet and iterates them back out sorted.

View Program →
62intermediate
Word Frequency Program
Map

Counts how often each word appears in a sentence.

View Program →