BAEL-2969: Copying Sets in Java
This commit is contained in:
		
							parent
							
								
									aba7e71359
								
							
						
					
					
						commit
						a0282482bc
					
				| @ -1,33 +1,46 @@ | |||||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | <project xmlns="http://maven.apache.org/POM/4.0.0" | ||||||
|     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||||
|     <modelVersion>4.0.0</modelVersion> | 	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||||
|     <artifactId>core-java-collections-set</artifactId> | 	<modelVersion>4.0.0</modelVersion> | ||||||
|     <version>0.1.0-SNAPSHOT</version> | 	<artifactId>core-java-collections-set</artifactId> | ||||||
|     <name>core-java-collections-set</name> | 	<version>0.1.0-SNAPSHOT</version> | ||||||
|     <packaging>jar</packaging> | 	<name>core-java-collections-set</name> | ||||||
|  | 	<packaging>jar</packaging> | ||||||
| 
 | 
 | ||||||
|     <parent> | 	<parent> | ||||||
|         <groupId>com.baeldung</groupId> | 		<groupId>com.baeldung</groupId> | ||||||
|         <artifactId>parent-java</artifactId> | 		<artifactId>parent-java</artifactId> | ||||||
|         <version>0.0.1-SNAPSHOT</version> | 		<version>0.0.1-SNAPSHOT</version> | ||||||
|         <relativePath>../../parent-java</relativePath> | 		<relativePath>../../parent-java</relativePath> | ||||||
|     </parent> | 	</parent> | ||||||
| 
 | 
 | ||||||
|     <dependencies> | 	<dependencies> | ||||||
|     <dependency> | 		<dependency> | ||||||
|         <groupId>com.google.guava</groupId> | 			<groupId>com.google.guava</groupId> | ||||||
|         <artifactId>guava</artifactId> | 			<artifactId>guava</artifactId> | ||||||
|         <version>${guava.version}</version> | 			<version>${guava.version}</version> | ||||||
|     </dependency> | 		</dependency> | ||||||
|     <dependency> | 		<dependency> | ||||||
|         <groupId>org.apache.commons</groupId> | 			<groupId>org.apache.commons</groupId> | ||||||
|         <artifactId>commons-collections4</artifactId> | 			<artifactId>commons-collections4</artifactId> | ||||||
|         <version>${commons-collections4.version}</version> | 			<version>${commons-collections4.version}</version> | ||||||
|     </dependency> | 		</dependency> | ||||||
|     </dependencies> |  | ||||||
| 
 | 
 | ||||||
|     <properties> | 		<dependency> | ||||||
|         <commons-collections4.version>4.3</commons-collections4.version> | 			<groupId>com.google.code.gson</groupId> | ||||||
|         <guava.version>27.1-jre</guava.version> | 			<artifactId>gson</artifactId> | ||||||
|     </properties> | 			<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> | </project> | ||||||
|  | |||||||
| @ -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; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user