Java collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following:
Interfaces: These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy.
Implementations, i.e., Classes: These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.
Algorithms: These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface.
In addition to collections, the framework defines several map interfaces and classes. Maps store key/value pairs. Although maps are not collections in the proper use of the term, but they are fully integrated with collections.
Related Tags
Tutorials
To find the max value in a Java HashMap, stream its entrySet() and call max(Map.Entry.comparingByValue()) to get the key and the value together. Learn the min value, ties, empty maps and null values too.
A Java priority queue (java.util.PriorityQueue) always returns the smallest element first, or the first element by a Comparator. Learn natural and custom order, max-heaps, ties and why a printed queue looks unsorted.
Replace an element in a Java ArrayList with set(index, element), which returns the old element. Find the index by value with indexOf(), replace all matches with Collections.replaceAll() or List.replaceAll(), and avoid IndexOutOfBoundsException.
UnsupportedOperationException is an unchecked exception that lists from Arrays.asList(), List.of() or Stream.toList() throw when the code changes them. Learn the causes, how to read the stack trace and how to fix it with a modifiable copy.
A case-insensitive map in Java treats String keys such as “Lokesh” and “LOKESH” as the same key. Create one with a TreeMap and String.CASE_INSENSITIVE_ORDER, Apache Commons CaseInsensitiveMap or Spring LinkedCaseInsensitiveMap.
Initialize a Java List in one line with List.of() for an unmodifiable list, or new ArrayList<>(List.of(…)) when the list changes later. Compare Arrays.asList(), Stream.toList(), Collections and Guava by mutability and null support.
Learn to create an immutable or unmodifiable Map using the factory methods added in Java 9 and other versions (Java 8 to Java 21) with examples.
Learn to initialize Java HashMap object in different ways such as empty map, pre-populated map, and collecting stream elements into the map.
In Java, all types of enumerations and iterators (such as Iterator, ListIterator, SplitIterator) are simply navigational cursors and the main purpose of these cursors is to iterate over the elements of the collection. Each cursor has its own features, advantages and disadvantages. In this article, we will walk through these …
Java ArrayList does not publicly expose the length of the backing array, and only the count of the stored elements can be retrieved using its size() method.
Learn to sort Java ArrayList in ascending and descending order using ArrayList.sort(), Collections.sort(), Comparator interface and Java 8 Streams.
The Map.computeIfAbsent() method computes the mapped value for a key using a mapping function if the specified key does not exist in the Map or is mapped to a null value. It has been added as the default method in the Map interface in Java 8. 1. When to Use the computeIfAbsent() …
Learn the Java ConcurrentSkipListMap and the major differences between ConcurrentSkipListMap and other Map implementations.
Learn to find the minimum value and the maximum value in a Map using Steam API, Collections API and simple comparison using iteration.
Comparing two Java lists with order ignored is a common requirement during JUnit tests where both lists come from different sources.
java.lang.UnsupportedOperationException means the code tried to change a collection that cannot be changed, such as one from Arrays.asList(), List.of() or Collections.unmodifiableMap(). Copy the data into an ArrayList or HashMap to fix it.
Learn about Java ArrayDeque and its features with practical examples. Learn to use it as Stack and Queue, and the difference from LinkedList.
Java SynchronousQueue is a specific type of BlockingQueue with no internal capacity and is primarily used in exchanging data between two threads.
Learn Queue data structure and the Java Queue interface and implementations with practical examples such as LinkedList, PriorityQueue and ArrayDeque.
Learn to convert Map keys and values to the array, List or Set using the ArrayList and HashSet constructors as well as Stream APIs.
Learn to remove all the occurrences of an element from a given List using Java loops, list methods and Stream API.
Flattening a nested list may seem hard at the beginning, but it’s not. It can be easily done using only plain Java, streams, or external libraries.
Learn to convert a List to a Map in Java using Streams, Commons collections, and Guava including Multimap class for duplicate list items.
Learn to create a List from array using Arrays.asList(array) and new ArrayList(Arrays.asList(array). Learn their differences with examples.
This Java tutorial will teach us how to invert a given Map using different techniques. We will learn to invert Maps with unique values and create Multimap when there are duplicate values. 1. What is an Inverted Map? An inverted Map <V, K> is an instance of the original Map<K, …
Collections.synchronizedMap() provides serial access to the backing Map, and ConcurrentHashMap is a thread-safe alternative to HashMap.
The HashMap, part of the Java Collections framework, is used to store key-value pairs for quick and efficient storage and retrieval operations.
A collection is a group of objects. Java Collections framework consists of classes and interfaces to work with lists, sets, maps and queues.
This Java guide will take us through Java Collections framework. We will understand the core concepts and performing the basic operations.
Java ArrayList class represents a resizable array of objects which allows us to add, remove, find, sort and replace elements.
Lists are the core building blocks of the Collections framework. These tutorials will teach us the basics of different List classes in Java.
A Java Map stores unique keys with one value each. Quick put/get/merge/computeIfAbsent examples, a comparison of HashMap, LinkedHashMap, TreeMap, ConcurrentHashMap, EnumMap, Hashtable and Map.of(), and all our Map tutorials grouped by topic.
Learn to use Java TreeMap, Spring’s LinkedCaseInsensitiveMap and Apache common’s CaseInsensitiveMap for creating case-insensitive Maps.
A submap is a portion or part of a map between the keys of a specific range. Learn how to get submap from map in Java with different ways.
Learn to create and work with the Java EnumMap in detail, difference between EnumMap and HashMap and the practical usecases for the same.
A nested Map is Map inside another Map. Learn to create a nested HashMap and add, remove and iterate over the elements with examples.
Learn to create immutable and unmodifiable maps in Java using Java collections APIs and Guava’s ImmutableMap class.
Learn to convert a Stream into immutable/unmodifiable collection using Stream methods such as stream() and Collectors.toUnmodifiableList().
Learn the differences between TreeMap and HashMap in Java as both implements the Map interface but differ in functionality and performance.
Learn to use BlockingQueue drainTo() method for draining the queue items (polling all or specific number of elements) into a Collection.
IdentityHashMap implements the Map interface and has almost the same features as HashMap. It uses reference equality on key search operations.
ConcurrentMap is an interface in Java Collections and used to create a thread-safe Maps. It stores the key-value pairs in a synchronized way.
Learn about the Java WeakHashMap, strong & weak references with examples. Also, learn the differences between WeakHashMap and HashMap.
CopyOnWriteArraySet is a thread-safe Set in Java. It ensures safe concurrent access by creating a new copy of the internal array for each modification.
Java CopyOnWriteArrayList is a thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array. It’s immutable snapshot style iterator method uses a reference to the state of the array at the point that the iterator …
In Java, the TransferQueue interface is a concurrent BlockingQueue implementation with support to support “synchronous message passing” between producers and consumers. The key feature of TransferQueue is the transfer() method that blocks until the message is handed off to a consumer. 1. TransferQueue Interface The TransferQueue is an interface in …
ArrayBlockingQueue class is Java concurrent and bounded blocking queue implementation backed by an array. It orders elements FIFO (first-in-first-out). The head of the ArrayBlockingQueue is that element that has been on the queue the longest time. The tail of the ArrayBlockingQueue is that element that has been on the queue …
Java PriorityBlockingQueue class is concurrent blocking queue data structure implementation in which objects are processed based on their priority. The “blocking” part of the name is added to imply the thread will block waiting until there’s an item available on the queue.
Java PriorityQueue is an unbounded Queue implementation that processes the items based on priorities. Custom ordering can be enforced with a Comparator.
Unlike traditional iterators, Spliterator is designed with parallelism in mind and mainly helps in parallel processing when the collection or stream has a large number of elements.