BAEL-4572: Add List#copyOf and Set#copyOf (#9903)
* BAEL-4572: Move Converting Between a List and a Set in Java to core-java-10 * BAEL-4572: Add Java10 examples
This commit is contained in:
parent
069d72e3d2
commit
f9e54fd2f6
@ -9,3 +9,4 @@ This module contains articles about Java 10 core features
|
|||||||
- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another)
|
- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another)
|
||||||
- [Deep Dive Into the New Java JIT Compiler – Graal](https://www.baeldung.com/graal-java-jit-compiler)
|
- [Deep Dive Into the New Java JIT Compiler – Graal](https://www.baeldung.com/graal-java-jit-compiler)
|
||||||
- [Copying Sets in Java](https://www.baeldung.com/java-copy-sets)
|
- [Copying Sets in Java](https://www.baeldung.com/java-copy-sets)
|
||||||
|
- [Converting between a List and a Set in Java](https://www.baeldung.com/convert-list-to-set-and-set-to-list)
|
||||||
|
@ -1,22 +1,29 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project
|
<project
|
||||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>core-java-10</artifactId>
|
<artifactId>core-java-10</artifactId>
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
<name>core-java-10</name>
|
<name>core-java-10</name>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<url>http://maven.apache.org</url>
|
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>core-java-modules</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../../</relativePath>
|
<relativePath>../</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-collections4</artifactId>
|
||||||
|
<version>${commons-collections4.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
@ -34,6 +41,7 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source.version>10</maven.compiler.source.version>
|
<maven.compiler.source.version>10</maven.compiler.source.version>
|
||||||
<maven.compiler.target.version>10</maven.compiler.target.version>
|
<maven.compiler.target.version>10</maven.compiler.target.version>
|
||||||
|
<commons-collections4.version>4.1</commons-collections4.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -0,0 +1,68 @@
|
|||||||
|
package com.baeldung.java10.collections.conversion;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class ListSetConversionUnitTest {
|
||||||
|
|
||||||
|
// Set -> List; List -> Set
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingCoreJava_whenSetConvertedToList_thenCorrect() {
|
||||||
|
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
|
||||||
|
final List<Integer> targetList = new ArrayList<>(sourceSet);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingCoreJava_whenListConvertedToSet_thenCorrect() {
|
||||||
|
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
|
||||||
|
final Set<Integer> targetSet = new HashSet<>(sourceList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUsingJava10_whenSetConvertedToList_thenCorrect() {
|
||||||
|
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
|
||||||
|
final List<Integer> targetList = List.copyOf(sourceSet);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUsingJava10_whenListConvertedToSet_thenCorrect() {
|
||||||
|
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
|
||||||
|
final Set<Integer> targetSet = Set.copyOf(sourceList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingGuava_whenSetConvertedToList_thenCorrect() {
|
||||||
|
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
|
||||||
|
final List<Integer> targetList = Lists.newArrayList(sourceSet);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingGuava_whenListConvertedToSet_thenCorrect() {
|
||||||
|
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
|
||||||
|
final Set<Integer> targetSet = Sets.newHashSet(sourceList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingCommonsCollections_whenListConvertedToSet_thenCorrect() {
|
||||||
|
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
|
||||||
|
|
||||||
|
final Set<Integer> targetSet = new HashSet<>(6);
|
||||||
|
CollectionUtils.addAll(targetSet, sourceList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenUsingCommonsCollections_whenSetConvertedToList_thenCorrect() {
|
||||||
|
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
|
||||||
|
|
||||||
|
final List<Integer> targetList = new ArrayList<>(6);
|
||||||
|
CollectionUtils.addAll(targetList, sourceSet);
|
||||||
|
}
|
||||||
|
}
|
@ -5,7 +5,6 @@ This module contains articles about conversions among Collection types and array
|
|||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Converting between an Array and a List in Java](https://www.baeldung.com/convert-array-to-list-and-list-to-array)
|
- [Converting between an Array and a List in Java](https://www.baeldung.com/convert-array-to-list-and-list-to-array)
|
||||||
- [Converting between an Array and a Set in Java](https://www.baeldung.com/convert-array-to-set-and-set-to-array)
|
- [Converting between an Array and a Set in Java](https://www.baeldung.com/convert-array-to-set-and-set-to-array)
|
||||||
- [Converting between a List and a Set in Java](https://www.baeldung.com/convert-list-to-set-and-set-to-list)
|
|
||||||
- [Convert a Map to an Array, List or Set in Java](https://www.baeldung.com/convert-map-values-to-array-list-set)
|
- [Convert a Map to an Array, List or Set in Java](https://www.baeldung.com/convert-map-values-to-array-list-set)
|
||||||
- [Converting a List to String in Java](https://www.baeldung.com/java-list-to-string)
|
- [Converting a List to String in Java](https://www.baeldung.com/java-list-to-string)
|
||||||
- [How to Convert List to Map in Java](https://www.baeldung.com/java-list-to-map)
|
- [How to Convert List to Map in Java](https://www.baeldung.com/java-list-to-map)
|
||||||
|
@ -101,42 +101,6 @@ public class JavaCollectionConversionUnitTest {
|
|||||||
final int[] primitiveTargetArray = ArrayUtils.toPrimitive(targetArray);
|
final int[] primitiveTargetArray = ArrayUtils.toPrimitive(targetArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set -> List; List -> Set
|
|
||||||
|
|
||||||
public final void givenUsingCoreJava_whenSetConvertedToList_thenCorrect() {
|
|
||||||
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
|
|
||||||
final List<Integer> targetList = new ArrayList<>(sourceSet);
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void givenUsingCoreJava_whenListConvertedToSet_thenCorrect() {
|
|
||||||
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
|
|
||||||
final Set<Integer> targetSet = new HashSet<>(sourceList);
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void givenUsingGuava_whenSetConvertedToList_thenCorrect() {
|
|
||||||
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
|
|
||||||
final List<Integer> targetList = Lists.newArrayList(sourceSet);
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void givenUsingGuava_whenListConvertedToSet_thenCorrect() {
|
|
||||||
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
|
|
||||||
final Set<Integer> targetSet = Sets.newHashSet(sourceList);
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void givenUsingCommonsCollections_whenListConvertedToSet_thenCorrect() {
|
|
||||||
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
|
|
||||||
|
|
||||||
final Set<Integer> targetSet = new HashSet<>(6);
|
|
||||||
CollectionUtils.addAll(targetSet, sourceList);
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void givenUsingCommonsCollections_whenSetConvertedToList_thenCorrect() {
|
|
||||||
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
|
|
||||||
|
|
||||||
final List<Integer> targetList = new ArrayList<>(6);
|
|
||||||
CollectionUtils.addAll(targetList, sourceSet);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Map (values) -> Array, List, Set
|
// Map (values) -> Array, List, Set
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user