Java 使用函子连接集合
给定 2 个具有相同数量元素的集合,例如 List
。在 JAVA
中,有哪些优雅的方法可以在具有相应索引的集合的每 2 个元素上应用函子?
比如说,一个例子可以是:列表<字符串> = { "苹果", "梨" };
列表<字符串> = { "BANANA", "ORANGE" };
将字符串连接在一起的谓词将产生以下 List
:列表<字符串> = { "APPLEBANANA", "梨橙" };
2 collections are given with the same number of elements, say List<String>
. What are elegant ways in JAVA
to apply a functor on each 2 elements of collections with corresponding indexes?
Say, one example could be:List<String> = { "APPLE", "PEAR" };
List<String> = { "BANANA", "ORANGE" };
A predicate that joins string together will result in the following List<String>
:List<String> = { "APPLEBANANA", "PEARORANGE" };
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
类似于 Apache Commons Collections 中找到的函子,我过去已经创建了二进制等价物。
对于您的情况,可以使用二进制转换器类型对象,它接受两个输入对象并返回一个对象。这是一些传达我的方法的示例代码:
HTH
Akin to the functors found in Apache Commons Collections, I have created binary equivalents in the past.
For your situation, a binary transformer type object, which takes to two input objects and returns a single object, could be used. Here is some sample code that's conveys my approach:
HTH
一个快速驱动程序表明它可以工作。不对所有测试用例负责。 :-)
如果你需要通用的东西,你需要知道它们有一种可以连接的方式
/////////////////// 编辑 - 这是一个基于@Brents的工作示例(上级)示例。感谢他比我更好地阐释了这种模式。
A quick driver of this showed it to work. Not responsible for all test cases. :-)
If you need something generic, you would need to know they ahve a way that they can be joined
///////////////// EDIT - here's a working example based off @Brents (superior) example. Props to him for illustrating this pattern better than I did.
您要求的不是谓词。它正在对压缩在一起的列表进行转换。执行此操作的通用方法是编写一个可迭代拉链,它将两个列表压缩为可迭代的 Pair,然后将转换应用于这对。
我最初以为你是在要求两个集合的交集,它在 Guava 集合中作为 Sets.intersection(Set, Set) 提供。
What you're asking for isn't a predicate. It's doing a transformation on the lists zipped together. The generic way to do this is to write an iterable zipper that will zip the two lists into an iterable of a Pair, and then apply the transformation to the pairs.
I initially thought you were asking for the intersection of two collections, which is supplied in Guava collections as Sets.intersection(Set, Set).
我认为你最好的选择是迭代地进行。我想不出任何核心 Java API 可以做到这一点。
还没有编译/运行它。祝你好运。
I think your best bet here will be to do it iteratively. I can't think of any core Java API that can do it.
Haven't compiled / run it. Good luck.