![]() |
HashSet is used to store a unique set of values in Java. It is a class that stores values and provides efficient access. As we know Java uses call-by-reference. So, if we copy a Set and want to make changes to the copied Set, we want it to not affect the element of the original Set. But if it is a shallow copy, then it affects the original set. So, we use a Deep copy of a set, which creates a separate memory for the given set. If you want to know more about the concept of shallow and deep copy you can go through shallow and deep copy. Program to perform a deep copy of a HashSet in JavaWe can use Copy Constructor in HashSet to achieve Deep copy. Bypassing the original set on the new Set’s constructor. For example, we have a set of Strings and we want to create a deep copy of it. Below is the implementation of creating Deep Copy Using Copy Constructor:Java
Output
Original Set [Geeks, gfg, hello] Copied Set[gfg, hello] Explanation of the above Program:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |