我有一个方法使用parallelStream来运行检查碰撞的对象的ArrayList,但我不知道如何从forEach()方法内的这种lambda表达式返回一个值。这是不可能的吗?从paralell流中返回一个值? java
public static boolean isColliding(Ship moveingShip){
Rectangle2D testRect = moveShip.getContainer(); //gets the rectangle to test
GameManager.getArrayList().parallelStream().forEach((pS) ->
{if(testRect.intersects(pS.getContainer())) {//return boolean}});
return //true or false depending on whether any collisions were detected
}
我不认为forEach方法有返回类型,所以我有点卡住了。除了还原为连续的forEach循环之外,还有其他方法可以做到这一点吗?使用并行流的目的是希望更快地通过ArrayList。谢谢。
谢谢你的建议! – zephos2014 2014-09-19 06:21:38