IDEA-161706 Migration to Stream API: support primitive types int/long/double

This commit is contained in:
Tagir Valeev
2016-09-26 13:05:05 +07:00
parent 66db09af76
commit 16e8ad126e
29 changed files with 373 additions and 90 deletions
@@ -0,0 +1,9 @@
// "Replace with anyMatch()" "true"
import java.util.List;
public class Main {
public boolean testPrimitiveMap(List<String> data) {
return data.stream().filter(str -> str.startsWith("xyz")).mapToInt(String::length).anyMatch(len -> len > 10);
}
}
@@ -0,0 +1,11 @@
// "Replace with collect" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public void testPrimitiveMap(List<String> data) {
List<Integer> list = data.stream().filter(str -> str.startsWith("xyz")).mapToInt(String::length).filter(len -> len > 10).boxed().collect(Collectors.toList());
}
}
@@ -0,0 +1,11 @@
// "Replace with collect" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public void testPrimitiveMap(List<String> data) {
List<String> list = data.stream().filter(str -> str.startsWith("xyz")).mapToInt(String::length).filter(len -> len > 10).mapToObj(String::valueOf).collect(Collectors.toList());
}
}
@@ -0,0 +1,10 @@
// "Replace with count()" "true"
import java.util.Arrays;
public class Main {
public int testPrimitiveArray(int[] data) {
int count = (int) Arrays.stream(data).mapToLong(val -> val * val).filter(square -> square > 100).count();
return count;
}
}
@@ -0,0 +1,9 @@
// "Replace with findFirst()" "true"
import java.util.List;
public class Main {
public int testPrimitiveMap(List<String> data) {
return data.stream().filter(str -> str.startsWith("xyz")).mapToInt(String::length).filter(len -> len > 10).findFirst().orElse(0);
}
}
@@ -1,17 +1,13 @@
// "Replace with forEach" "true"
// "Replace with collect" "true"
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class Main {
public List<Integer> test(int[][] arr) {
List<Integer> result = new ArrayList<>();
Arrays.stream(arr).filter(Objects::nonNull).forEach(subArr -> {
for (int str : subArr) {
result.add(str);
}
});
return result;
List<Integer> result = Arrays.stream(arr).filter(Objects::nonNull).flatMapToInt(Arrays::stream).boxed().collect(Collectors.toList());
return result;
}
}
@@ -6,9 +6,9 @@ import java.util.Objects;
class Sample {
List<String> foo = new ArrayList<>();
String foo(){
foo.stream().filter(Objects::isNull).forEach(s -> {
int i = 0;
});
foo.stream().filter(Objects::isNull).forEach(s -> bar());
return null;
}
bar() {}
}
@@ -0,0 +1,9 @@
// "Replace with sum()" "true"
import java.util.List;
public class Main {
public void testPrimitiveMap(List<String> data) {
int sum = data.stream().filter(str -> str.startsWith("xyz")).mapToInt(String::length).filter(len -> len > 10).map(len -> len * 2).sum();
}
}
@@ -0,0 +1,9 @@
// "Replace with sum()" "true"
import java.util.List;
public class Main {
public void testPrimitiveMap(List<String> data) {
long sum = data.stream().filter(str -> str.startsWith("xyz")).mapToInt(String::length).filter(len -> len > 10).asLongStream().sum();
}
}
@@ -0,0 +1,17 @@
// "Replace with anyMatch()" "false"
import java.util.List;
public class Main {
public boolean testPrimitiveMap(List<String> data) {
for(String str : d<caret>ata) {
if(str.startsWith("xyz")) {
float len = str.length();
if(len > 10) {
return true;
}
}
}
return false;
}
}
@@ -0,0 +1,17 @@
// "Replace with anyMatch()" "true"
import java.util.List;
public class Main {
public boolean testPrimitiveMap(List<String> data) {
for(String str : d<caret>ata) {
if(str.startsWith("xyz")) {
int len = str.length();
if(len > 10) {
return true;
}
}
}
return false;
}
}
@@ -0,0 +1,18 @@
// "Replace with collect" "true"
import java.util.ArrayList;
import java.util.List;
public class Main {
public void testPrimitiveMap(List<String> data) {
List<Integer> list = new ArrayList<>();
for(String str : d<caret>ata) {
if(str.startsWith("xyz")) {
int len = str.length();
if(len > 10) {
list.add(len);
}
}
}
}
}
@@ -0,0 +1,18 @@
// "Replace with collect" "true"
import java.util.ArrayList;
import java.util.List;
public class Main {
public void testPrimitiveMap(List<String> data) {
List<String> list = new ArrayList<>();
for(String str : d<caret>ata) {
if(str.startsWith("xyz")) {
int len = str.length();
if(len > 10) {
list.add(String.valueOf(len));
}
}
}
}
}
@@ -0,0 +1,14 @@
// "Replace with count()" "true"
public class Main {
public int testPrimitiveArray(int[] data) {
int count = 0;
for(int val : dat<caret>a) {
long square = val*val;
if(square > 100) {
count++;
}
}
return count;
}
}
@@ -0,0 +1,14 @@
// "Replace with count()" "false"
public class Main {
public int testPrimitiveArray(short[] data) {
int count = 0;
for(int val : dat<caret>a) {
long square = val*val;
if(square > 100) {
count++;
}
}
return count;
}
}
@@ -0,0 +1,17 @@
// "Replace with findFirst()" "true"
import java.util.List;
public class Main {
public int testPrimitiveMap(List<String> data) {
for(String str : dat<caret>a) {
if(str.startsWith("xyz")) {
int len = str.length();
if(len > 10) {
return len;
}
}
}
return 0;
}
}
@@ -0,0 +1,18 @@
// "Replace with findFirst()" "false"
import java.util.List;
// Not supported as OptionalInt lacks mapping method
public class Main {
public int testPrimitiveMap(List<String> data) {
for(String str : dat<caret>a) {
if(str.startsWith("xyz")) {
int len = str.length();
if(len > 10) {
return len * 2;
}
}
}
return 0;
}
}
@@ -1,6 +1,6 @@
// "Replace with forEach" "true"
// "Replace with collect" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.List;
public class Main {
public List<Integer> test(int[][] arr) {
@@ -6,8 +6,10 @@ class Sample {
List<String> foo = new ArrayList<>();
String foo(){
for (String s : fo<caret>o) {
if (s == null) int i = 0;
if (s == null) bar();
}
return null;
}
bar() {}
}
@@ -0,0 +1,17 @@
// "Replace with sum()" "true"
import java.util.List;
public class Main {
public void testPrimitiveMap(List<String> data) {
int sum = 0;
for(String str : dat<caret>a) {
if(str.startsWith("xyz")) {
int len = str.length();
if(len > 10) {
sum += len*2;
}
}
}
}
}
@@ -0,0 +1,17 @@
// "Replace with sum()" "true"
import java.util.List;
public class Main {
public void testPrimitiveMap(List<String> data) {
long sum = 0;
for(String str : da<caret>ta) {
if(str.startsWith("xyz")) {
int len = str.length();
if(len > 10) {
sum += len;
}
}
}
}
}