Search This Blog

Guide to LinkedHashSet Class

In this guide, we will learn the LinkedHashSet class implementation of the Set interface with examples.

LinkedHashSet Class is a Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order).

This guide covers all the important LinkedHashSet class APIs with examples.

What Will We Learn?

Overview of LinkedHashSet class

Create LinkedHashSet Example

LinkedHashSet Insertion Order Example

LinkedHashSet remove API's with Example

Iterating over a LinkedHashSet

LinkedHashSet with User-defined objects

How to make LinkedHashSet thread safe?

1. Overview of LinkedHashSet class

Java LinkedHashSet class is a Hash table and Linked list implementation of the Set interface.

Contains unique elements only like HashSet.

Provides all optional set operations, and permits null elements.

Maintains insertion order.

LinkedHashSet is not synchronized - If multiple threads access a linked hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. Example:

7. How to make LinkedHashSet thread safe?

This class implementation is not synchronized so it is not thread-safe. If multiple threads access a hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. LinkedHashSet must be synchronized externally. Example :