How to Create Immutable Map in Java?
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.
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.
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 UnsupportedOperationException is an unchecked exception and occurs when a requested operation is not supported in a class.
A Java List can be initialized in various ways. However, initializing a list with multiple elements can be a tedious task. This tutorial will discuss different techniques to initialize a list in one line to make your work easy and reduce boilerplate code. Method List Attributes Java Version List list …
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.
Maps in Java are the core building blocks of the Collections framework. These tutorials will teach the basics of working with different maps.
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.
The Java ListIterator interface is a bidirectional iterator that iterates over the elements in both forward and backward directions.
Java Iterator interface is used to iterate over the elements of a Collection (List, Set or Map). The Iterator helps in retrieving the elements from the specified collection one by one and optionally performs operations over each element. Java Iterator was first introduced in Java 1.2 as a replacement of Enumerations. …
Java Comparator interface used to sort a array or list of objects based on custom order. Custom ordering of elements is imposed by implementing Comparator.compare() method in the objects.
Java Comparable interface is part of Collection Framework. Learn the purpose of Comparable interface and use it in different scenarios. 1. Comparable Interface 1.1. Why Implement Comparable? In Java, if we want to sort a List of elements then we can Collections.sort() method. It sorts the list items according to …
Java LinkedList class is doubly-linked list implementation of the List and Deque interfaces. It implements all optional list operations, and permits all elements (including null). Table of Contents 1. LinkedList Hierarchy 2. LinkedList Features 3. LinkedList Constructors 4. LinkedList Methods 5. LinkedList Example 6. LinkedList Usecases 7. LinkedList Performance 8. …
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.