What's the best & concise way to change the code given below in Java 8:
static int[] breakingRecords(int[] scores) {
int lowest = 0, highest = 0, countlow = 0, counthigh = 0;
for (int i = 0; i < scores.length; i++) {
if (i == 0) {
lowest = scores[0];
highest = scores[0];
} else {
if (scores[i] < lowest) {
lowest = scores[i];
countlow++;
} else if (scores[i] > highest) {
highest = scores[i];
counthigh++;
}
}
}
int rc[] = {counthigh, countlow};
return rc;
}
Note: I know that I can use:
IntStream.range(0, 10).forEach(
i -> {
if (i == 0) {
...
}
But to do so, I need to declare all the variables as AtomicInteger which makes the code verbose & hard to read.
There must be a better way than using AtomicIntegers. Is there?