IDEA-166211 Convert for-loop with Collections.addAll inside into Java 8's stream API calls chain

This commit is contained in:
Tagir Valeev
2017-01-11 15:57:54 +07:00
parent 616eb95766
commit 1ee8a1fbfe
6 changed files with 137 additions and 24 deletions
@@ -0,0 +1,8 @@
// "Replace with toArray" "true"
import java.util.*;
public class Test {
Object[] test(List<String[]> list) {
return list.stream().filter(Objects::nonNull).flatMap(Arrays::stream).sorted().toArray();
}
}
@@ -0,0 +1,13 @@
// "Replace with toArray" "true"
import java.util.*;
import java.util.stream.Stream;
public class Test {
Object[] test(List<String> list) {
return list.stream().filter(Objects::nonNull).flatMap(str -> Stream.of(str, str + str)).sorted().toArray();
}
public static void main(String[] args) {
System.out.println(Arrays.toString(new Test().test(Arrays.asList("a", "b", "ba", "x", null, "c"))));
}
}
@@ -0,0 +1,15 @@
// "Replace with toArray" "true"
import java.util.*;
public class Test {
Object[] test(List<String[]> list) {
List<Object> result = new LinkedList<>();
for(String[] str : li<caret>st) {
if(str != null) {
Collections.addAll(result, str);
}
}
result.sort(null);
return result.toArray();
}
}
@@ -0,0 +1,19 @@
// "Replace with toArray" "true"
import java.util.*;
public class Test {
Object[] test(List<String> list) {
List<Object> result = new LinkedList<>();
for(String str : lis<caret>t) {
if(str != null) {
Collections.addAll(result, str, str+str);
}
}
result.sort(null);
return result.toArray();
}
public static void main(String[] args) {
System.out.println(Arrays.toString(new Test().test(Arrays.asList("a", "b", "ba", "x", null, "c"))));
}
}