Java:有地图功能吗?
我需要一个 map 函数。 Java 中已经有类似的东西了吗?
(对于那些想知道的人:我当然知道如何自己实现这个简单的功能......)
I need a map function. Is there something like this in Java already?
(For those who wonder: I of course know how to implement this trivial function myself...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从 Java 8 开始,JDK 中有一些标准选项可以执行此操作:
请参阅
java.util.Collection.stream()
和java.util.stream.Collectors.toList()
。Since Java 8, there are some standard options to do this in JDK:
See
java.util.Collection.stream()
andjava.util.stream.Collectors.toList()
.从 java 6 开始,JDK 中没有函数的概念。
Guava 有一个 Function 界面以及
Collections2.transform(Collection, Function)
方法提供您需要的功能。
示例:
输出:
如今,Java 8 中实际上有了一个地图函数,因此我可能会以更简洁的方式编写代码:
There is no notion of a function in the JDK as of java 6.
Guava has a Function interface though and the
Collections2.transform(Collection<E>, Function<E,E2>)
method provides the functionality you require.
Example:
Output:
These days, with Java 8, there is actually a map function, so I'd probably write the code in a more concise way:
有一个很棒的库,名为 Functional Java,它可以处理许多您希望 Java 具有但它没有的功能。再说一次,还有一种奇妙的语言 Scala,它可以完成 Java 应该做但没有做的所有事情,同时仍然与为 JVM 编写的任何内容兼容。
There is a wonderful library called Functional Java which handles many of the things you'd want Java to have but it doesn't. Then again, there's also this wonderful language Scala which does everything Java should have done but doesn't while still being compatible with anything written for the JVM.
使用番石榴的
Collections2.transform()
时要非常小心。这种方法的最大优点也是它最大的危险:它的惰性。
查看
Lists.transform()
的文档,我相信它也适用于Collections2.transform()
:另外,在 Collections2.transform() 的文档中,他们提到您可以获得实时视图,源列表中的更改会影响转换后的列表。如果开发人员没有意识到它的工作方式,这种行为可能会导致难以跟踪的问题。
如果您想要一个更经典的“地图”,它只会运行一次,那么您最好使用
FluentIterable
,同样来自Guava,它的操作要简单得多。这是谷歌的示例:transform()
这里是地图方法。它使用相同的 Function<> “回调”为Collections.transform()
。不过,您返回的列表是只读的,请使用 copyInto() 来获取读写列表。否则,当 java8 推出 lambda 时,这就会过时。
Be very careful with
Collections2.transform()
from guava.That method's greatest advantage is also its greatest danger: its laziness.
Look at the documentation of
Lists.transform()
, which I believe applies also toCollections2.transform()
:Also in the documentation of
Collections2.transform()
they mention you get a live view, that change in the source list affect the transformed list. This sort of behaviour can lead to difficult-to-track problems if the developer doesn't realize the way it works.If you want a more classical "map", that will run once and once only, then you're better off with
FluentIterable
, also from Guava, which has an operation which is much more simple. Here is the google example for it:transform()
here is the map method. It uses the same Function<> "callbacks" asCollections.transform()
. The list you get back is read-only though, usecopyInto()
to get a read-write list.Otherwise of course when java8 comes out with lambdas, this will be obsolete.
这是另一个您可以使用地图的功能库:http://code.google.com/p/完全懒惰/
This is another functional lib with which you may use map: http://code.google.com/p/totallylazy/
尽管这是一个老问题,我想展示另一个解决方案:
只需使用 java 泛型和 java 8 流定义您自己的操作:
您可以编写如下代码:
Even though it's an old question I'd like to show another solution:
Just define your own operation using java generics and java 8 streams:
Than you can write code like this: