Get size of LinkedHashSet in Java

In this tutorial, we will learn how to Get size of LinkedHashSet in Java.

LinkedHashSet in Java is Hash table and linked list implementation of the Set interface. It extends HashSet and implements Set interface. LinkedHashSet maintains a linked list of the entries in the set, in the order in which they were inserted. This allows insertion-order iteration over the set. That is, when iterating through a LinkedHashSet using an iterator, the elements will be returned in the order in which they were inserted. This is also the order in which they are contained in the string returned by toString( ) when called on a LinkedHashSet object.

In we want to calculate the size or no. of elements in LinkedHashSet , then this class in Java provides size() method to calculate the size. The size method returns the size or the no. of elements in LinkedHashSet. The syntax of size() method is:

Syntax of size() method:

Returns:
the number of elements in this set

Example:

Output:

 

Related posts

Leave a Comment