[英]What is the equivalent method in Java to map in haskell or select in C#?
Is there any built-in function in Java similar either to the map function in Haskell or Schema or to the Select extensor method in C#? Java中是否有类似于Haskell或Schema中的map函数或C#中的Select extensor方法的内置函数? I just want to write this kind of code in a shorter way:
我只是想以更短的方式编写这种代码:
List<String> names = new ArrayList<String>();
foreach (User element : getUsers()) {
names.add(element.getName());
};
return names;
a shorter way (in C#) 更短的方式(在C#中)
List<String> names = getUsers().Select(x => x.getName()).ToList();
I also need a way of pass function as parameter in a "friendly" way. 我还需要一种以“友好”的方式传递函数作为参数的方法。
In Java 8, this would look something like 在Java 8中,这看起来像
import static java.util.stream.Collectors.toList;
List<String> names = getUsers().stream().map(User::getName).collect(toList());
在Java 8中使用流,您可以执行以下操作:
List<String> names = getUsers().stream().map(User::getName).collect(Collectors.toList())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.