Clone a Set in Java
Write a Java program to construct a set containing all elements of the specified set and in the same order as the original set. In other words, clone a set in Java. Assume there are no mutable objects in the set, and a shallow copy can be performed.
1. Copy Constructor
The simplest solution is to use a copy constructor of the Set interface for cloning a set. A copy constructor is just a special constructor for creating a new object as a copy of an existing object.
|
1 2 3 4 |
public static<T> Set<T> clone(Set<T> original) { Set<T> copy = new HashSet<>(original); return copy; } |
2. Using addAll() method
The Set interface provides addAll(Collection<? extends E> c) method, which appends all elements of the specified collection at the end of a set. We can use it for copying elements of the given set to an empty result set.
|
1 2 3 4 5 |
public static<T> Set<T> clone(Set<T> original) { Set<T> copy = new HashSet<>(); copy.addAll(original); return copy; } |
3. Using Java 8
We can use Java 8 Stream with Collectors to clone a set, as demonstrated below:
|
1 2 3 4 5 |
public static<T> Set<T> clone(Set<T> original) { Set<T> copy = original.stream() .collect(Collectors.toSet()); return copy; } |
4. Using Object.clone() method
The idea is to iterate through the set, clone each item using the clone() method, and add the cloned item to the result set. Please note that the clone() method provided by the Object class needs to be overridden by implementing the Cloneable interface.
|
1 2 3 4 5 6 7 8 9 10 |
public static Set<User> clone(Set<User> original) throws CloneNotSupportedException { Set<User> copy = new HashSet<>(original.size()); for (User user: original) { copy.add(user.clone()); } return copy; } |
5. Using Apache Commons Lang
We can use Apache Commons Lang’s SerializationUtils class for Serialization and Deserialization of objects. It provides serialize() and deserialize() methods, which can be used for cloning as the serialized object holds no reference to the original object on deserialization. SerializationUtils class also provides clone() method:
|
1 2 3 4 5 6 7 |
public static <T extends Serializable> Set<T> clone(Set<T> original) { Set<T> copy = new HashSet<>(); for (T item: original) { copy.add(SerializationUtils.clone(item)); } return copy; } |
6. JSON
We can also use Google’s Gson library for cloning. The idea is to convert the set to a JSON string and then convert the JSON string back to a new Set.
|
1 2 3 4 5 6 7 8 9 10 |
public static <T> Set<T> clone(Set<T> original) { Gson gson = new Gson(); // Convert the set into a JSON string String jsonString = gson.toJson(original); // Convert the JSON string back to a new Set instance and return it Set<T> copy = gson.fromJson(jsonString, Set.class); return copy; } |
That’s all about cloning a Set in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)