Java HashSet clone() Method Last Updated : 23 Jan, 2025 Comments Improve Suggest changes Like Article Like Report The HashSet clone() method in Java is used to return a shallow copy of the given HashSet. It just creates a copy of the set.Syntax of HashSet clone() Methodpublic Object clone()Return Type: This method returns a new HashSet object that contains the same element as the original set.Example: This example demonstrates the use of the clone() method to return a shallow copy of the HashSet. Java //Java program to demonstrates the working of clone() import java.io.*; import java.util.HashSet; public class Geeks { public static void main(String args[]) { // Creating an empty HashSet HashSet<String> hs = new HashSet<String>(); // Use add() method to add elements into the Set hs.add("Geek1"); hs.add("Geek2"); hs.add("Geek3"); hs.add("Geek4"); System.out.println("HashSet: " + hs); // Creating a new cloned set HashSet cs = new HashSet(); // Cloning the set using clone() method cs = (HashSet)hs.clone(); System.out.println("ClonedSet: " + cs); } } OutputHashSet: [Geek4, Geek3, Geek2, Geek1] ClonedSet: [Geek3, Geek2, Geek1, Geek4] Note: The clone() method returns object and casting it to the desired type is necessary when assigning to a HashSet variable. Comment More infoAdvertise with us Next Article Java AbstractSet equals() Method chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions java-hashset +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads Java HashSet HashSet in Java implements the Set interface of Collections Framework. It is used to store the unique elements and it doesn't maintain any specific order of elements. Can store the Null values.Uses HashMap (implementation of hash table data structure) internally.Also implements Serializable and Clon 12 min read Java HashSet add() Method The HashSet add() method in Java is used to add a specific element to the set. This method will add the element only if the specified element is not present in the HashSet. If the element already exists, the method will not add it again. This method ensures that no duplicate elements are added to th 2 min read Java HashSet clear() Method The clear() method of the HashSet class in Java is used to clear all the elements from the HahSet. This method makes the HashSet empty but does not delete the HashSet itself. It simply removes all elements, leaving the set ready for reuse.Syntax of HashSet clear() Methodvoid clear()Key Points:After 1 min read Java HashSet contains() Method The contains() method of the HashSet class in Java is used to check if a specific element is present in the HashSet or not. So, mainly it is used to check if a set contains any particular element.Java// Java program to demonstrates the working of contains() import java.util.*; public class Geeks { p 1 min read HashSet remove() Method in Java The HashSet remove() method in Java is used to remove a specific element from the set if it is present.Note: HashSet and the remove() were introduced in JDK 1.2 as part of the Collections Framework and are not available in earlier versions of Java (JDK 1.0 and JDK 1.1).Example 1: Here, the remove() 2 min read Java HashSet iterator() Method The HashSet iterator() method in Java is used to return an iterator that can be used to iterate over the elements in a HashSet. HashSet does not maintain any specific order of its elements, so the elements are returned in random order when iterated over. The iterator() method provides a way to trave 3 min read Java HashSet isEmpty() Method The HashSet isEmpty() in Java is used to check if the HashSet is empty or not.Syntax of HashSet isEmpty() Methodboolean isEmpty()Return Type: This method returns "true" if the HashSet is empty, otherwise returns "false".Example: This example demonstrates how the isEmpty() method checks whether the g 1 min read Java HashSet size() Method The HashSet size() method in Java is used to get the size of the HashSet or the number of elements present in the HashSet. Syntax of HashSet size() Methodint size()Return Type: This method returns an integer value that represents the number of elements currently stored in the HashSet.Example: This e 1 min read Java HashSet clone() Method The HashSet clone() method in Java is used to return a shallow copy of the given HashSet. It just creates a copy of the set.Syntax of HashSet clone() Methodpublic Object clone()Return Type: This method returns a new HashSet object that contains the same element as the original set.Example: This exam 1 min read Java AbstractSet equals() Method The AbstractSet equals() Method in Java is used to check for equality between two sets. This method verifies whether the elements of one set are equal to the elements of another set.Syntax of AbstractSet equals() Methodpublic boolean equals(Object o)Parameter: The object "o" is to be compared for eq 1 min read Like