FuseStreamOperationsInspection

Fixes IDEA-179303 Suggest replacing Collectors.toList()/toSet() + collection constructor with Collectors.toCollection
This commit is contained in:
Tagir Valeev
2017-09-26 11:01:37 +07:00
parent b2df7a760a
commit 2a29c82c70
17 changed files with 436 additions and 4 deletions
@@ -0,0 +1,18 @@
// "Fix all 'Subsequent steps can be fused into Stream API chain' problems in file" "true"
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
interface Foo {
}
// IDEA-179303
void test1(Stream<Foo> fooStream) {
ArrayList<Foo> collectedFoos = fooStream.collect(Collectors.toCollection(ArrayList::new));
}
void test2(Stream<Foo> fooStream) {
List<Foo> collectedFoos = fooStream.collect(Collectors.toList());
}
}
@@ -0,0 +1,10 @@
// "Fuse HashSet and ArrayList into the Stream API chain" "true"
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
Collection<String> test(String[] args) {
return Arrays.stream(args).filter(String::isEmpty).distinct().collect(Collectors.toList());
}
}
@@ -0,0 +1,10 @@
// "Fuse ArrayList, 'sort' and 'toArray' into the Stream API chain" "true"
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
public void testSetListSort(String[] args) {
System.out.println(Arrays.stream(args).distinct().sorted().toArray());
}
}
@@ -0,0 +1,11 @@
// "Fuse 'sort' into the Stream API chain" "true"
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
public List<String> testToArray(String[] args) {
List<String> list = Arrays.stream(args).sorted().collect(Collectors.toList());
return list;
}
}
@@ -0,0 +1,10 @@
// "Fuse 'toArray' into the Stream API chain" "true"
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
public String[] testToArray(String[] args) {
return Arrays.stream(args).toArray(String[]::new);
}
}
@@ -0,0 +1,10 @@
// "Fuse 'toArray' into the Stream API chain" "true"
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
public String[] testToArray(String[] args) {
return Arrays.stream(args).distinct().sorted().toArray(String[]::new);
}
}
@@ -0,0 +1,18 @@
// "Fix all 'Subsequent steps can be fused into Stream API chain' problems in file" "true"
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
interface Foo {
}
// IDEA-179303
void test1(Stream<Foo> fooStream) {
ArrayList<Foo> collectedFoos = new ArrayList<>(fooStream.co<caret>llect(Collectors.toList()));
}
void test2(Stream<Foo> fooStream) {
List<Foo> collectedFoos = new ArrayList<>(fooStream.collect(Collectors.toList()));
}
}
@@ -0,0 +1,12 @@
// "Fuse HashSet and ArrayList into the Stream API chain" "true"
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
Collection<String> test(String[] args) {
List<String> list = Arrays.stream(args).filter(String::isEmpty).coll<caret>ect(Collectors.toList());
HashSet<String> strings = new HashSet<>(list);
return new ArrayList<>(strings);
}
}
@@ -0,0 +1,13 @@
// "Fuse ArrayList, 'sort' and 'toArray' into the Stream API chain" "true"
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
public void testSetListSort(String[] args) {
Set<String> set = Arrays.stream(args).co<caret>llect(Collectors.toSet());
List<String> list = new ArrayList<>(set);
list.sort(null);
System.out.println(list.toArray());
}
}
@@ -0,0 +1,12 @@
// "Fuse 'sort' into the Stream API chain" "true"
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
public List<String> testToArray(String[] args) {
List<String> list = Arrays.stream(args).c<caret>ollect(Collectors.toList());
list.sort(null);
return list;
}
}
@@ -0,0 +1,11 @@
// "Fuse 'toArray' into the Stream API chain" "true"
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
public String[] testToArray(String[] args) {
List<String> list = Arrays.stream(args).co<caret>llect(Collectors.toList());
return list.toArray(new String[list.size()]);
}
}
@@ -0,0 +1,11 @@
// "Fuse 'toArray' into the Stream API chain" "true"
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test {
public String[] testToArray(String[] args) {
Set<String> set = Arrays.stream(args).co<caret>llect(Collectors.toCollection(TreeSet::new));
return set.toArray(new String[set.size()]);
}
}