|
11 | 11 |
|
12 | 12 | public class CopySets { |
13 | 13 |
|
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 | + } |
19 | 19 |
|
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 | + } |
26 | 26 |
|
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 | + } |
32 | 32 |
|
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); |
38 | 38 |
|
39 | | - return copy; |
40 | | - } |
| 39 | + return copy; |
| 40 | + } |
41 | 41 |
|
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 | + } |
50 | 50 |
|
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()); |
54 | 55 |
|
55 | | - return copy; |
56 | | - } |
| 56 | + return copy; |
| 57 | + } |
57 | 58 |
|
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 | + } |
63 | 64 |
|
64 | 65 | } |
0 commit comments