Posts

Showing posts with the label java-8

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...

Java8 enum avoid multiple if else

Java8 enum avoid multiple if else In java 8 is there any option to avoid multiple if else check with enum value and to execute particular operation. I dont like to use some thing like below example ? if enum equals A PRINT A else if enum equals B PRINT B else if enum equlas C PRINT C Is it possible you're looking for switch statements? – Silvio Mayolo Jul 1 at 17:37 You do not need to check the equality if the operation is always the same – Yassin Hajaj Jul 1 at 18:06 2 Answers 2 Define enum with abstract method and provide it's implementation with values. enum MyEnum{ A{ @Override...