BAEL-2181 complete commit (#5294)
This commit is contained in:
parent
f581e90411
commit
a14104319f
|
@ -63,6 +63,12 @@
|
|||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${openjdk.jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-exec</artifactId>
|
||||
<version>1.3</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.combiningcollections;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import com.google.common.collect.ObjectArrays;
|
||||
|
||||
public class CombiningArrays {
|
||||
|
||||
public static Object[] usingNativeJava(Object[] first, Object[] second) {
|
||||
Object[] combined = new Object[first.length + second.length];
|
||||
System.arraycopy(first, 0, combined, 0, first.length);
|
||||
System.arraycopy(second, 0, combined, first.length, second.length);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Object[] usingJava8ObjectStream(Object[] first, Object[] second) {
|
||||
Object[] combined = Stream.concat(Arrays.stream(first), Arrays.stream(second)).toArray();
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Object[] usingJava8FlatMaps(Object[] first, Object[] second) {
|
||||
Object[] combined = Stream.of(first, second).flatMap(Stream::of).toArray(String[]::new);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Object[] usingApacheCommons(Object[] first, Object[] second) {
|
||||
Object[] combined = ArrayUtils.addAll(first, second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Object[] usingGuava(Object[] first, Object[] second) {
|
||||
Object [] combined = ObjectArrays.concat(first, second, Object.class);
|
||||
return combined;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.combiningcollections;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.collections4.ListUtils;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
public class CombiningLists {
|
||||
|
||||
public static List<Object> usingNativeJava(List<Object> first, List<Object> second) {
|
||||
List<Object> combined = new ArrayList<>();
|
||||
combined.addAll(first);
|
||||
combined.addAll(second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static List<Object> usingJava8ObjectStream(List<Object> first, List<Object> second) {
|
||||
List<Object> combined = Stream.concat(first.stream(), second.stream()).collect(Collectors.toList());
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static List<Object> usingJava8FlatMaps(List<Object> first, List<Object> second) {
|
||||
List<Object> combined = Stream.of(first, second).flatMap(Collection::stream).collect(Collectors.toList());
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static List<Object> usingApacheCommons(List<Object> first, List<Object> second) {
|
||||
List<Object> combined = ListUtils.union(first, second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static List<Object> usingGuava(List<Object> first, List<Object> second) {
|
||||
Iterable<Object> combinedIterables = Iterables.unmodifiableIterable(
|
||||
Iterables.concat(first, second));
|
||||
|
||||
List<Object> combined = Lists.newArrayList(combinedIterables);
|
||||
return combined;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.combiningcollections;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.exec.util.MapUtils;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
public class CombiningMaps {
|
||||
|
||||
public static Map<String, String> usingPlainJava(Map<String, String> first, Map<String, String> second) {
|
||||
Map<String, String> combined = new HashMap<>();
|
||||
combined.putAll(first);
|
||||
combined.putAll(second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Map<String, String> usingJava8ForEach(Map<String, String> first, Map<String, String> second) {
|
||||
second.forEach((key, value) -> first.merge(key, value, String::concat));
|
||||
return first;
|
||||
}
|
||||
|
||||
public static Map<String, String> usingJava8FlatMaps(Map<String, String> first, Map<String, String> second) {
|
||||
Map<String, String> combined = Stream.of(first, second).map(Map::entrySet).flatMap(Collection::stream)
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, String::concat));
|
||||
return combined;
|
||||
|
||||
}
|
||||
|
||||
public static Map<String, String> usingApacheCommons(Map<String, String> first, Map<String, String> second) {
|
||||
Map<String, String> combined = MapUtils.merge(first, second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Map<String, String> usingGuava(Map<String, String> first, Map<String, String> second) {
|
||||
Map<String, String> combined = ImmutableMap.<String, String>builder()
|
||||
.putAll(first)
|
||||
.putAll(second)
|
||||
.build();
|
||||
return combined;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.combiningcollections;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.collections4.SetUtils;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
public class CombiningSets {
|
||||
|
||||
public static Set<Object> usingNativeJava(Set<Object> first, Set<Object> second) {
|
||||
Set<Object> combined = new HashSet<>();
|
||||
combined.addAll(first);
|
||||
combined.addAll(second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Set<Object> usingJava8ObjectStream(Set<Object> first, Set<Object> second) {
|
||||
Set<Object> combined = Stream.concat(first.stream(), second.stream()).collect(Collectors.toSet());
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Set<Object> usingJava8FlatMaps(Set<Object> first, Set<Object> second) {
|
||||
Set<Object> combined = Stream.of(first, second).flatMap(Collection::stream).collect(Collectors.toSet());
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Set<Object> usingApacheCommons(Set<Object> first, Set<Object> second) {
|
||||
Set<Object> combined = SetUtils.union(first, second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
public static Set<Object> usingGuava(Set<Object> first, Set<Object> second) {
|
||||
Set<Object> combined = Sets.union(first, second);
|
||||
return combined;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.combiningcollections;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CombiningArraysUnitTest {
|
||||
private static final String first[] = {
|
||||
"One",
|
||||
"Two",
|
||||
"Three"
|
||||
};
|
||||
|
||||
private static final String second[] = {
|
||||
"Four",
|
||||
"Five",
|
||||
"Six"
|
||||
};
|
||||
|
||||
private static final String expected[] = {
|
||||
"One",
|
||||
"Two",
|
||||
"Three",
|
||||
"Four",
|
||||
"Five",
|
||||
"Six"
|
||||
};
|
||||
|
||||
@Test
|
||||
public void givenTwoArrays_whenUsingNativeJava_thenArraysCombined() {
|
||||
assertArrayEquals(expected, CombiningArrays.usingNativeJava(first, second));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoArrays_whenUsingObjectStreams_thenArraysCombined() {
|
||||
assertArrayEquals(expected, CombiningArrays.usingJava8ObjectStream(first, second));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoArrays_whenUsingFlatMaps_thenArraysCombined() {
|
||||
assertArrayEquals(expected, CombiningArrays.usingJava8FlatMaps(first, second));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoArrays_whenUsingApacheCommons_thenArraysCombined() {
|
||||
assertArrayEquals(expected, CombiningArrays.usingApacheCommons(first, second));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoArrays_whenUsingGuava_thenArraysCombined() {
|
||||
assertArrayEquals(expected, CombiningArrays.usingGuava(first, second));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.combiningcollections;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CombiningListsUnitTest {
|
||||
private static final List<Object> first = Arrays.asList(new Object[]{
|
||||
"One",
|
||||
"Two",
|
||||
"Three"
|
||||
});
|
||||
|
||||
private static final List<Object> second = Arrays.asList(new Object[]{
|
||||
"Four",
|
||||
"Five",
|
||||
"Six"
|
||||
});
|
||||
|
||||
private static final List<Object> expected = Arrays.asList(new Object[]{
|
||||
"One",
|
||||
"Two",
|
||||
"Three",
|
||||
"Four",
|
||||
"Five",
|
||||
"Six"
|
||||
});
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenUsingNativeJava_thenArraysCombined() {
|
||||
assertThat(CombiningLists.usingNativeJava(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenUsingObjectStreams_thenArraysCombined() {
|
||||
assertThat(CombiningLists.usingJava8ObjectStream(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenUsingFlatMaps_thenArraysCombined() {
|
||||
assertThat(CombiningLists.usingJava8FlatMaps(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenUsingApacheCommons_thenArraysCombined() {
|
||||
assertThat(CombiningLists.usingApacheCommons(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoLists_whenUsingGuava_thenArraysCombined() {
|
||||
assertThat(CombiningLists.usingGuava(first, second), is(expected));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.combiningcollections;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CombiningMapsUnitTest {
|
||||
private static final Map<String, String> first = new HashMap<>();
|
||||
private static final Map<String, String> second = new HashMap<>();
|
||||
private static Map<String, String> expected = new HashMap<>();
|
||||
|
||||
static {
|
||||
first.put("one", "first String");
|
||||
first.put("two", "second String");
|
||||
|
||||
second.put("three", "third String");
|
||||
second.put("four", "fourth String");
|
||||
|
||||
expected.put("one", "first String");
|
||||
expected.put("two", "second String");
|
||||
expected.put("three", "third String");
|
||||
expected.put("four", "fourth String");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoMaps_whenUsingNativeJava_thenMapsCombined() {
|
||||
assertThat(CombiningMaps.usingPlainJava(first, second), is(expected));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenTwoMaps_whenUsingForEach_thenMapsCombined() {
|
||||
assertThat(CombiningMaps.usingJava8ForEach(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoMaps_whenUsingFlatMaps_thenMapsCombined() {
|
||||
assertThat(CombiningMaps.usingJava8FlatMaps(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoMaps_whenUsingApacheCommons_thenMapsCombined() {
|
||||
assertThat(CombiningMaps.usingApacheCommons(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoMaps_whenUsingGuava_thenMapsCombined() {
|
||||
assertThat(CombiningMaps.usingGuava(first, second), is(expected));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
|
||||
package com.baeldung.combiningcollections;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CombiningSetsUnitTest {
|
||||
private static final Set<Object> first = new HashSet<Object>(Arrays.asList(new Object[] { "One", "Two", "Three" }));
|
||||
|
||||
private static final Set<Object> second = new HashSet<Object>(Arrays.asList(new Object[] { "Four", "Five", "Six" }));
|
||||
|
||||
private static final Set<Object> expected = new HashSet<Object>(Arrays
|
||||
.asList(new Object[] { "One", "Two", "Three", "Four", "Five", "Six" }));
|
||||
|
||||
@Test
|
||||
public void givenTwoSets_whenUsingNativeJava_thenArraysCombined() {
|
||||
assertThat(CombiningSets.usingNativeJava(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSets_whenUsingObjectStreams_thenArraysCombined() {
|
||||
assertThat(CombiningSets.usingJava8ObjectStream(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSets_whenUsingFlatMaps_thenArraysCombined() {
|
||||
assertThat(CombiningSets.usingJava8FlatMaps(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSets_whenUsingApacheCommons_thenArraysCombined() {
|
||||
assertThat(CombiningSets.usingApacheCommons(first, second), is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoSets_whenUsingGuava_thenArraysCombined() {
|
||||
assertThat(CombiningSets.usingGuava(first, second), is(expected));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue