Get Size of Java LinkedHashSet


To get the size of LinkedHashSet, use the size() method.

First, set the LinkedHashSet and add elements −

LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();
set.add(10);
set.add(20);
set.add(30);
set.add(40);
set.add(50);
set.add(60);

Display the size of the LinkedHashSet created above −

set.size()

The following is an example to get the size of LinkedHashSet −

Example

 Live Demo

import java.util.*;
public class Demo {
   public static void main(String[] args) {
      LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();
      set.add(10);
      set.add(20);
      set.add(30);
      set.add(40);
      set.add(50);
      set.add(60);
      System.out.println("LinkedHashSet elements...");
      System.out.println(set);
      System.out.println("Size: "+set.size());
   }
}

Output

LinkedHashSet elements...
[10, 20, 30, 40, 50, 60]
Size: 6

Updated on: 30-Jun-2020

239 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements