IDEA-160898 Inspection to convert several commonly-used lambdas to method references

This commit is contained in:
Tagir Valeev
2016-09-08 12:25:37 +07:00
parent f0c23baa29
commit 8015562c2d
25 changed files with 232 additions and 47 deletions
@@ -1,11 +1,12 @@
// "Replace with forEach" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
class Sample {
List<String> foo = new ArrayList<>();
String foo(){
((List<String>) foo).stream().filter(s -> s == null).forEach(System.out::println);
((List<String>) foo).stream().filter(Objects::isNull).forEach(System.out::println);
return null;
}
}
@@ -10,6 +10,6 @@ public class Collect {
}
void collectNames(List<Person> persons){
List<String> names = persons.stream().filter(person -> person != null).map(Person::getName).collect(Collectors.toList());
List<String> names = persons.stream().filter(Objects::nonNull).map(Person::getName).collect(Collectors.toList());
}
}
@@ -1,10 +1,11 @@
// "Replace with count()" "true"
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
public class Main {
public void test(List<Set<String>> nested) {
int count = (int) nested.stream().filter(element -> element != null).flatMap(Collection::stream).filter(str -> str.startsWith("xyz")).count();
int count = (int) nested.stream().filter(Objects::nonNull).flatMap(Collection::stream).filter(str -> str.startsWith("xyz")).count();
}
}
@@ -1,12 +1,9 @@
// "Replace with collect" "true"
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public void test(List<Set<String>> nested) {
List<String> result = nested.stream().filter(element -> element != null).flatMap(Collection::stream).filter(str -> str.startsWith("xyz")).map(String::trim).collect(Collectors.toList());
List<String> result = nested.stream().filter(Objects::nonNull).flatMap(Collection::stream).filter(str -> str.startsWith("xyz")).map(String::trim).collect(Collectors.toList());
}
}
@@ -1,10 +1,11 @@
// "Replace with forEach" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
class Sample {
List<String> foo = new ArrayList<>();
{
foo.stream().filter(s -> s != null).forEach(System.out::println);
foo.stream().filter(Objects::nonNull).forEach(System.out::println);
}
}
@@ -2,11 +2,12 @@
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<String> test(String[][] arr) {
List<String> result = Arrays.stream(arr).filter(subArr -> subArr != null).flatMap(Arrays::stream).collect(Collectors.toList());
List<String> result = Arrays.stream(arr).filter(Objects::nonNull).flatMap(Arrays::stream).collect(Collectors.toList());
return result;
}
}
@@ -2,11 +2,12 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class Main {
public List<Integer> test(int[][] arr) {
List<Integer> result = new ArrayList<>();
Arrays.stream(arr).filter(subArr -> subArr != null).forEach(subArr -> {
Arrays.stream(arr).filter(Objects::nonNull).forEach(subArr -> {
for (int str : subArr) {
result.add(str);
}
@@ -1,11 +1,12 @@
// "Replace with forEach" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
class Sample {
List<String> foo = new ArrayList<>();
String foo(){
foo.stream().filter(s -> s == null).forEach(s -> {
foo.stream().filter(Objects::isNull).forEach(s -> {
int i = 0;
});
return null;
@@ -1,10 +1,11 @@
// "Replace with forEach" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
class Sample {
List<String> foo = new ArrayList<>();
{
foo.stream().filter(s -> s != null).forEach(System.out::println);
foo.stream().filter(Objects::nonNull).forEach(System.out::println);
}
}
@@ -1,10 +1,11 @@
// "Replace with forEach" "true"
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
class Sample {
List<String> foo = new ArrayList<>();
{
foo.stream().filter(s -> s != null).filter(s -> s.startsWith("a")).forEach(System.out::println);
foo.stream().filter(Objects::nonNull).filter(s -> s.startsWith("a")).forEach(System.out::println);
}
}
@@ -1,11 +1,12 @@
// "Replace with sum()" "true"
import java.util.Arrays;
import java.util.Objects;
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();
d += Arrays.stream(array).filter(Objects::nonNull).flatMap(Arrays::stream).filter(a -> a.startsWith("xyz")).mapToDouble(a -> 1.0 / a.length()).sum();
return d;
}
}