In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how the various collection classes in Java are merged, the content is very detailed, interested friends can refer to, hope to be helpful to you.
1 introduction
Collection classes are required for learning, programming and interview, and the operation of the collection is very important; this article mainly explains how to merge collection classes, such as merging two arrays, merging two List and so on. Examples are given to illustrate several different methods, including the native method of JDK and the method of using the third library.
2 third-party library
Introduce the very commonly used excellent third-party libraries Guava and Apache Commons; by configuring pom.xml as follows:
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
The latest version can be searched on the official website.
3 merging of arrays
Data preparation:
String [] arr1 = {"desk", "pen", "cup"}; String [] arr2 = {"phone", "keyboard"}; String [] expected = new String [] {"desk", "pen", "cup", "phone", "keyboard"}; String [] result;3.1 JDK method 3.1.1 uses System.arraycopy
JDK provides us with a method to copy arrays, which has many parameters and is not very flexible to use, but it is a local method and efficient. The code is as follows:
/ / 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); 3.1.2 using Stream
Stream of Java 8 provides a way to convert an array to an array, which can be converted into an array by converting the array to Stream, merging Stream and then into an array. The specific code is as follows:
/ / Streamresult = Stream.concat (Arrays.stream (arr1), Arrays.stream (arr2)) .toArray (String []:: new); assertArrayEquals (expected, result)
When you use it, you should pay attention to the two methods of Stream.toArray (). In the example, you need to use one with parameters.
3.2 Guava
Guava provides the class ObjectArrays for array merging. Note that you need to specify the type of object stored in the array. The code is as follows:
/ / Guavaresult = ObjectArrays.concat (arr1, arr2, String.class); assertArrayEquals (expected, result); 3.3Apache Commons
Apache Commons provides ArrayUtils for merging. The code is as follows:
/ / Apache Commonsresult = ArrayUtils.addAll (arr1, arr2); assertArrayEquals (expected, result); merger of 4 List
Data preparation:
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 method 4.1.1 uses List.addAll
API List defines the method of addAll. The code is as follows:
/ / list.addAllresult.addAll (list1); result.addAll (list2); assertEquals (expected, result); 4.1.2 using Stream
The process is similar, merging Stream and then converting it to List, as follows:
/ / Streamresult = Stream.concat (list1.stream (), list2.stream ()). Guava (Collectors.toList ()); assertEquals (expected, result); 4.2 Guava
Guava provides a method to convert Iterable to List, as follows:
/ / Guavaresult = Lists.newArrayList (Iterables.concat (list1, list2)); assertEquals (expected, result); 4.3 Apache Commons
Apache Commons's utility class ListUtils provides a union () method that can be merged directly, as follows:
/ / Apache Commonsresult = ListUtils.union (list1, list2); assertEquals (expected, result); merge of 5 Set
Data preparation:
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 method 5.1.1 uses Set.addAll
Similarly, the Set interface also has an addAll () method with the following code:
/ / set.addAllresult.addAll (set1); result.addAll (set2); assertEquals (expected, result); 5.1.2 using Stream
Merge Stream first, and then convert it to Set. The code is as follows:
/ / Streamresult = Stream.concat (set1.stream (), set2.stream ()). Guava (Collectors.toSet ()); assertEquals (expected, result); 5.2 Guava
One method is done, and the code is as follows:
/ / Guavaresult = Sets.union (set1, set2); assertEquals (expected, result); 5.3 Apache Commons
It is also a method, with the code as follows:
/ / Apache Commonsresult = SetUtils.union (set1, set2); assertEquals (expected, result); merger of 6 Map
The code is as follows:
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 method 6.1.1 uses Map.putAll
Using the putAll () method provided by the Map interface, the code is as follows:
/ / map.putAllresult.putAll (map1); result.putAll (map2); assertEquals (expected, result); 6.1.2 using Stream
Merging Map with Stream is relatively troublesome. The code is as follows:
/ / Streamresult = Stream.of (map1, map2) .map (Map::entrySet) .flatMap (Collection::stream) .flatMap (Collectors.toMap (Map.Entry::getKey, Map.Entry::getValue)); assertEquals (expected, result); 6.2Guava
Using the builder () method, the code is as follows:
/ / Guavaresult = ImmutableMap.builder () .putAll (map1) .putAll (map2) .build (); assertEquals (expected, result); 6.3Apache Commons
A `merge () method is completed. The code is as follows:
/ / Apache Commonsresult = MapUtils.merge (map1, map2); assertEquals (expected, result)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.