Posts

Showing posts with the label java-stream

Java Streams for looping twice over same list

Java Streams for looping twice over same list I am new to java 8, and i have a problem understanding streams for some reason. Let's say we have a list of objects List< MyObject >, where MyObject has 2 fields : Long Id, Date insertTime, and i would like to remove elements with same ID and earlier time. With 2 for loops it is something like this : for(MyObject object : myObjects) { for(MyObject tmpObject : myObjects) { if(object.getId() == tmpObject.getId()) { if(object.getInsertDate().after(tmpObject.getInsertDate())) myObjects.remove(tmpObject); else myObjects.remove(object); } } } How would this look when using streams? @Michael my bad. Than i would need another list or array to store the response data. Let's say i have So i need as a result Thank you all. "With 2 for loops it is something like this" I'm afraid it's not, because you can't remove from a list while y...