Skip to content

Commit 8b4cf42

Browse files
authored
[JAVA-46873] Moving some article links on Github - core-java-collections-conversions (#18591)
1 parent 3bdb2e5 commit 8b4cf42

File tree

23 files changed

+173
-168
lines changed

23 files changed

+173
-168
lines changed

core-java-modules/core-java-collections-conversions-2/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
<version>${modelmapper.version}</version>
2626
</dependency>
2727
<dependency>
28-
<groupId>io.vavr</groupId>
29-
<artifactId>vavr</artifactId>
30-
<version>${vavr.version}</version>
28+
<groupId>org.apache.commons</groupId>
29+
<artifactId>commons-collections4</artifactId>
30+
<version>${commons-collections4.version}</version>
3131
</dependency>
3232
</dependencies>
3333

@@ -42,7 +42,7 @@
4242
</build>
4343

4444
<properties>
45-
<vavr.version>0.10.3</vavr.version>
4645
<modelmapper.version>3.2.0</modelmapper.version>
4746
</properties>
47+
4848
</project>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.baeldung.hashmaptoarraylist;
22

3+
import com.google.common.collect.Lists;
4+
import com.google.common.collect.Maps;
5+
import com.google.common.collect.Maps.EntryTransformer;
6+
37
import java.util.ArrayList;
48
import java.util.HashMap;
59
import java.util.Map;
610
import java.util.stream.Collectors;
711

8-
import com.google.common.collect.Lists;
9-
import com.google.common.collect.Maps;
10-
import com.google.common.collect.Maps.EntryTransformer;
11-
1212
public class HashMapToArrayListConverterUtils {
1313

1414
static ArrayList<String> convertUsingConstructor(HashMap<Integer, String> hashMap) {
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.baeldung.convertcollectiontoarraylist;
22

3-
import java.util.ArrayList;
4-
import java.util.Collection;
5-
import java.util.Comparator;
6-
import java.util.HashSet;
7-
import java.util.Iterator;
8-
import static java.util.stream.Collectors.toCollection;
93
import org.junit.BeforeClass;
104
import org.junit.Test;
5+
6+
import java.util.*;
7+
8+
import static java.util.stream.Collectors.toCollection;
119
import static org.junit.Assert.*;
1210

1311
/**
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.baeldung.convertiteratortolist;
22

3-
import static org.hamcrest.MatcherAssert.assertThat;
4-
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
5-
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
3+
import com.google.common.collect.ImmutableList;
4+
import com.google.common.collect.Lists;
5+
import org.apache.commons.collections4.IteratorUtils;
6+
import org.junit.Before;
7+
import org.junit.Test;
68

79
import java.util.ArrayList;
810
import java.util.Arrays;
@@ -11,12 +13,9 @@
1113
import java.util.stream.Collectors;
1214
import java.util.stream.StreamSupport;
1315

14-
import org.apache.commons.collections4.IteratorUtils;
15-
import org.junit.Before;
16-
import org.junit.Test;
17-
18-
import com.google.common.collect.ImmutableList;
19-
import com.google.common.collect.Lists;
16+
import static org.hamcrest.MatcherAssert.assertThat;
17+
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
18+
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
2019

2120
public class ConvertIteratorToListServiceUnitTest {
2221

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.baeldung.hashmaptoarraylist;
22

3-
import static org.hamcrest.MatcherAssert.assertThat;
4-
import static org.hamcrest.Matchers.containsInAnyOrder;
3+
import org.junit.Before;
4+
import org.junit.Test;
55

66
import java.util.ArrayList;
77
import java.util.HashMap;
88

9-
import org.junit.Before;
10-
import org.junit.Test;
9+
import static org.hamcrest.MatcherAssert.assertThat;
10+
import static org.hamcrest.Matchers.containsInAnyOrder;
1111

1212
public class HashMapToArrayListConverterUtilsUnitTest {
1313

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.java.collections;
2+
3+
import com.google.common.collect.Sets;
4+
import com.google.common.primitives.Ints;
5+
import org.apache.commons.collections4.CollectionUtils;
6+
import org.apache.commons.lang3.ArrayUtils;
7+
import org.junit.Test;
8+
9+
import java.util.*;
10+
11+
@SuppressWarnings("unused")
12+
public class JavaCollectionConversionUnitTest {
13+
14+
15+
@Test
16+
public final void givenUsingCoreJavaV1_whenArrayConvertedToSet_thenCorrect() {
17+
final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
18+
final Set<Integer> targetSet = new HashSet<Integer>(Arrays.asList(sourceArray));
19+
}
20+
21+
@Test
22+
public final void givenUsingCoreJavaV2_whenArrayConvertedToSet_thenCorrect() {
23+
final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
24+
final Set<Integer> targetSet = new HashSet<Integer>();
25+
Collections.addAll(targetSet, sourceArray);
26+
}
27+
28+
@Test
29+
public final void givenUsingCoreJava_whenSetConvertedToArray_thenCorrect() {
30+
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
31+
final Integer[] targetArray = sourceSet.toArray(new Integer[0]);
32+
}
33+
34+
@Test
35+
public final void givenUsingGuava_whenArrayConvertedToSet_thenCorrect() {
36+
final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
37+
final Set<Integer> targetSet = Sets.newHashSet(sourceArray);
38+
}
39+
40+
@Test
41+
public final void givenUsingGuava_whenSetConvertedToArray_thenCorrect() {
42+
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
43+
final int[] targetArray = Ints.toArray(sourceSet);
44+
}
45+
46+
@Test
47+
public final void givenUsingCommonsCollections_whenArrayConvertedToSet_thenCorrect() {
48+
final Integer[] sourceArray = { 0, 1, 2, 3, 4, 5 };
49+
final Set<Integer> targetSet = new HashSet<>(6);
50+
CollectionUtils.addAll(targetSet, sourceArray);
51+
}
52+
53+
@Test
54+
public final void givenUsingCommonsCollections_whenSetConvertedToArrayOfPrimitives_thenCorrect() {
55+
final Set<Integer> sourceSet = Sets.newHashSet(0, 1, 2, 3, 4, 5);
56+
final Integer[] targetArray = sourceSet.toArray(new Integer[0]);
57+
final int[] primitiveTargetArray = ArrayUtils.toPrimitive(targetArray);
58+
}
59+
60+
}

core-java-modules/core-java-collections-conversions-3/pom.xml

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,6 @@
2121
</dependency>
2222
</dependencies>
2323

24-
<build>
25-
<finalName>core-java-collections-conversions-3</finalName>
26-
<plugins>
27-
<plugin>
28-
<groupId>org.apache.maven.plugins</groupId>
29-
<artifactId>maven-compiler-plugin</artifactId>
30-
<configuration>
31-
<source>9</source>
32-
<target>9</target>
33-
</configuration>
34-
</plugin>
35-
</plugins>
36-
<resources>
37-
<resource>
38-
<directory>src/main/resources</directory>
39-
<filtering>true</filtering>
40-
</resource>
41-
</resources>
42-
</build>
43-
4424
<properties>
4525
<apache.commons.version>3.14.0</apache.commons.version>
4626
</properties>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package optionaltoarraylist;
1+
package com.baeldung.optionaltoarraylist;
22

33
import java.util.ArrayList;
44
import java.util.List;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.java.collections;
2+
3+
import com.google.common.collect.Lists;
4+
import org.junit.Test;
5+
6+
import java.util.*;
7+
8+
@SuppressWarnings("unused")
9+
public class JavaCollectionConversionUnitTest {
10+
11+
@Test
12+
public final void givenUsingCoreJava_whenMapValuesConvertedToArray_thenCorrect() {
13+
final Map<Integer, String> sourceMap = createMap();
14+
15+
final Collection<String> values = sourceMap.values();
16+
final String[] targetArray = values.toArray(new String[0]);
17+
}
18+
19+
@Test
20+
public final void givenUsingCoreJava_whenMapValuesConvertedToList_thenCorrect() {
21+
final Map<Integer, String> sourceMap = createMap();
22+
23+
final List<String> targetList = new ArrayList<>(sourceMap.values());
24+
}
25+
26+
@Test
27+
public final void givenUsingGuava_whenMapValuesConvertedToList_thenCorrect() {
28+
final Map<Integer, String> sourceMap = createMap();
29+
30+
final List<String> targetList = Lists.newArrayList(sourceMap.values());
31+
}
32+
33+
@Test
34+
public final void givenUsingCoreJava_whenMapValuesConvertedToSet_thenCorrect() {
35+
final Map<Integer, String> sourceMap = createMap();
36+
37+
final Set<String> targetSet = new HashSet<>(sourceMap.values());
38+
}
39+
40+
// UTIL
41+
42+
private final Map<Integer, String> createMap() {
43+
final Map<Integer, String> sourceMap = new HashMap<>(3);
44+
sourceMap.put(0, "zero");
45+
sourceMap.put(1, "one");
46+
sourceMap.put(2, "two");
47+
return sourceMap;
48+
}
49+
50+
}

0 commit comments

Comments
 (0)