The
LinkedHashSetis similar to theHashSet, except thatLinkedHashSetmaintains the insertion order of elements, whereasHashSetdoes not. Read more aboutLinkedHashSethere.
The isEmpty method can be used to check if the LinkedHashSet object is
public boolean isEmpty()
This method doesn’t take any input parameters.
This method returns true if the LinkedHashSet object is empty. Otherwise, false is returned.
import java.util.LinkedHashSet;class isEmpty {public static void main( String args[] ) {LinkedHashSet<String> set = new LinkedHashSet<>();System.out.println("The set is " + set);System.out.println("set.isEmpty : " + set.isEmpty());set.add("hi");System.out.println("\nThe set is " + set);System.out.println("set.isEmpty : " + set.isEmpty());}}
In the code above:
On line number 1: We import the LinkedHashSet class.
On line number 4: We create a new LinkedHashSet object with the name set.
On line number 6: We use the isEmptymethod to check if the set is empty. In our case, true is returned because the set contains no elements.
On line number 8: We add an element hi to the set using the add method.
On line number 10: We use the isEmptymethod to check if the set is empty. In this place, false is returned because the set contains one element (hi).