Sitelet https://howtodoinjava.com/java/collections/

Java Collections

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

ArrayList Replace at Index in Java: set() and replaceAll()

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.

Java UnsupportedOperationException: Causes and Fixes

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.

Case-Insensitive Map in Java (TreeMap, Commons, Spring)

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.

Java List Initialization in One Line: List.of and ArrayList

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.

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 Enumeration, Iterator, ListIterator and Spliterator

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 Length and Size

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.

Java Map computeIfAbsent()

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() …

Java ConcurrentSkipListMap

Learn the Java ConcurrentSkipListMap and the major differences between ConcurrentSkipListMap and other Map implementations.

Java UnsupportedOperationException: Causes and How to Fix

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.

Java ArrayDeque with Examples

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 with Example

Java SynchronousQueue is a specific type of BlockingQueue with no internal capacity and is primarily used in exchanging data between two threads.

How to Flatten a Nested List in Java

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.

Convert List to Map in Java

Learn to convert a List to a Map in Java using Streams, Commons collections, and Guava including Multimap class for duplicate list items.

Inverting a Map In Java

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, …

Intoduction to Java Collections

A collection is a group of objects. Java Collections framework consists of classes and interfaces to work with lists, sets, maps and queues.

Java Collection Tutorials

This Java guide will take us through Java Collections framework. We will understand the core concepts and performing the basic operations.

Java List

Lists are the core building blocks of the Collections framework. These tutorials will teach us the basics of different List classes in Java.

Java Map: HashMap vs LinkedHashMap vs TreeMap and More

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.

Get SubMap From Map In Java

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.

Java EnumMap with Examples

Learn to create and work with the Java EnumMap in detail, difference between EnumMap and HashMap and the practical usecases for the same.

Java Nested Map with Examples

A nested Map is Map inside another Map. Learn to create a nested HashMap and add, remove and iterate over the elements with examples.

Java TreeMap vs HashMap

Learn the differences between TreeMap and HashMap in Java as both implements the Map interface but differ in functionality and performance.

Guide to Java IdentityHashMap

IdentityHashMap implements the Map interface and has almost the same features as HashMap. It uses reference equality on key search operations.

Guide to Java ConcurrentMap

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.

Guide to Java WeakHashMap

Learn about the Java WeakHashMap, strong & weak references with examples. Also, learn the differences between WeakHashMap and HashMap.

Java CopyOnWriteArraySet class

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 class

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 …

Java TransferQueue Vs. LinkedTransferQueue

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 …

Java ArrayBlockingQueue class

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

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 Spliterator (with Examples)

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.

About Us

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.