In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the methods of compressing the two sets". In daily operation, I believe many people have doubts about the methods of compressing the two sets. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "what are the methods of compressing the two sets"! Next, please follow the small series to learn together!
Case study explains what a compressed set is
There are now two collections: names and ages.
List names = new ArrayList(Arrays.asList("Zhang San", "Li Si", "Wang Wu")); List ages = new ArrayList(Arrays.asList(24, 25, 27));
After compression, we traverse the compressed object into the following form:
John 3:24, John 4:25, John 5:27
That is, the key-value pair form of name:age, of course, if our collection is large, we can compress more collections. For example, a person's various information, id:name:age, etc.
OK, know what compression is, the following does not go back to the long story, directly give several forms of compression,
Several ways to compress collections
Java 8 IntStream
List names = new ArrayList(Arrays.asList("Zhang San", "Li Si", "Wang Wu")); List ages = new ArrayList(Arrays.asList(24, 25, 27)); //Method 1: java8 IntStream stream = IntStream .range(0, Math.min(names.size(), ages.size())) .mapToObj(i -> names.get(i) + ":" + ages.get(i)); //Traverse output stream.forEach(System.out::println);
This is a simpler way to take the streams of two collections and convert them into objects. Java 8 can be used directly.
Part 2: Guava Streams
List names = new ArrayList(Arrays.asList("Zhang San", "Li Si", "Wang Wu")); List ages = new ArrayList(Arrays.asList(24, 25, 27)); //Method 2: guava //The first Streams.forEachPair( Stream.of("Zhang San", "Li Si", "Wang Wu"), Stream.of(1, 2,3), (name, age) -> System.out.println(name + ":" + age)); //Stream stream2 = Streams.zip (names.stream (), ages.stream (), (name, age) -> name + ":" + age); stream2.forEach(System.out::println); //third type: Stream3 = Streams.mapWithIndex( Stream.of("a", "b", "c"), (str, index) -> str + ":" + index); stream3.forEach(System.out::println);
The above are several ways to achieve, this big guy gave one, I went to the official website to find several other ways. In addition, when using guava, version 21 or above is required. We can add the following dependencies to the pom file:
com.google.guava guava 30.1-jre
Note that the latest version can be used here. Others may be wrong. I tried version 21 and it went wrong.
Method 2: Using jOOλ (jOOL)
jOOL also provides some interesting new features on Java 8 Lambda, and zip operations are made even more interesting by support for Tuple1 to Tuple16:
//Method 3: jool //Seq s1 = Seq.of("Zhang San","Li Si", "Wang Wu") .zip(Seq.of(24,25,27)); //Seq s2 = Seq.of(1, 2, 3) .zip(Seq.of("a", "b", "c"), (x, y) -> x + ":" + y); //Seq s3 = Seq.of("a", "b", "c").zipWithIndex(); s1.forEach(System.out::println); s2.forEach (System. out::println); s3.forEach (System. out::println);
Seq results in a compressed tuple, which we can see.
(tuple(1, "a"), tuple(2, "b"), tuple(3, "c"))
This method also requires adding dependencies. Add the following dependencies to the pom file:
org.jooq jool-java-8 0.9.14
ok, there are many more, the basic idea is to cut into streams and then merge them. This can also be done directly with Java.
At this point, the study of "what are the methods of compressing two sets" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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: 250
*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.