我想從某個過濾器的列表中刪除對象,並且存在多個對象。獲取lambda foreach表達式中的索引java 8
list.stream().filter(g->g.getName().equalsIgnoreCase("String")).forEach(result ->{
/* is it possible to get the index of the result here?
.remove(), will iterate through the list again. I don't want that.
*/
list.remove(result);
});
這不是一個真正可行的方法。一個更好的方法是將你不想過濾的元素收集到一個新列表中。 –
是的,有道理。謝謝。但我需要將結果合併到一個對象中,然後刪除以前的對象。我怎樣才能做到這一點?這是我的最終目標 –
@AnkurSehdev如果您需要合併,然後再從列表中刪除,請在您的問題中指定,以便答案可以對您有所幫助。請同時告訴我們這個合併是什麼,等等。 –