我有一個方法使用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