StreamToLoopInspection: sorted() operation (currently for non-primitive streams only)

This commit is contained in:
Tagir Valeev
2016-12-27 13:13:53 +07:00
parent fb12495825
commit ae5d133904
10 changed files with 151 additions and 21 deletions
@@ -0,0 +1,23 @@
// "Replace Stream API chain with loop" "true"
import java.util.*;
public class Main {
public String testSorted(List<List<String>> list) {
for (List<String> lst : list) {
List<String> toSort = new ArrayList<>();
for (String x : lst) {
if (x != null) {
toSort.add(x);
}
}
toSort.sort(null);
for (String x : toSort) {
if (x.length() < 5) {
return x;
}
}
}
return "";
}
}
@@ -0,0 +1,25 @@
// "Replace Stream API chain with loop" "true"
import java.util.*;
import java.util.stream.*;
public class Main {
public List<String> testSorted(List<String> list) {
List<String> toSort = new ArrayList<>();
for (String s : list) {
if (s != null) {
toSort.add(s);
}
}
toSort.sort(null);
List<String> result = new ArrayList<>();
Set<String> uniqueValues = new HashSet<>();
for (String s : toSort) {
String trim = s.trim();
if (uniqueValues.add(trim)) {
result.add(trim);
}
}
return result;
}
}
@@ -0,0 +1,19 @@
// "Replace Stream API chain with loop" "true"
import java.util.*;
import java.util.stream.*;
public class Main {
public List<String> testSorted(List<String> list) {
List<String> toSort = new ArrayList<>();
for (String s : list) {
toSort.add(s);
}
toSort.sort(String.CASE_INSENSITIVE_ORDER);
List<String> result = new ArrayList<>();
for (String s : toSort) {
result.add(s);
}
return result;
}
}
@@ -0,0 +1,9 @@
// "Replace Stream API chain with loop" "true"
import java.util.*;
public class Main {
public String testSorted(List<List<String>> list) {
return list.stream().flatMap(lst -> lst.stream().filter(Objects::nonNull).sorted()).filter(x -> x.length() < 5).<caret>findFirst().orElse("");
}
}
@@ -0,0 +1,10 @@
// "Replace Stream API chain with loop" "true"
import java.util.*;
import java.util.stream.*;
public class Main {
public List<String> testSorted(List<String> list) {
return list.stream().filter(Objects::nonNull).sorted().map(String::trim).distinct().<caret>collect(Collectors.toList());
}
}
@@ -0,0 +1,10 @@
// "Replace Stream API chain with loop" "true"
import java.util.*;
import java.util.stream.*;
public class Main {
public List<String> testSorted(List<String> list) {
return list.stream().sorted(String.CASE_INSENSITIVE_ORDER).<caret>collect(Collectors.toList());
}
}