Skip to content

Commit a141043

Browse files
ramansahasimaibin
authored andcommitted
BAEL-2181 complete commit (eugenp#5294)
1 parent f581e90 commit a141043

File tree

9 files changed

+388
-0
lines changed

9 files changed

+388
-0
lines changed

core-java-collections/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
<artifactId>jmh-generator-annprocess</artifactId>
6464
<version>${openjdk.jmh.version}</version>
6565
</dependency>
66+
<dependency>
67+
<groupId>org.apache.commons</groupId>
68+
<artifactId>commons-exec</artifactId>
69+
<version>1.3</version>
70+
</dependency>
71+
6672
</dependencies>
6773

6874

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.combiningcollections;
2+
3+
import java.util.Arrays;
4+
import java.util.stream.Stream;
5+
6+
import org.apache.commons.lang3.ArrayUtils;
7+
8+
import com.google.common.collect.ObjectArrays;
9+
10+
public class CombiningArrays {
11+
12+
public static Object[] usingNativeJava(Object[] first, Object[] second) {
13+
Object[] combined = new Object[first.length + second.length];
14+
System.arraycopy(first, 0, combined, 0, first.length);
15+
System.arraycopy(second, 0, combined, first.length, second.length);
16+
return combined;
17+
}
18+
19+
public static Object[] usingJava8ObjectStream(Object[] first, Object[] second) {
20+
Object[] combined = Stream.concat(Arrays.stream(first), Arrays.stream(second)).toArray();
21+
return combined;
22+
}
23+
24+
public static Object[] usingJava8FlatMaps(Object[] first, Object[] second) {
25+
Object[] combined = Stream.of(first, second).flatMap(Stream::of).toArray(String[]::new);
26+
return combined;
27+
}
28+
29+
public static Object[] usingApacheCommons(Object[] first, Object[] second) {
30+
Object[] combined = ArrayUtils.addAll(first, second);
31+
return combined;
32+
}
33+
34+
public static Object[] usingGuava(Object[] first, Object[] second) {
35+
Object [] combined = ObjectArrays.concat(first, second, Object.class);
36+
return combined;
37+
}
38+
39+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.combiningcollections;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.Stream;
8+
9+
import org.apache.commons.collections4.ListUtils;
10+
11+
import com.google.common.collect.Iterables;
12+
import com.google.common.collect.Lists;
13+
14+
public class CombiningLists {
15+
16+
public static List<Object> usingNativeJava(List<Object> first, List<Object> second) {
17+
List<Object> combined = new ArrayList<>();
18+
combined.addAll(first);
19+
combined.addAll(second);
20+
return combined;
21+
}
22+
23+
public static List<Object> usingJava8ObjectStream(List<Object> first, List<Object> second) {
24+
List<Object> combined = Stream.concat(first.stream(), second.stream()).collect(Collectors.toList());
25+
return combined;
26+
}
27+
28+
public static List<Object> usingJava8FlatMaps(List<Object> first, List<Object> second) {
29+
List<Object> combined = Stream.of(first, second).flatMap(Collection::stream).collect(Collectors.toList());
30+
return combined;
31+
}
32+
33+
public static List<Object> usingApacheCommons(List<Object> first, List<Object> second) {
34+
List<Object> combined = ListUtils.union(first, second);
35+
return combined;
36+
}
37+
38+
public static List<Object> usingGuava(List<Object> first, List<Object> second) {
39+
Iterable<Object> combinedIterables = Iterables.unmodifiableIterable(
40+
Iterables.concat(first, second));
41+
42+
List<Object> combined = Lists.newArrayList(combinedIterables);
43+
return combined;
44+
}
45+
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.combiningcollections;
2+
3+
import java.util.Collection;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.Stream;
8+
9+
import org.apache.commons.exec.util.MapUtils;
10+
11+
import com.google.common.collect.ImmutableMap;
12+
13+
public class CombiningMaps {
14+
15+
public static Map<String, String> usingPlainJava(Map<String, String> first, Map<String, String> second) {
16+
Map<String, String> combined = new HashMap<>();
17+
combined.putAll(first);
18+
combined.putAll(second);
19+
return combined;
20+
}
21+
22+
public static Map<String, String> usingJava8ForEach(Map<String, String> first, Map<String, String> second) {
23+
second.forEach((key, value) -> first.merge(key, value, String::concat));
24+
return first;
25+
}
26+
27+
public static Map<String, String> usingJava8FlatMaps(Map<String, String> first, Map<String, String> second) {
28+
Map<String, String> combined = Stream.of(first, second).map(Map::entrySet).flatMap(Collection::stream)
29+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, String::concat));
30+
return combined;
31+
32+
}
33+
34+
public static Map<String, String> usingApacheCommons(Map<String, String> first, Map<String, String> second) {
35+
Map<String, String> combined = MapUtils.merge(first, second);
36+
return combined;
37+
}
38+
39+
public static Map<String, String> usingGuava(Map<String, String> first, Map<String, String> second) {
40+
Map<String, String> combined = ImmutableMap.<String, String>builder()
41+
.putAll(first)
42+
.putAll(second)
43+
.build();
44+
return combined;
45+
}
46+
47+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.combiningcollections;
2+
3+
import java.util.Collection;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.Stream;
8+
9+
import org.apache.commons.collections4.SetUtils;
10+
11+
import com.google.common.collect.Sets;
12+
13+
public class CombiningSets {
14+
15+
public static Set<Object> usingNativeJava(Set<Object> first, Set<Object> second) {
16+
Set<Object> combined = new HashSet<>();
17+
combined.addAll(first);
18+
combined.addAll(second);
19+
return combined;
20+
}
21+
22+
public static Set<Object> usingJava8ObjectStream(Set<Object> first, Set<Object> second) {
23+
Set<Object> combined = Stream.concat(first.stream(), second.stream()).collect(Collectors.toSet());
24+
return combined;
25+
}
26+
27+
public static Set<Object> usingJava8FlatMaps(Set<Object> first, Set<Object> second) {
28+
Set<Object> combined = Stream.of(first, second).flatMap(Collection::stream).collect(Collectors.toSet());
29+
return combined;
30+
}
31+
32+
public static Set<Object> usingApacheCommons(Set<Object> first, Set<Object> second) {
33+
Set<Object> combined = SetUtils.union(first, second);
34+
return combined;
35+
}
36+
37+
public static Set<Object> usingGuava(Set<Object> first, Set<Object> second) {
38+
Set<Object> combined = Sets.union(first, second);
39+
return combined;
40+
}
41+
42+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.combiningcollections;
2+
3+
import static org.junit.Assert.*;
4+
import org.junit.Test;
5+
6+
public class CombiningArraysUnitTest {
7+
private static final String first[] = {
8+
"One",
9+
"Two",
10+
"Three"
11+
};
12+
13+
private static final String second[] = {
14+
"Four",
15+
"Five",
16+
"Six"
17+
};
18+
19+
private static final String expected[] = {
20+
"One",
21+
"Two",
22+
"Three",
23+
"Four",
24+
"Five",
25+
"Six"
26+
};
27+
28+
@Test
29+
public void givenTwoArrays_whenUsingNativeJava_thenArraysCombined() {
30+
assertArrayEquals(expected, CombiningArrays.usingNativeJava(first, second));
31+
}
32+
33+
@Test
34+
public void givenTwoArrays_whenUsingObjectStreams_thenArraysCombined() {
35+
assertArrayEquals(expected, CombiningArrays.usingJava8ObjectStream(first, second));
36+
}
37+
38+
@Test
39+
public void givenTwoArrays_whenUsingFlatMaps_thenArraysCombined() {
40+
assertArrayEquals(expected, CombiningArrays.usingJava8FlatMaps(first, second));
41+
}
42+
43+
@Test
44+
public void givenTwoArrays_whenUsingApacheCommons_thenArraysCombined() {
45+
assertArrayEquals(expected, CombiningArrays.usingApacheCommons(first, second));
46+
}
47+
48+
@Test
49+
public void givenTwoArrays_whenUsingGuava_thenArraysCombined() {
50+
assertArrayEquals(expected, CombiningArrays.usingGuava(first, second));
51+
}
52+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.combiningcollections;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import org.junit.Test;
10+
11+
public class CombiningListsUnitTest {
12+
private static final List<Object> first = Arrays.asList(new Object[]{
13+
"One",
14+
"Two",
15+
"Three"
16+
});
17+
18+
private static final List<Object> second = Arrays.asList(new Object[]{
19+
"Four",
20+
"Five",
21+
"Six"
22+
});
23+
24+
private static final List<Object> expected = Arrays.asList(new Object[]{
25+
"One",
26+
"Two",
27+
"Three",
28+
"Four",
29+
"Five",
30+
"Six"
31+
});
32+
33+
@Test
34+
public void givenTwoLists_whenUsingNativeJava_thenArraysCombined() {
35+
assertThat(CombiningLists.usingNativeJava(first, second), is(expected));
36+
}
37+
38+
@Test
39+
public void givenTwoLists_whenUsingObjectStreams_thenArraysCombined() {
40+
assertThat(CombiningLists.usingJava8ObjectStream(first, second), is(expected));
41+
}
42+
43+
@Test
44+
public void givenTwoLists_whenUsingFlatMaps_thenArraysCombined() {
45+
assertThat(CombiningLists.usingJava8FlatMaps(first, second), is(expected));
46+
}
47+
48+
@Test
49+
public void givenTwoLists_whenUsingApacheCommons_thenArraysCombined() {
50+
assertThat(CombiningLists.usingApacheCommons(first, second), is(expected));
51+
}
52+
53+
@Test
54+
public void givenTwoLists_whenUsingGuava_thenArraysCombined() {
55+
assertThat(CombiningLists.usingGuava(first, second), is(expected));
56+
}
57+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.combiningcollections;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
import org.junit.Test;
10+
11+
public class CombiningMapsUnitTest {
12+
private static final Map<String, String> first = new HashMap<>();
13+
private static final Map<String, String> second = new HashMap<>();
14+
private static Map<String, String> expected = new HashMap<>();
15+
16+
static {
17+
first.put("one", "first String");
18+
first.put("two", "second String");
19+
20+
second.put("three", "third String");
21+
second.put("four", "fourth String");
22+
23+
expected.put("one", "first String");
24+
expected.put("two", "second String");
25+
expected.put("three", "third String");
26+
expected.put("four", "fourth String");
27+
}
28+
29+
@Test
30+
public void givenTwoMaps_whenUsingNativeJava_thenMapsCombined() {
31+
assertThat(CombiningMaps.usingPlainJava(first, second), is(expected));
32+
}
33+
34+
35+
@Test
36+
public void givenTwoMaps_whenUsingForEach_thenMapsCombined() {
37+
assertThat(CombiningMaps.usingJava8ForEach(first, second), is(expected));
38+
}
39+
40+
@Test
41+
public void givenTwoMaps_whenUsingFlatMaps_thenMapsCombined() {
42+
assertThat(CombiningMaps.usingJava8FlatMaps(first, second), is(expected));
43+
}
44+
45+
@Test
46+
public void givenTwoMaps_whenUsingApacheCommons_thenMapsCombined() {
47+
assertThat(CombiningMaps.usingApacheCommons(first, second), is(expected));
48+
}
49+
50+
@Test
51+
public void givenTwoMaps_whenUsingGuava_thenMapsCombined() {
52+
assertThat(CombiningMaps.usingGuava(first, second), is(expected));
53+
}
54+
}

0 commit comments

Comments
 (0)