Stream API migration: replace with findFirst().ifPresent() chain

This commit is contained in:
Tagir Valeev
2016-10-03 12:43:45 +07:00
parent 50a618dfb2
commit 3c07adbe09
5 changed files with 63 additions and 2 deletions
@@ -0,0 +1,13 @@
// "Replace with findFirst()" "true"
import java.util.*;
public class Main {
public Integer[] testFindFirstIfPresent(List<List<String>> data) {
List<Integer> result = new ArrayList<>();
for (List<String> list : data) {
list.stream().filter(str -> !str.isEmpty()).findFirst().ifPresent(str -> result.add(str.length()));
}
return result.toArray(new Integer[result.size()]);
}
}
@@ -0,0 +1,18 @@
// "Replace with findFirst()" "true"
import java.util.*;
public class Main {
public Integer[] testFindFirstIfPresent(List<List<String>> data) {
List<Integer> result = new ArrayList<>();
for (List<String> list : data) {
for (String str : lis<caret>t) {
if (!str.isEmpty()) {
result.add(str.length());
break;
}
}
}
return result.toArray(new Integer[result.size()]);
}
}
@@ -0,0 +1,20 @@
// "Replace with findFirst()" "false"
import java.util.*;
// The result will have block-lambda so we don't suggest the replacement here
public class Main {
public Integer[] testFindFirstIfPresent(List<List<String>> data) {
List<Integer> result = new ArrayList<>();
for (List<String> list : data) {
for (String str : lis<caret>t) {
if (!str.isEmpty()) {
result.add(str.length());
System.out.println(str);
break;
}
}
}
return result.toArray(new Integer[result.size()]);
}
}