I am trying to iterate over a TreeMap in java using the forEach
loop in Java 8. I want break out of the loop when max == v
but i am getting an error "break cannot be used outside of a loop or a switch". Here's a section of the code.
tm.forEach(
(k,v)->{
if(max == v)
{
System.out.print(k + " "+ v);
break;
}
}
);
So my question is Can we use break statement inside forEach
loop and if yes then what is the mistake i am doing?
I am using eclipse Neon with Java8.