Zipping Collections in Java – 在Java中压缩集合

最后修改: 2017年 7月 10日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this tutorial, we’ll illustrate how to zip two collections into one logical collection.

在本教程中,我们将说明如何将两个集合压缩成一个逻辑集合。

The “zip” operation is slightly different from the standard “concat” or “merge”. While the “concat” or “merge” operations will simply add the new collection at the end of the existing collection, “zip” operation will take an element from each collection and combine them.

zip”操作与标准的 “concat “或 “merge”略有不同。concat “或 “merge “操作将简单地在现有集合的末尾添加新的集合,而”zip”操作将从每个集合中抽取一个元素并将其合并。

The core library does not support “zip” implicitly, but there are certainly third-party libraries which do feature this useful operation.

核心库并不隐含地支持”zip”,但肯定有第三方库具有这一有用的操作。

Consider two lists, one having names of people, other contains their ages.

考虑两个列表,一个有人名,另一个包含他们的年龄。

List<String> names = new ArrayList<>(Arrays.asList("John", "Jane", "Jack", "Dennis"));

List<Integer> ages = new ArrayList<>(Arrays.asList(24, 25, 27));

After zipping, we end up with name-age pairs constructed from corresponding elements from those two collections.

经过压缩,我们最终得到了由这两个集合的相应元素构建的名字-年龄对。

2. Using Java 8 IntStream

2.使用Java 8的IntStream

Using core Java, we could generate indexes using IntStream and then use them to extract corresponding elements from two collections:

使用核心Java,我们可以使用IntStream生成索引,然后使用它们从两个集合中提取相应的元素。

IntStream
  .range(0, Math.min(names.size(), ages.size()))
  .mapToObj(i -> names.get(i) + ":" + ages.get(i))
  // ...

3. Using Guava Streams

3.使用Guava流

Starting version 21, Google Guava provides a zip helper method in the Streams class. This removes all the fuss of creating and mapping indexes and reduces the syntax to inputs and operations:

从21版开始,Google Guava在Streams类中提供了一个zip辅助方法。这消除了创建和映射索引的所有麻烦,并将语法简化为输入和操作。

Streams
  .zip(names.stream(), ages.stream(), (name, age) -> name + ":" + age)
  // ...

4. Using jOOλ (jOOL)

4.使用jOOλ (jOOL)

jOOL also provides some of the fascinating additions over Java 8 Lambda, and with the support of Tuple1 to Tuple16, the zip operation becomes much more interesting:

jOOL还提供了一些比Java 8 Lambda更吸引人的附加功能,随着对Tuple1Tuple16的支持,压缩操作变得更加有趣。

Seq
  .of("John","Jane", "Dennis")
  .zip(Seq.of(24,25,27));

This will produce a result of a Seq containing Tuples of zipped elements:

这将产生一个包含压缩元素的Seq的结果Tuples

(tuple(1, "a"), tuple(2, "b"), tuple(3, "c"))

jOOL’s zip method gives the flexibility to provide custom transformation function:

jOOL的zip方法给出了提供自定义转换功能的灵活性。

Seq
  .of(1, 2, 3)
  .zip(Seq.of("a", "b", "c"), (x, y) -> x + ":" + y);

or if one wishes to zip with index only, he can go with the zipWithIndex method provided by jOOL:

或者如果一个人希望只用索引来压缩,他可以使用zipWithIndex方法,该方法由jOOL:/em>提供。

Seq.of("a", "b", "c").zipWithIndex();

5. Conclusion

5.总结

In this quick tutorial, we had a look at how to perform the zip operation.

在这个快速教程中,我们看了一下如何执行zip操作。

As always, the code examples in the article can be found over on GitHub.

一如既往,文章中的代码示例可以在GitHub上找到over