Java中的 LinkedHashSet clone() 方法及示例
LinkedHashSet 类的 clone() 方法用于返回上述哈希集的浅表副本。它只是创建集合的副本。
--> java.util Package
--> LinkedHashSet Class
--> clone() Method
句法:
linked_hash_set.clone()
参数:该方法不带任何参数。
返回类型:该方法仅返回 LinkedHashSet 的副本。
例子:
Java
// Java Program to illustrate clone() Method
// of LinkedHashSet Class
// Importing required classes
import java.io.*;
import java.util.LinkedHashSet;
// Main class
public class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an empty LinkedHashSet
LinkedHashSet set
= new LinkedHashSet();
// Adding elements into the Set
// using add() method
set.add("Welcome");
set.add("To");
set.add("Geeks");
set.add("4");
set.add("Geeks");
// Displaying the LinkedHashSet
System.out.println("LinkedHashSet: " + set);
// Creating a new cloned Set
LinkedHashSet cloned_set = new LinkedHashSet();
// Cloning the above Set
// using clone() method
cloned_set = (LinkedHashSet)set.clone();
// Displaying the new Set after Cloning
System.out.println("The new set: " + cloned_set);
}
}
输出:
LinkedHashSet: [Welcome, To, Geeks, 4]
The new set: [Welcome, To, Geeks, 4]