Skip to content

Commit d69a81e

Browse files
author
codehunter34
committed
BAEL-2969: Copying Sets in Java
1 parent 858927c commit d69a81e

2 files changed

Lines changed: 43 additions & 43 deletions

File tree

core-java-modules/core-java-collections-set/pom.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0"
2-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
32
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
43
<modelVersion>4.0.0</modelVersion>
54
<artifactId>core-java-collections-set</artifactId>

core-java-modules/core-java-collections-set/src/main/java/com/baeldung/set/CopySets.java

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,54 +11,55 @@
1111

1212
public class CopySets {
1313

14-
// Copy Constructor
15-
public static <T> Set<T> copyByConstructor(Set<T> original) {
16-
Set<T> copy = new HashSet<>(original);
17-
return copy;
18-
}
14+
// Copy Constructor
15+
public static <T> Set<T> copyByConstructor(Set<T> original) {
16+
Set<T> copy = new HashSet<>(original);
17+
return copy;
18+
}
1919

20-
// Set.addAll
21-
public static <T> Set<T> copyBySetAddAll(Set<T> original) {
22-
Set<T> copy = new HashSet<>();
23-
copy.addAll(original);
24-
return copy;
25-
}
20+
// Set.addAll
21+
public static <T> Set<T> copyBySetAddAll(Set<T> original) {
22+
Set<T> copy = new HashSet<>();
23+
copy.addAll(original);
24+
return copy;
25+
}
2626

27-
// Set.clone
28-
public static <T> Set<T> copyBySetClone(HashSet<T> original) {
29-
Set<T> copy = (Set<T>) original.clone();
30-
return copy;
31-
}
27+
// Set.clone
28+
public static <T> Set<T> copyBySetClone(HashSet<T> original) {
29+
Set<T> copy = (Set<T>) original.clone();
30+
return copy;
31+
}
3232

33-
// JSON
34-
public static <T> Set<T> copyByJson(Set<T> original) {
35-
Gson gson = new Gson();
36-
String jsonStr = gson.toJson(original);
37-
Set<T> copy = gson.fromJson(jsonStr, Set.class);
33+
// JSON
34+
public static <T> Set<T> copyByJson(Set<T> original) {
35+
Gson gson = new Gson();
36+
String jsonStr = gson.toJson(original);
37+
Set<T> copy = gson.fromJson(jsonStr, Set.class);
3838

39-
return copy;
40-
}
39+
return copy;
40+
}
4141

42-
// Apache Commons Lang
43-
public static <T extends Serializable> Set<T> copyByApacheCommonsLang(Set<T> original) {
44-
Set<T> copy = new HashSet<>();
45-
for (T item : original) {
46-
copy.add((T) SerializationUtils.clone(item));
47-
}
48-
return copy;
49-
}
42+
// Apache Commons Lang
43+
public static <T extends Serializable> Set<T> copyByApacheCommonsLang(Set<T> original) {
44+
Set<T> copy = new HashSet<>();
45+
for (T item : original) {
46+
copy.add((T) SerializationUtils.clone(item));
47+
}
48+
return copy;
49+
}
5050

51-
// Collectors.toSet
52-
public static <T extends Serializable> Set<T> copyByCollectorsToSet(Set<T> original) {
53-
Set<T> copy = original.stream().collect(Collectors.toSet());
51+
// Collectors.toSet
52+
public static <T extends Serializable> Set<T> copyByCollectorsToSet(Set<T> original) {
53+
Set<T> copy = original.stream()
54+
.collect(Collectors.toSet());
5455

55-
return copy;
56-
}
56+
return copy;
57+
}
5758

58-
// Using Java 10
59-
public static <T> Set<T> copyBySetCopyOf(Set<T> original) {
60-
Set<T> copy = Set.copyOf(original);
61-
return copy;
62-
}
59+
// Using Java 10
60+
public static <T> Set<T> copyBySetCopyOf(Set<T> original) {
61+
Set<T> copy = Set.copyOf(original);
62+
return copy;
63+
}
6364

6465
}

0 commit comments

Comments
 (0)