java的数组和集合(「Java必修课」各种集合类的合并(数组、List、Set、Map))java基础 / Java数组与集合...

wufei123 发布于 2024-06-16 阅读(8)

1 介绍集合类可谓是学习必知、编程必用、面试必会的,而且集合的操作十分重要;本文主要讲解如何合并集合类,如合并两个数组,合并两个List等通过例子讲解几种不同的方法,有JDK原生的方法,还有使用第三库的方法。

2 第三方库引入十分常用的优秀的第三方库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

最新版本可以去官网搜索查看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.arraycopyJDK为我们提供了一个复制数组的方法,这个方法参数较多,使用不是很灵活,但它是一个本地方法,效率高代码如下://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 使用StreamJava 8的Stream提供了转化成数组的方法,可以通过将数组转化成Stream,合并Stream后再转化为数组,具体代码如下://Stream result = Stream.concat(Arrays.stream(arr1), Arrays.stream(arr2)) .toArray(String[]::new); assertArrayEquals(expected, result);

使用的时候要注意Stream.toArray()的两个方法,例子中需要使用带参数的3.2 GuavaGuava提供了类ObjectArrays进行数组合并,注意需要指定数组存储的对象的类型,代码如下://Guava result = ObjectArrays.concat(arr1, arr2, String.class); assertArrayEquals(expected, result);

3.3 Apache CommonsApache Commons提供了ArrayUtils进行合并,代码如下://Apache Commons result = ArrayUtils.addAll(arr1, arr2); assertArrayEquals(expected, result);

4 List的合并数据准备:List list1 = asList("desk", "pen", "cup"); List list2 = asList("phone", "keyboard"); List expected = asList("desk", "pen", "cup", "phone", "keyboard"); List result = new ArrayList<>();

4.1 JDK方法4.1.1 使用List.addAllList接口定义了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 GuavaGuava提供了将Iterable转化为List的方法,代码如下://Guava result = Lists.newArrayList(Iterables.concat(list1, list2)); assertEquals(expected, result);

4.3 Apache CommonsApache Commons的工具类ListUtils提供了union()方法可以直接合并,代码如下://Apache Commons result = ListUtils.union(list1, list2); assertEquals(expected, result);

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

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

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.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 总结本文分别列举了数组、List、Set和Map的合并的多种方法,虽然代码简单,理解也容易,但这些方法应该熟练掌握可以收藏一下,必要的时间查一查欢迎关注公众号,将持续为你更新...欢迎多多交流。

多读书,多分享;多写作,多整理。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

河南中青旅行社综合资讯 奇遇综合资讯 盛世蓟州综合资讯 综合资讯 游戏百科综合资讯 新闻68455