BAEL-2969: Copying Sets in Java
This commit is contained in:
parent
a0282482bc
commit
858927c985
@ -2,7 +2,6 @@ package com.baeldung.set;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -12,63 +11,54 @@ import com.google.gson.Gson;
|
|||||||
|
|
||||||
public class CopySets {
|
public class CopySets {
|
||||||
|
|
||||||
public static <T> Set<T> copyByConstructor(Set<T> original) {
|
// Copy Constructor
|
||||||
Set<T> copy = new HashSet<>(original);
|
public static <T> Set<T> copyByConstructor(Set<T> original) {
|
||||||
return copy;
|
Set<T> copy = new HashSet<>(original);
|
||||||
}
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
public static <T> Set<T> copyBySetAddAll(Set<T> original) {
|
// Set.addAll
|
||||||
Set<T> copy = new HashSet<>();
|
public static <T> Set<T> copyBySetAddAll(Set<T> original) {
|
||||||
copy.addAll(original);
|
Set<T> copy = new HashSet<>();
|
||||||
return copy;
|
copy.addAll(original);
|
||||||
}
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
public static <T> Set<T> copyBySetClone(HashSet<T> original) {
|
// Set.clone
|
||||||
Set<T> copy = (Set<T>) original.clone();
|
public static <T> Set<T> copyBySetClone(HashSet<T> original) {
|
||||||
return copy;
|
Set<T> copy = (Set<T>) original.clone();
|
||||||
}
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
public static <T> Set<T> copyByJson(Set<T> original) {
|
// JSON
|
||||||
Gson gson = new Gson();
|
public static <T> Set<T> copyByJson(Set<T> original) {
|
||||||
String jsonStr = gson.toJson(original);
|
Gson gson = new Gson();
|
||||||
Set<T> copy = gson.fromJson(jsonStr, Set.class);
|
String jsonStr = gson.toJson(original);
|
||||||
|
Set<T> copy = gson.fromJson(jsonStr, Set.class);
|
||||||
|
|
||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T extends Serializable> Set<T> copyByApacheCommonsLang(Set<T> original) {
|
// Apache Commons Lang
|
||||||
Set<T> copy = new HashSet<>();
|
public static <T extends Serializable> Set<T> copyByApacheCommonsLang(Set<T> original) {
|
||||||
for (T item : original) {
|
Set<T> copy = new HashSet<>();
|
||||||
copy.add((T) SerializationUtils.clone(item));
|
for (T item : original) {
|
||||||
}
|
copy.add((T) SerializationUtils.clone(item));
|
||||||
return copy;
|
}
|
||||||
}
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
public static <T> void copyByStreamsAPI(Set<T> original) {
|
// Collectors.toSet
|
||||||
Set<T> copy1 = original.stream()
|
public static <T extends Serializable> Set<T> copyByCollectorsToSet(Set<T> original) {
|
||||||
.collect(Collectors.toSet());
|
Set<T> copy = original.stream().collect(Collectors.toSet());
|
||||||
|
|
||||||
// Skip the first element
|
return copy;
|
||||||
Set<T> copy2 = original.stream()
|
}
|
||||||
.skip(1)
|
|
||||||
.collect(Collectors.toSet());
|
|
||||||
|
|
||||||
// Filter by comparing the types and attributes
|
// Using Java 10
|
||||||
Set<T> copy3 = original.stream()
|
public static <T> Set<T> copyBySetCopyOf(Set<T> original) {
|
||||||
.filter(f -> f.getClass()
|
Set<T> copy = Set.copyOf(original);
|
||||||
.equals(Integer.class))
|
return copy;
|
||||||
.collect(Collectors.toSet());
|
}
|
||||||
|
|
||||||
// Null check in case of expecting null values
|
|
||||||
Set<T> copy4 = original.stream()
|
|
||||||
.filter(Objects::nonNull)
|
|
||||||
.collect(Collectors.toSet());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> Set<T> copyByJava8(Set<T> original) {
|
|
||||||
Set<T> copy = Set.copyOf(original);
|
|
||||||
return copy;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user