IDEA-160789 Migration to Stream API: replace with mapToInt/Long/Double().sum() when possible

This commit is contained in:
Tagir Valeev
2016-09-06 15:50:03 +07:00
parent 6679bf239a
commit 838d8a7146
6 changed files with 443 additions and 261 deletions
@@ -0,0 +1,10 @@
// "Replace with sum()" "true"
import java.util.Arrays;
public class Main {
public long test(String[] array) {
long i = Arrays.stream(array).filter(a -> a.startsWith("xyz")).mapToLong(String::length).sum();
return i;
}
}
@@ -0,0 +1,11 @@
// "Replace with sum()" "true"
import java.util.Arrays;
public class Main {
public double test(String[][] array) {
double d = 10;
d += Arrays.stream(array).filter(arr -> arr != null).flatMap(Arrays::stream).filter(a -> a.startsWith("xyz")).mapToDouble(a -> 1.0 / a.length()).sum();
return d;
}
}
@@ -0,0 +1,12 @@
// "Replace with sum()" "true"
public class Main {
public long test(String[] array) {
long i = 0;
for(String a : ar<caret>ray) {
if(a.startsWith("xyz"))
i = i + a.length();
}
return i;
}
}
@@ -0,0 +1,12 @@
// "Replace with sum()" "false"
public class Main {
public long test(String[] array) {
float i = 0;
for(String a : ar<caret>ray) {
if(a.startsWith("xyz"))
i = i + a.length();
}
return i;
}
}
@@ -0,0 +1,16 @@
// "Replace with sum()" "true"
public class Main {
public double test(String[][] array) {
double d = 10;
for(String[] arr : arra<caret>y) {
if(arr != null) {
for (String a : arr) {
if (a.startsWith("xyz"))
d = d + 1.0/a.length();
}
}
}
return d;
}
}