「Java必修課」各種集合類的合併(數組、List、Set、Map)

1 介紹

集合類可謂是學習必知、編程必用、面試必會的,而且集合的操作十分重要;本文主要講解如何合併集合類,如合併兩個數組,合併兩個List等。通過例子講解幾種不同的方法,有JDK原生的方法,還有使用第三庫的方法。

「Java必修課」各種集合類的合併(數組、List、Set、Map)

2 第三方庫

引入十分常用的優秀的第三方庫Guava和Apache Commons;通過配置pom.xml如下:

<dependency>
<groupid>com.google.guava/<groupid>
<artifactid>guava/<artifactid>
<version>28.1-jre/<version>
/<dependency>
<dependency>
<groupid>org.apache.commons/<groupid>
<artifactid>commons-collections4/<artifactid>
<version>4.4/<version>
/<dependency>
<dependency>
<groupid>org.apache.commons/<groupid>
<artifactid>commons-exec/<artifactid>
<version>1.3/<version>
/<dependency>
<dependency>
<groupid>org.apache.commons/<groupid>
<artifactid>commons-lang3/<artifactid>
<version>3.5/<version>
/<dependency>

最新版本可以去官網搜索查看。

3 數組的合併

數據準備:

String[] arr1 = {"desk", "pen", "cup"};
String[] arr2 = {"phone", "keyboard"};
String[] expected = new String[]{"desk", "pen", "cup", "phone", "keyboard"};
String[] result;

3.1 JDK方法

3.1.1 使用System.arraycopy

JDK為我們提供了一個複製數組的方法,這個方法參數較多,使用不是很靈活,但它是一個本地方法,效率高。代碼如下:

//System.arraycopy
result = 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);

3.1.2 使用Stream

Java 8的Stream提供了轉化成數組的方法,可以通過將數組轉化成Stream,合併Stream後再轉化為數組,具體代碼如下:

//Stream
result = Stream.concat(Arrays.stream(arr1), Arrays.stream(arr2))
.toArray(String[]::new);
assertArrayEquals(expected, result);

使用的時候要注意Stream.toArray()的兩個方法,例子中需要使用帶參數的。

3.2 Guava

Guava提供了類ObjectArrays進行數組合並,注意需要指定數組存儲的對象的類型,代碼如下:

//Guava
result = ObjectArrays.concat(arr1, arr2, String.class);
assertArrayEquals(expected, result);

3.3 Apache Commons

Apache Commons提供了ArrayUtils進行合併,代碼如下:

//Apache Commons
result = ArrayUtils.addAll(arr1, arr2);
assertArrayEquals(expected, result);

4 List的合併

數據準備:

List<string> list1 = asList("desk", "pen", "cup");
List<string> list2 = asList("phone", "keyboard");
List<string> expected = asList("desk", "pen", "cup", "phone", "keyboard");
List<string> result = new ArrayList<>();
/<string>/<string>/<string>/<string>

4.1 JDK方法

4.1.1 使用List.addAll

List接口定義了addAll的方法,代碼如下:

//list.addAll
result.addAll(list1);
result.addAll(list2);
assertEquals(expected, result);

4.1.2 使用Stream

過程大體相似,合併Stream,然後轉化為List,代碼如下:

//Stream
result = Stream.concat(list1.stream(), list2.stream())
.collect(Collectors.toList());
assertEquals(expected, result);

4.2 Guava

Guava提供了將Iterable轉化為List的方法,代碼如下:

//Guava
result = Lists.newArrayList(Iterables.concat(list1, list2));
assertEquals(expected, result);

4.3 Apache Commons

Apache Commons的工具類ListUtils提供了union()方法可以直接合並,代碼如下:

//Apache Commons
result = ListUtils.union(list1, list2);
assertEquals(expected, result);

5 Set的合併

數據準備:

 Set<string> set1 = Sets.newHashSet("desk", "pen", "cup", "phone", "keyboard");
Set<string> set2 = Sets.newHashSet("phone", "keyboard");
Set<string> expected = Sets.newHashSet("desk", "pen", "cup", "phone", "keyboard");
Set<string> result = Sets.newHashSet();
/<string>/<string>/<string>/<string>

5.1 JDK方法

5.1.1 使用Set.addAll

同樣,Set接口也有addAll()方法,代碼如下:

//set.addAll
result.addAll(set1);
result.addAll(set2);
assertEquals(expected, result);

5.1.2 使用Stream

先合併Stream,再轉化成Set,代碼如下:

//Stream
result = Stream.concat(set1.stream(), set2.stream())
.collect(Collectors.toSet());
assertEquals(expected, result);

5.2 Guava

一個方法搞定,代碼如下:

//Guava
result = Sets.union(set1, set2);
assertEquals(expected, result);

5.3 Apache Commons

同樣是一個方法,代碼如下:

//Apache Commons
result = SetUtils.union(set1, set2);
assertEquals(expected, result);

6 Map的合併

代碼如下:

Map<string> map1 = ImmutableMap.of("One", 1, "Two", 2);
Map<string> map2 = ImmutableMap.of("Three", 3);
Map<string> expected = ImmutableMap.of("One", 1, "Two", 2, "Three", 3);
Map<string> result = Maps.newHashMap();
/<string>/<string>/<string>/<string>

6.1 JDK方法

6.1.1 使用Map.putAll

使用Map接口提供的putAll()方法,代碼如下:

//map.putAll
result.putAll(map1);
result.putAll(map2);
assertEquals(expected, result);

6.1.2 使用Stream

使用Stream進行合併Map相對麻煩一些,代碼如下:

//Stream
result = Stream.of(map1, map2)
.map(Map::entrySet)
.flatMap(Collection::stream)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
assertEquals(expected, result);

6.2 Guava

使用builder()方法,代碼如下:

//Guava
result = ImmutableMap.<string>builder()
.putAll(map1)
.putAll(map2)
.build();
assertEquals(expected, result);
/<string>

6.3 Apache Commons

一個`merge()方法搞定,代碼如下:

//Apache Commons
result = MapUtils.merge(map1, map2);
assertEquals(expected, result);

7 總結

本文分別列舉了數組、List、Set和Map的合併的多種方法,雖然代碼簡單,理解也容易,但這些方法應該熟練掌握。可以收藏一下,必要的時間查一查。

歡迎多多交流。

多讀書,多分享;多寫作,多整理。


分享到:


相關文章: