本文共 4041 字,大约阅读时间需要 13 分钟。
引入十分常用的优秀的第三方库Guava和Apache Commons;通过配置pom.xml如下:
com.google.guava guava 28.1-jre org.apache.commons commons-collections4 4.4 org.apache.commons commons-exec 1.3 org.apache.commons commons-lang3 3.5
最新版本可以去官网搜索查看
数据准备:
String[] arr1 = {"desk", "pen", "cup"};String[] arr2 = {"phone", "keyboard"};String[] expected = new String[]{"desk", "pen", "cup", "phone", "keyboard"};String[] result;
//System.arraycopyresult = new String[arr1.length + arr2.length];System.arraycopy(arr1, 0, result, 0, arr1.length);System.arraycopy(arr2, 0, result, arr1.length, arr2.length);assertArrayEquals(expected, result);
//Streamresult = Stream.concat(Arrays.stream(arr1), Arrays.stream(arr2)) .toArray(String[]::new);assertArrayEquals(expected, result);
使用的时候要注意Stream.toArray()的两个方法,例子中需要使用带参数的
Guava提供了类ObjectArrays进行数组合并,注意需要指定数组存储的对象的类型,代码如下:
//Guavaresult = ObjectArrays.concat(arr1, arr2, String.class);assertArrayEquals(expected, result);
Apache Commons提供了ArrayUtils进行合并,代码如下:
//Apache Commonsresult = ArrayUtils.addAll(arr1, arr2);assertArrayEquals(expected, result);
数据准备:
Listlist1 = asList("desk", "pen", "cup");List list2 = asList("phone", "keyboard");List expected = asList("desk", "pen", "cup", "phone", "keyboard");List result = new ArrayList<>();
//list.addAllresult.addAll(list1);result.addAll(list2);assertEquals(expected, result);
//Streamresult = Stream.concat(list1.stream(), list2.stream()) .collect(Collectors.toList());assertEquals(expected, result);
Guava提供了将Iterable转化为List的方法,代码如下:
//Guavaresult = Lists.newArrayList(Iterables.concat(list1, list2));assertEquals(expected, result);
Apache Commons的工具类ListUtils提供了union()方法可以直接合并,代码如下:
//Apache Commonsresult = ListUtils.union(list1, list2);assertEquals(expected, result);
数据准备:
Setset1 = Sets.newHashSet("desk", "pen", "cup", "phone", "keyboard");Set set2 = Sets.newHashSet("phone", "keyboard");Set expected = Sets.newHashSet("desk", "pen", "cup", "phone", "keyboard");Set result = Sets.newHashSet();
//set.addAllresult.addAll(set1);result.addAll(set2);assertEquals(expected, result);
//Streamresult = Stream.concat(set1.stream(), set2.stream()) .collect(Collectors.toSet());assertEquals(expected, result);
一个方法搞定,代码如下:
//Guavaresult = Sets.union(set1, set2);assertEquals(expected, result);
同样是一个方法,代码如下:
//Apache Commonsresult = SetUtils.union(set1, set2);assertEquals(expected, result);
数据准备:
Mapmap1 = ImmutableMap.of("One", 1, "Two", 2);Map map2 = ImmutableMap.of("Three", 3);Map expected = ImmutableMap.of("One", 1, "Two", 2, "Three", 3);Map result = Maps.newHashMap();
//map.putAllresult.putAll(map1);result.putAll(map2);assertEquals(expected, result);
//Streamresult = Stream.of(map1, map2) .map(Map::entrySet) .flatMap(Collection::stream) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));assertEquals(expected, result);
使用builder()方法,代码如下:
//Guavaresult = ImmutableMap.builder() .putAll(map1) .putAll(map2) .build();assertEquals(expected, result);
一个merge()方法搞定,代码如下:
//Apache Commonsresult = MapUtils.merge(map1, map2);assertEquals(expected, result);
转载链接: