BAEL-2969: Copying Sets in Java

This commit is contained in:
codehunter34 2019-05-31 23:23:12 -04:00
parent aba7e71359
commit a0282482bc
2 changed files with 116 additions and 29 deletions

View File

@ -1,33 +1,46 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-set</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-collections-set</name>
<packaging>jar</packaging>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-set</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-collections-set</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
</dependency>
<properties>
<commons-collections4.version>4.3</commons-collections4.version>
<guava.version>27.1-jre</guava.version>
</properties>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<properties>
<commons-collections4.version>4.3</commons-collections4.version>
<guava.version>27.1-jre</guava.version>
</properties>
</project>

View File

@ -0,0 +1,74 @@
package com.baeldung.set;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang.SerializationUtils;
import com.google.gson.Gson;
public class CopySets {
public static <T> Set<T> copyByConstructor(Set<T> original) {
Set<T> copy = new HashSet<>(original);
return copy;
}
public static <T> Set<T> copyBySetAddAll(Set<T> original) {
Set<T> copy = new HashSet<>();
copy.addAll(original);
return copy;
}
public static <T> Set<T> copyBySetClone(HashSet<T> original) {
Set<T> copy = (Set<T>) original.clone();
return copy;
}
public static <T> Set<T> copyByJson(Set<T> original) {
Gson gson = new Gson();
String jsonStr = gson.toJson(original);
Set<T> copy = gson.fromJson(jsonStr, Set.class);
return copy;
}
public static <T extends Serializable> Set<T> copyByApacheCommonsLang(Set<T> original) {
Set<T> copy = new HashSet<>();
for (T item : original) {
copy.add((T) SerializationUtils.clone(item));
}
return copy;
}
public static <T> void copyByStreamsAPI(Set<T> original) {
Set<T> copy1 = original.stream()
.collect(Collectors.toSet());
// Skip the first element
Set<T> copy2 = original.stream()
.skip(1)
.collect(Collectors.toSet());
// Filter by comparing the types and attributes
Set<T> copy3 = original.stream()
.filter(f -> f.getClass()
.equals(Integer.class))
.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;
}
}