IDEA-195573 Support collectingAndThen in Stream-to-loop (for now only if downstream is collection-based)

This commit is contained in:
Tagir Valeev
2018-07-29 09:14:48 +07:00
parent 6216496a7f
commit 9d96fb9a63
3 changed files with 111 additions and 8 deletions

View File

@@ -0,0 +1,51 @@
// "Fix all 'Stream API call chain can be replaced with loop' problems in file" "true"
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
void sample(List<String> people) {
// comment
List<String> list = new ArrayList<>();
for (String person : people) {
list.add(person);
}
List<String> list1 = Collections.unmodifiableList(list);
}
void sample1(List<String> people) {
// comment
List<String> list = new ArrayList<>();
for (String person : people) {
list.add(person);
}
List<String> listIdentity = list;
}
void sample2(List<String> people) {
// comment
Map<Integer, String> result = new HashMap<>();
for (String person : people) {
if (result.put(person.length(), person) != null) {
throw new IllegalStateException("Duplicate key");
}
}
Map<Integer, String> map = Collections.unmodifiableMap(result);
}
void sample3(List<String> people) {
List<String> list2 = people.stream().collect( // comment
Collectors.collectingAndThen(Collectors.<String, List<String>>toCollection(LinkedList::new),
list -> {
List<String> result = new ArrayList<>();
for (Iterator<String> it = Stream.concat(list.stream(), list.stream()).iterator(); it.hasNext(); ) {
String s = it.next();
result.add(s);
}
return result;
}));
}
}

View File

@@ -0,0 +1,33 @@
// "Fix all 'Stream API call chain can be replaced with loop' problems in file" "true"
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
void sample(List<String> people) {
List<String> list1 = people.stream().coll<caret>ect( // comment
Collectors.collectingAndThen(Collectors.toList(),
Collections::unmodifiableList));
}
void sample1(List<String> people) {
List<String> listIdentity = people.stream().collect( // comment
Collectors.collectingAndThen(Collectors.toList(),
Function.identity()));
}
void sample2(List<String> people) {
Map<Integer, String> map = people.stream().collect( // comment
Collectors.collectingAndThen(Collectors.toMap(String::length, Function.identity()),
Collections::unmodifiableMap));
}
void sample3(List<String> people) {
List<String> list2 = people.stream().collect( // comment
Collectors.collectingAndThen(Collectors.<String, List<String>>toCollection(LinkedList::new),
list -> Stream.concat(list.stream(), list.stream()).collect(Collectors.toList())));
}
}