Java中的HashSet clone()方法

爪哇。util。哈希集。clone()方法用于返回上述哈希集的浅层副本。它只是创建了一个集合的副本。

null

语法:

Hash_Set.clone()

参数: 该方法不采用任何参数。

返回值: 该方法只返回哈希集的一个副本。

下面的程序演示了Java。util。哈希集。clone()方法:

// Java code to illustrate clone()
import java.io.*;
import java.util.HashSet;
public class Hash_Set_Demo {
public static void main(String args[])
{
// Creating an empty HashSet
HashSet<String> set = new HashSet<String>();
// Use add() method to add elements into the Set
set.add( "Welcome" );
set.add( "To" );
set.add( "Geeks" );
set.add( "4" );
set.add( "Geeks" );
// Displaying the HashSet
System.out.println( "HashSet: " + set);
// Creating a new cloned set
HashSet cloned_set = new HashSet();
// Cloning the set using clone() method
cloned_set = (HashSet)set.clone();
// Displaying the new Set after Cloning;
System.out.println( "The new set: " + cloned_set);
}
}


输出:

HashSet: [4, Geeks, Welcome, To]
The new set: [Geeks, Welcome, To, 4]

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享