In this example we will show how to get the size or nubmer of elements stored in Java LinkedHashSet object with size method.
Source Code
package com.beginner.examples;
import java.util.LinkedHashSet;
public class GetSizeOfJavaLinkedHashSet {
public static void main(String[] args) {
//create LinkedHashSet.
LinkedHashSet linkedHashSet = new LinkedHashSet();
linkedHashSet.add("A");
linkedHashSet.add("B");
linkedHashSet.add("C");
linkedHashSet.add("D");
/*
* To get the size of LinkedHashSet use size() method of LinkedHashSet.
*/
System.out.println("The size of LinkedHashSet is : " + linkedHashSet.size());
}
}
Output:
The size of LinkedHashSet is : 4
References
Imported packages in Java documentation: