Get Size of Java LinkedHashSet



LinkedHashSet is a collection in Java that maintains the insertion order of elements. It is part of the Java Collections Framework and extends the HashSet class. It stores unique elements and allows null values, but only one null element.

It is similar to HashSet; the main difference is that a LinkedHashSet maintains a linked list holding of the entries of the current object, allowing it to maintain the order of elements.

Let's learn how to get the size of a LinkedHashSet in Java. The following are some example scenarios:

Scenario 1

Input : set = {1, 2, 3, 4, 5}
Output: 5
Explanation: The LinkedHashSet contains 5 elements, so the size is 5.

Scenario 2

Input : set = {}
Output: 0
Explanation: The LinkedHashSet is empty, so the size is 0.

Get the Size of Java LinkedHashSet

The following are various ways to retrieve the size of a LinkedHashSet in Java:

Let's explore each approach in detail:

Using size() method

The size() is a built-in method of the LinkedHashSet class that returns the number of elements in the current object in the form of an integer (which is the size).

The syntax for using the size() method is as follows:

set.size()

Example

In the following example, we are retrieving the size of a LinkedHashSet using the use the size() method:

import java.util.LinkedHashSet;
public class LinkedHashSetSize {
   public static void main(String[] args) {
      LinkedHashSet<Integer>; set = new LinkedHashSet<>();
      
      set.add(1);
      set.add(2);
      set.add(3);
      set.add(4);
      set.add(5);
      
      int size = set.size();
      System.out.println("Size of the LinkedHashSet: " + size); // Output: Size of the LinkedHashSet: 5
   }
}

When you run the above code, it will produce the following output:

Size of the LinkedHashSet: 5

Using a for loop

We can also use a for loop to iterate through the elements of the LinkedHashSet one by one and count the number of elements. 

Example

In the following example, we are using a for loop to count the number of elements of a LinkedHashSet:

import java.util.LinkedHashSet;
public class LinkedHashSetSizeForLoop {
   public static void main(String[] args) {
      LinkedHashSet<Integer> set = new LinkedHashSet<>();
      
      set.add(1);
      set.add(2);
      set.add(3);
      set.add(4);
      set.add(5);
      
      int count = 0;
      for (Integer element : set) {
         count++;
      }
      
      System.out.println("Size of the LinkedHashSet: " + count); 
   }
}

When you run the above code, it will produce the following output:

Size of the LinkedHashSet: 5

Using Streams API

Java Streams API provides a functional approach to process collections of objects. We can use the count() method of the Stream interface to get the size of a LinkedHashSet.

The syntax for using the Streams API to get the size of a LinkedHashSet is as follows:

set.stream().count()

Example

In the following example, we are using the stream() and count() methods to get the size of the set:

import java.util.LinkedHashSet;
import java.util.stream.Collectors;
public class LinkedHashSetSizeStreams {
   public static void main(String[] args) {
      LinkedHashSet<Integer> set = new LinkedHashSet<>();
      
      set.add(1);
      set.add(2);
      set.add(3);
      set.add(4);
      set.add(5);
      
      long size = set.stream().count();
      System.out.println("Size of the LinkedHashSet: " + size); // Output: Size of the LinkedHashSet: 5
   }
}

When you run the above code, it will produce the following output:

Size of the LinkedHashSet: 5

Conclusion

In this article, we have learned how to get the size of a LinkedHashSet in Java using different methods such as size(), for loop, and Streams API. Each method has its use case.

Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-07-29T18:49:52+05:30

364 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements