📦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
Adds and removes elements from both ends of an ArrayDeque.
View Program →Adds, reads, and replaces elements in an ArrayList by index.
View Program →Creates, modifies, and iterates over an ArrayList.
View Program →Converts an ArrayList into a plain array of the same type.
View Program →Walks through an ArrayList three ways: a for-each loop, an explicit Iterator, and an index-based loop.
View Program →Strips duplicate elements out of an ArrayList while keeping the original order.
View Program →Reverses the order of an ArrayList's elements in place.
View Program →Checks whether an ArrayList contains a value and finds its position.
View Program →Sorts an ArrayList of integers into ascending order.
View Program →Counts how many times one specific value appears in a List using Collections.frequency().
View Program →Finds the largest element in an unsorted list using Collections.max().
View Program →Finds the smallest element in an unsorted List using Collections.min().
View Program →Reverses the order of any List in place using Collections.reverse().
View Program →Finds an element's index in a sorted list using Collections.binarySearch().
View Program →Randomly reorders a List using Collections.shuffle(), with a fixed seed for reproducible output.
View Program →Swaps the elements at two given positions in a List using Collections.swap().
View Program →Implements Comparable on a custom class to define its natural ordering.
View Program →Converts a List into a Set by passing it straight into a HashSet constructor.
View Program →Pulls a Map's keys out into a standalone List.
View Program →Pulls every value out of a Map into a standalone List.
View Program →Converts a Set into a List by passing it straight into an ArrayList constructor.
View Program →Uses a HashMap to tally how many times each character shows up in a string.
View Program →Finds the elements shared between two Lists using retainAll().
View Program →Uses a HashMap to find every value that shows up more than once in an array.
View Program →Uses a HashMap to find the first character in a string whose total count is exactly one.
View Program →Stores key-value pairs in a HashMap and looks them up by key.
View Program →Walks through a HashMap's entries, keys, and values using entrySet(), keySet(), and values().
View Program →Adds elements to a HashSet and shows that duplicates are silently ignored.
View Program →Stores key-value pairs in a legacy, synchronized Hashtable.
View Program →Creates a genuinely immutable List using the List.of() factory method.
View Program →Creates a genuinely immutable Map using the Map.of() factory method.
View Program →Creates a genuinely immutable Set using the Set.of() factory method.
View Program →Walks a list with an explicit Iterator and removes matching elements safely during iteration.
View Program →Stores key-value pairs in a LinkedHashMap, which preserves insertion order.
View Program →Adds elements to a LinkedHashSet and shows they iterate back out in the same order they were added.
View Program →Builds up a LinkedList by inserting elements at both its front and back.
View Program →Adds, reads by index, and removes elements from a LinkedList.
View Program →Uses a LinkedList through the Queue interface for first-in-first-out processing.
View Program →Removes elements from both the front and back of a LinkedList.
View Program →Reverses the order of elements in a LinkedList.
View Program →Combines two Lists into a single List using addAll().
View Program →Uses the Queue interface's offer/poll/peek methods to process elements first-in-first-out.
View Program →Deduplicates a List by funneling it through a Set.
View Program →Strips every null entry out of a List using removeIf().
View Program →Sorts a List of custom objects using the natural ordering defined by Comparable.
View Program →Sorts a list of custom objects using an external Comparator instead of natural ordering.
View Program →Uses the legacy Stack class for last-in-first-out access.
View Program →Uses the legacy synchronized Vector class as a resizable list.
View Program →Wraps a mutable List in a read-only view using Collections.unmodifiableList(), and shows the view still reflects changes to the original.
View Program →Uses a HashMap to tally how many times each word shows up in a sentence.
View Program →Sorts a list of objects by one field, then breaks ties using a second field.
View Program →Uses a single Deque both as a stack and as a queue in the same program.
View Program →Triggers ConcurrentModificationException by modifying a list directly while a for-each loop is iterating it.
View Program →Takes an existing HashMap and produces its entries sorted by key, after the fact.
View Program →Takes an existing HashMap and produces its entries sorted by value instead of by key.
View Program →Traverses a list forward and backward, and replaces an element mid-traversal, using ListIterator.
View Program →Adds integers out of order and polls them back out in ascending order.
View Program →Produces a sorted list of a Set's elements, since a Set itself has no defined order.
View Program →Uses ArrayDeque's push/pop/peek methods to get stack behavior without the legacy Stack class.
View Program →Stores key-value pairs in a TreeMap, which keeps them sorted by key automatically.
View Program →Adds integers out of order to a TreeSet and iterates them back out sorted.
View Program →Counts how often each word appears in a sentence.
View Program →Finds whether a linked list loops back on itself, and where the loop begins, with Floyd's tortoise-and-hare algorithm.
View Program →Builds a binary search tree with insert, search, in-order traversal and height, using recursive node links.
View Program →Builds a small hash map from scratch with an array of buckets, collision chaining and automatic resizing.
View Program →Builds a fixed-size cache that evicts the least recently used entry, first with LinkedHashMap and then from scratch.
View Program →Finds the shortest route from one city to every other in a weighted graph using Dijkstra's algorithm and a priority queue.
View Program →Visits every node of a graph breadth-first with a queue and depth-first with recursion, using an adjacency list.
View Program →