我刚刚开始使用Java8lambdas,并且我注意到我不能在NetBeans集成开发环境中调试它们。如果我试图将断点附加到下面的代码中,我会得到一个变量断点,这肯定不是我想要的:
private EventListener myListener (Event event) ->
{
command1;
command2; // Set Breakpoint here
command3;
};NetBeans将调试器附加到"myListener“变量,但我不能进入EventListener本身,因此我看不到它内部发生了什么。
是否缺少调试信息,这是NetBeans中缺少的特性,还是根本不可能调试Lambdas?
发布于 2014-03-20 20:00:24
它在Eclipse中适用于我。例如:
public class Foo {
private static final Runnable r1 = () -> {
System.out.println("r1a");
System.out.println("r1b");
};
public static void main(String[] args) {
Runnable r2 = () -> {
System.out.println("r2a");
System.out.println("r2b");
};
r1.run();
r2.run();
}
}我可以在r1和r2中的单独行中添加断点,并通过单步执行等方式适当地命中断点。
如果我只在run()调用上设置断点,我也可以单步执行相关的lambda表达式。
因此,听起来所有的调试信息至少都是可用的。
编辑:显然上面的方法在Netbeans中也可以工作--我建议你尝试一下,看看你能不能让它正常工作。
发布于 2014-03-20 20:11:31
在Netbeans 8 Release中使用以下示例代码
private void init() {
List<Map<Integer, String>> mapList = new ArrayList<>();
Map<Integer, String> map1 = new HashMap<>();
map1.put(1, "String1");
mapList.add(map1);
Map<Integer, String> map2 = new HashMap<>();
map2.put(2, "String2");
mapList.add(map2);
Map<Integer, String> map3 = new HashMap<>();
map3.put(1, "String3");
mapList.add(map3);
Map<Integer, String> map4 = new HashMap<>();
map4.put(2, "String4");
mapList.add(map4);
Map<Integer, List<String>> response = mapList.stream()
.flatMap(map -> map.entrySet().stream())
.collect(
Collectors.groupingBy(
Map.Entry::getKey,
Collectors.mapping(
Map.Entry::getValue,
Collectors.toList()
)
)
);
response.forEach((i, l) -> {
System.out.println("Integer: " + i + " / List: " + l);
});
}我可以在System.out.println("Integer: " + i + " / List: " + l);上设置断点并很好地检查值(i,l)。
所以我倾向于说它是有效的。
发布于 2014-03-20 20:10:37
在IntelliJ中,它也适用于我:
Stream.generate(() -> {
return random.nextInt();
}).limit(10).count();只有在提供像count()这样的终端操作时,我才能调试到返回的random.nextInt()中。
https://stackoverflow.com/questions/22532051
复制相似问题