From f9e54fd2f6352f2f86a77d1b46b550afa1488607 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Fri, 21 Aug 2020 14:33:39 +0200 Subject: [PATCH] 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 --- core-java-modules/core-java-10/README.md | 1 + core-java-modules/core-java-10/pom.xml | 24 ++++--- .../conversion/ListSetConversionUnitTest.java | 68 +++++++++++++++++++ java-collections-conversions/README.md | 1 - .../JavaCollectionConversionUnitTest.java | 36 ---------- 5 files changed, 85 insertions(+), 45 deletions(-) create mode 100644 core-java-modules/core-java-10/src/test/java/com/baeldung/java10/collections/conversion/ListSetConversionUnitTest.java diff --git a/core-java-modules/core-java-10/README.md b/core-java-modules/core-java-10/README.md index 2b57ec9064..23f598b902 100644 --- a/core-java-modules/core-java-10/README.md +++ b/core-java-modules/core-java-10/README.md @@ -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) - [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) +- [Converting between a List and a Set in Java](https://www.baeldung.com/convert-list-to-set-and-set-to-list) diff --git a/core-java-modules/core-java-10/pom.xml b/core-java-modules/core-java-10/pom.xml index a9b991852f..b293eb6c2f 100644 --- a/core-java-modules/core-java-10/pom.xml +++ b/core-java-modules/core-java-10/pom.xml @@ -1,22 +1,29 @@ + 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"> 4.0.0 core-java-10 0.1.0-SNAPSHOT core-java-10 jar - http://maven.apache.org - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - ../../ + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + ../ + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + @@ -34,6 +41,7 @@ 10 10 + 4.1 diff --git a/core-java-modules/core-java-10/src/test/java/com/baeldung/java10/collections/conversion/ListSetConversionUnitTest.java b/core-java-modules/core-java-10/src/test/java/com/baeldung/java10/collections/conversion/ListSetConversionUnitTest.java new file mode 100644 index 0000000000..1526d1ae7f --- /dev/null +++ b/core-java-modules/core-java-10/src/test/java/com/baeldung/java10/collections/conversion/ListSetConversionUnitTest.java @@ -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 sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5); + final List targetList = new ArrayList<>(sourceSet); + } + + @Test + public final void givenUsingCoreJava_whenListConvertedToSet_thenCorrect() { + final List sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5); + final Set targetSet = new HashSet<>(sourceList); + } + + @Test + public void givenUsingJava10_whenSetConvertedToList_thenCorrect() { + final Set sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5); + final List targetList = List.copyOf(sourceSet); + } + + @Test + public void givenUsingJava10_whenListConvertedToSet_thenCorrect() { + final List sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5); + final Set targetSet = Set.copyOf(sourceList); + } + + @Test + public final void givenUsingGuava_whenSetConvertedToList_thenCorrect() { + final Set sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5); + final List targetList = Lists.newArrayList(sourceSet); + } + + @Test + public final void givenUsingGuava_whenListConvertedToSet_thenCorrect() { + final List sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5); + final Set targetSet = Sets.newHashSet(sourceList); + } + + @Test + public final void givenUsingCommonsCollections_whenListConvertedToSet_thenCorrect() { + final List sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5); + + final Set targetSet = new HashSet<>(6); + CollectionUtils.addAll(targetSet, sourceList); + } + + @Test + public final void givenUsingCommonsCollections_whenSetConvertedToList_thenCorrect() { + final Set sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5); + + final List targetList = new ArrayList<>(6); + CollectionUtils.addAll(targetList, sourceSet); + } +} diff --git a/java-collections-conversions/README.md b/java-collections-conversions/README.md index 2d3aa41f2d..25a4d11b8b 100644 --- a/java-collections-conversions/README.md +++ b/java-collections-conversions/README.md @@ -5,7 +5,6 @@ This module contains articles about conversions among Collection types and array ### 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 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) - [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) diff --git a/java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java b/java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java index 7b856309f1..7947d1b6c7 100644 --- a/java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java +++ b/java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java @@ -101,42 +101,6 @@ public class JavaCollectionConversionUnitTest { final int[] primitiveTargetArray = ArrayUtils.toPrimitive(targetArray); } - // Set -> List; List -> Set - - public final void givenUsingCoreJava_whenSetConvertedToList_thenCorrect() { - final Set sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5); - final List targetList = new ArrayList<>(sourceSet); - } - - public final void givenUsingCoreJava_whenListConvertedToSet_thenCorrect() { - final List sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5); - final Set targetSet = new HashSet<>(sourceList); - } - - public final void givenUsingGuava_whenSetConvertedToList_thenCorrect() { - final Set sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5); - final List targetList = Lists.newArrayList(sourceSet); - } - - public final void givenUsingGuava_whenListConvertedToSet_thenCorrect() { - final List sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5); - final Set targetSet = Sets.newHashSet(sourceList); - } - - public final void givenUsingCommonsCollections_whenListConvertedToSet_thenCorrect() { - final List sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5); - - final Set targetSet = new HashSet<>(6); - CollectionUtils.addAll(targetSet, sourceList); - } - - public final void givenUsingCommonsCollections_whenSetConvertedToList_thenCorrect() { - final Set sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5); - - final List targetList = new ArrayList<>(6); - CollectionUtils.addAll(targetList, sourceSet); - } - // Map (values) -> Array, List, Set @Test