Skip to content

Commit cdb3b1f

Browse files
authored
BAEL-4572: Add List#copyOf and Set#copyOf (eugenp#9903)
* BAEL-4572: Move Converting Between a List and a Set in Java to core-java-10 * BAEL-4572: Add Java10 examples
1 parent 5cebc4a commit cdb3b1f

5 files changed

Lines changed: 85 additions & 45 deletions

File tree

core-java-modules/core-java-10/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ This module contains articles about Java 10 core features
99
- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another)
1010
- [Deep Dive Into the New Java JIT Compiler – Graal](https://www.baeldung.com/graal-java-jit-compiler)
1111
- [Copying Sets in Java](https://www.baeldung.com/java-copy-sets)
12+
- [Converting between a List and a Set in Java](https://www.baeldung.com/convert-list-to-set-and-set-to-list)

core-java-modules/core-java-10/pom.xml

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project
3-
xmlns="http://maven.apache.org/POM/4.0.0"
4-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
66
<modelVersion>4.0.0</modelVersion>
77
<artifactId>core-java-10</artifactId>
88
<version>0.1.0-SNAPSHOT</version>
99
<name>core-java-10</name>
1010
<packaging>jar</packaging>
11-
<url>http://maven.apache.org</url>
1211

1312
<parent>
14-
<groupId>com.baeldung</groupId>
15-
<artifactId>parent-modules</artifactId>
16-
<version>1.0.0-SNAPSHOT</version>
17-
<relativePath>../../</relativePath>
13+
<groupId>com.baeldung.core-java-modules</groupId>
14+
<artifactId>core-java-modules</artifactId>
15+
<version>0.0.1-SNAPSHOT</version>
16+
<relativePath>../</relativePath>
1817
</parent>
1918

19+
<dependencies>
20+
<dependency>
21+
<groupId>org.apache.commons</groupId>
22+
<artifactId>commons-collections4</artifactId>
23+
<version>${commons-collections4.version}</version>
24+
</dependency>
25+
</dependencies>
26+
2027
<build>
2128
<plugins>
2229
<plugin>
@@ -34,6 +41,7 @@
3441
<properties>
3542
<maven.compiler.source.version>10</maven.compiler.source.version>
3643
<maven.compiler.target.version>10</maven.compiler.target.version>
44+
<commons-collections4.version>4.1</commons-collections4.version>
3745
</properties>
3846

3947
</project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.java10.collections.conversion;
2+
3+
import com.google.common.collect.Lists;
4+
import com.google.common.collect.Sets;
5+
import org.apache.commons.collections4.CollectionUtils;
6+
import org.junit.Test;
7+
8+
import java.util.ArrayList;
9+
import java.util.HashSet;
10+
import java.util.List;
11+
import java.util.Set;
12+
13+
public class ListSetConversionUnitTest {
14+
15+
// Set -> List; List -> Set
16+
17+
@Test
18+
public final void givenUsingCoreJava_whenSetConvertedToList_thenCorrect() {
19+
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
20+
final List<Integer> targetList = new ArrayList<>(sourceSet);
21+
}
22+
23+
@Test
24+
public final void givenUsingCoreJava_whenListConvertedToSet_thenCorrect() {
25+
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
26+
final Set<Integer> targetSet = new HashSet<>(sourceList);
27+
}
28+
29+
@Test
30+
public void givenUsingJava10_whenSetConvertedToList_thenCorrect() {
31+
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
32+
final List<Integer> targetList = List.copyOf(sourceSet);
33+
}
34+
35+
@Test
36+
public void givenUsingJava10_whenListConvertedToSet_thenCorrect() {
37+
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
38+
final Set<Integer> targetSet = Set.copyOf(sourceList);
39+
}
40+
41+
@Test
42+
public final void givenUsingGuava_whenSetConvertedToList_thenCorrect() {
43+
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
44+
final List<Integer> targetList = Lists.newArrayList(sourceSet);
45+
}
46+
47+
@Test
48+
public final void givenUsingGuava_whenListConvertedToSet_thenCorrect() {
49+
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
50+
final Set<Integer> targetSet = Sets.newHashSet(sourceList);
51+
}
52+
53+
@Test
54+
public final void givenUsingCommonsCollections_whenListConvertedToSet_thenCorrect() {
55+
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
56+
57+
final Set<Integer> targetSet = new HashSet<>(6);
58+
CollectionUtils.addAll(targetSet, sourceList);
59+
}
60+
61+
@Test
62+
public final void givenUsingCommonsCollections_whenSetConvertedToList_thenCorrect() {
63+
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
64+
65+
final List<Integer> targetList = new ArrayList<>(6);
66+
CollectionUtils.addAll(targetList, sourceSet);
67+
}
68+
}

java-collections-conversions/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ This module contains articles about conversions among Collection types and array
55
### Relevant Articles:
66
- [Converting between an Array and a List in Java](https://www.baeldung.com/convert-array-to-list-and-list-to-array)
77
- [Converting between an Array and a Set in Java](https://www.baeldung.com/convert-array-to-set-and-set-to-array)
8-
- [Converting between a List and a Set in Java](https://www.baeldung.com/convert-list-to-set-and-set-to-list)
98
- [Convert a Map to an Array, List or Set in Java](https://www.baeldung.com/convert-map-values-to-array-list-set)
109
- [Converting a List to String in Java](https://www.baeldung.com/java-list-to-string)
1110
- [How to Convert List to Map in Java](https://www.baeldung.com/java-list-to-map)

java-collections-conversions/src/test/java/com/baeldung/java/collections/JavaCollectionConversionUnitTest.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -101,42 +101,6 @@ public final void givenUsingCommonsCollections_whenSetConvertedToArrayOfPrimitiv
101101
final int[] primitiveTargetArray = ArrayUtils.toPrimitive(targetArray);
102102
}
103103

104-
// Set -> List; List -> Set
105-
106-
public final void givenUsingCoreJava_whenSetConvertedToList_thenCorrect() {
107-
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
108-
final List<Integer> targetList = new ArrayList<>(sourceSet);
109-
}
110-
111-
public final void givenUsingCoreJava_whenListConvertedToSet_thenCorrect() {
112-
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
113-
final Set<Integer> targetSet = new HashSet<>(sourceList);
114-
}
115-
116-
public final void givenUsingGuava_whenSetConvertedToList_thenCorrect() {
117-
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
118-
final List<Integer> targetList = Lists.newArrayList(sourceSet);
119-
}
120-
121-
public final void givenUsingGuava_whenListConvertedToSet_thenCorrect() {
122-
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
123-
final Set<Integer> targetSet = Sets.newHashSet(sourceList);
124-
}
125-
126-
public final void givenUsingCommonsCollections_whenListConvertedToSet_thenCorrect() {
127-
final List<Integer> sourceList = Lists.newArrayList(0, 1, 2, 3, 4, 5);
128-
129-
final Set<Integer> targetSet = new HashSet<>(6);
130-
CollectionUtils.addAll(targetSet, sourceList);
131-
}
132-
133-
public final void givenUsingCommonsCollections_whenSetConvertedToList_thenCorrect() {
134-
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
135-
136-
final List<Integer> targetList = new ArrayList<>(6);
137-
CollectionUtils.addAll(targetList, sourceSet);
138-
}
139-
140104
// Map (values) -> Array, List, Set
141105

142106
@Test

0 commit comments

Comments
 (0)