For example there ara code for finde minimum from restricted amount of elements:
public int min(String s) {
return s.chars().map(this::mapToFactor).min().getAsInt();
}
private int mapToFactor(int ch) {
switch(ch) {
case 'A': return 1;
case 'C': return 2;
case 'G': return 3;
case 'T': return 4;
default: return Integer.MAX_VALUE;
}
}
Totaly exist only 5 number : 1,2,3,4,Integer.MAX_VALUE. When we faced with 1 then can skip future iteration and return result.
public int min(String s) {
int min = Integer.MAX_VALUE;
for (Character ch : s.toCharArray()) {
int current = mapToFactor(ch);
if(current == 1) {
//How I can implement this in Java 8 stream style?
return 1;
}
if (current < min) {
min = current;
}
return min;
}
}
So on if our String will wary large then we can significantly down performance by using Java 8 stream instead of Java 7 style with skip iterrations if 1 found.
Could you please explain how to write Java 7 code above in java 8 stream style?