我使用 Project Lambdas 的 .map() 方法从 List 对象中提取一些数据。之前,Jdk 1.8 包含 Mapper 接口(interface),但现在,我看到它的 map 方法获取 Function 接口(interface)。我的方法用法如下:
List<Nut> nutList =new ArrayList<Nut>();
我已经向 nutList 添加了一些项目,我使用下面的方法通过 getter 提取字段值。
nutList.stream().map(n->n.getShell())
但是上面的 n 参数表现得像一个对象,它不能访问原始对象的方法。此用法通常使用 Mapper Functional 接口(interface)运行。
最佳答案
在b75下,下面的代码可以正确编译运行:
List<Nut> nutList =new ArrayList<>();
nutList.stream().map(n -> n.getShell()).forEach(System.out::print);
也许您使用的是旧版本?
关于Java 8 Map 和 Reduce 不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14657071/