java.util.HashSet.clone() Method


Advertisements


Description

The clone() method is used to return a shallow copy of this HashSet instance.

Declaration

Following is the declaration for java.util.HashSet.clone() method.

public Object clone()

Parameters

NA

Return Value

The method call returns a shallow copy of this set.

Exception

NA

Example

The following example shows the usage of java.util.HashSet.clone()

Live Demo
package com.tutorialspoint;

import java.util.*;

public class HashSetDemo {
   public static void main(String args[]) {
      
      // create two hash sets     
      HashSet <String> newset = new HashSet <String>();
      HashSet <String> cloneset = new HashSet <String>();

      // populate hash set
      newset.add("Learning"); 
      newset.add("Easy");
      newset.add("Simply");  

      // clone the hash set
      cloneset = (HashSet)newset.clone();

      System.out.println("Hash set values: "+ newset);      
      System.out.println("Clone Hash set values: "+ cloneset);
   }    
}

Let us compile and run the above program, this will produce the following result.

Hash set values: [Learning, Simply, Easy]
Clone Hash set values: [Learning, Simply, Easy]

java_util_hashset.htm

Advertisements