博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java各种集合类的合并(数组、List、Set、Map)
阅读量:4092 次
发布时间:2019-05-25

本文共 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;
1.1 JDK方法
  1. 使用System.arraycopy
    JDK为我们提供了一个复制数组的方法,这个方法参数较多,使用不是很灵活,但它是一个本地方法,效率高。代码如下:
//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);
  1. 使用Stream
    Java 8的Stream提供了转化成数组的方法,可以通过将数组转化成Stream,合并Stream后再转化为数组,具体代码如下:
//Streamresult = Stream.concat(Arrays.stream(arr1), Arrays.stream(arr2)) .toArray(String[]::new);assertArrayEquals(expected, result);

使用的时候要注意Stream.toArray()的两个方法,例子中需要使用带参数

1.2 Guava

Guava提供了类ObjectArrays进行数组合并,注意需要指定数组存储的对象的类型,代码如下:

//Guavaresult = ObjectArrays.concat(arr1, arr2, String.class);assertArrayEquals(expected, result);
1.3 Apache Commons

Apache Commons提供了ArrayUtils进行合并,代码如下:

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

二、List的合并

数据准备:

List
list1 = asList("desk", "pen", "cup");List
list2 = asList("phone", "keyboard");List
expected = asList("desk", "pen", "cup", "phone", "keyboard");List
result = new ArrayList<>();
2.1 JDK方法
  1. 使用List.addAll
    List接口定义了addAll的方法,代码如下:
//list.addAllresult.addAll(list1);result.addAll(list2);assertEquals(expected, result);
  1. 使用Stream
    过程大体相似,合并Stream,然后转化为List,代码如下:
//Streamresult = Stream.concat(list1.stream(), list2.stream())  .collect(Collectors.toList());assertEquals(expected, result);
2.2 Guava

Guava提供了将Iterable转化为List的方法,代码如下:

//Guavaresult = Lists.newArrayList(Iterables.concat(list1, list2));assertEquals(expected, result);
2.3 Apache Commons

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

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

三、Set的合并

数据准备:

Set
set1 = 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();
3.1 JDK方法
  1. 使用Set.addAll
    同样,Set接口也有addAll()方法,代码如下:
//set.addAllresult.addAll(set1);result.addAll(set2);assertEquals(expected, result);
  1. 使用Stream
    先合并Stream,再转化成Set,代码如下:
//Streamresult = Stream.concat(set1.stream(), set2.stream())  .collect(Collectors.toSet());assertEquals(expected, result);
3.2 Guava

一个方法搞定,代码如下:

//Guavaresult = Sets.union(set1, set2);assertEquals(expected, result);
3.3 Apache Commons

同样是一个方法,代码如下:

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

四、Map的合并

数据准备:

Map
map1 = 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();
4.1 JDK方法
  1. 使用Map.putAll
    使用Map接口提供的putAll()方法,代码如下:
//map.putAllresult.putAll(map1);result.putAll(map2);assertEquals(expected, result);
  1. 使用Stream
    使用Stream进行合并Map相对麻烦一些,代码如下:
//Streamresult = Stream.of(map1, map2)  .map(Map::entrySet)  .flatMap(Collection::stream)  .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));assertEquals(expected, result);
4.2 Guava

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

//Guavaresult = ImmutableMap.
builder() .putAll(map1) .putAll(map2) .build();assertEquals(expected, result);
4.3 Apache Commons

一个merge()方法搞定,代码如下:

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

转载链接:

你可能感兴趣的文章
开局一张图,学一学项目管理神器Maven!
查看>>
Android中的Binder(二)
查看>>
Framework之View的工作原理(一)
查看>>
Web应用架构
查看>>
设计模式之策略模式
查看>>
深究Java中的RMI底层原理
查看>>
用idea创建一个maven web项目
查看>>
Kafka
查看>>
9.1 为我们的角色划分权限
查看>>
维吉尼亚之加解密及破解
查看>>
DES加解密
查看>>
TCP/IP协议三次握手与四次握手流程解析
查看>>
PHP 扩展开发 : 编写一个hello world !
查看>>
inet_ntoa、 inet_aton、inet_addr
查看>>
用模板写单链表
查看>>
用模板写单链表
查看>>
链表各类操作详解
查看>>
C++实现 简单 单链表
查看>>
数据结构之单链表——C++模板类实现
查看>>
Linux的SOCKET编程 简单演示
查看>>