Get Size of Java LinkedHashSet

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 30-Jan-2019 10:57:38
Advertisements