【Java必修課】各種集合類的合並(數組、List、Set、Map)


1 介紹

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

file

2 第三方庫

引入十分常用的優秀的第三方庫GuavaApache 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<>();

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();

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, Integer> map1 = ImmutableMap.of("One", 1, "Two", 2);
Map<String, Integer> map2 = ImmutableMap.of("Three", 3);
Map<String, Integer> expected = ImmutableMap.of("One", 1, "Two", 2, "Three", 3);
Map<String, Integer> result = Maps.newHashMap();

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, Integer>builder()
  .putAll(map1)
  .putAll(map2)
  .build();
assertEquals(expected, result);

6.3 Apache Commons

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

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

7 總結

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


歡迎關注公眾號<南瓜慢說>,將持續為你更新...

歡迎多多交流。

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM