mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-08-25 14:25:04 +07:00
IDEA-168203 StreamApiMigration: accept more standard collection constructors
This commit is contained in:
+52
@@ -0,0 +1,52 @@
|
||||
// "Fix all 'Loop can be collapsed with Stream API' problems in file" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Test {
|
||||
void testList(List<String> input) {
|
||||
List<String> result = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toCollection(() -> new ArrayList<>(10)));
|
||||
System.out.println(result);
|
||||
|
||||
ArrayList<String> result2 = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toCollection(() -> new ArrayList<>(20)));
|
||||
System.out.println(result2);
|
||||
|
||||
// Non-empty
|
||||
ArrayList<String> result3 = new ArrayList<>(input);
|
||||
input.stream().filter(s -> !s.isEmpty()).forEach(result3::add);
|
||||
System.out.println(result3);
|
||||
}
|
||||
|
||||
void testSet(List<String> input) {
|
||||
Set<String> result = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toCollection(() -> new HashSet<>(10)));
|
||||
System.out.println(result);
|
||||
|
||||
Collection<String> result2 = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toCollection(() -> new LinkedHashSet<>(20, 0.8f)));
|
||||
System.out.println(result2);
|
||||
|
||||
// Non-empty
|
||||
AbstractSet<String> result3 = new HashSet<>(input);
|
||||
input.stream().filter(s -> !s.isEmpty()).forEach(result3::add);
|
||||
System.out.println(result3);
|
||||
|
||||
Collection<TimeUnit> result4 = input.stream().filter(s -> !s.isEmpty()).map(TimeUnit::valueOf).collect(Collectors.toCollection(() -> EnumSet.noneOf(TimeUnit.class)));
|
||||
System.out.println(result4);
|
||||
}
|
||||
|
||||
void testMap(List<String> input) {
|
||||
Map<Integer, String> result = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toMap(String::length, s -> s, (a, b) -> b, () -> new HashMap<>(10)));
|
||||
System.out.println(result);
|
||||
|
||||
Map<Integer, String> result2 = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toMap(String::length, s -> s, (a, b) -> b, () -> new HashMap<>(10, 0.8f)));
|
||||
System.out.println(result2);
|
||||
|
||||
EnumMap<TimeUnit, String> result3 = input.stream().filter(s -> !s.isEmpty()).collect(Collectors.toMap(TimeUnit::valueOf, s -> s, (a, b) -> b, () -> new EnumMap<>(TimeUnit.class)));
|
||||
System.out.println(result3);
|
||||
|
||||
// Non-empty
|
||||
EnumMap<TimeUnit, String> result4 = new EnumMap<>(result3);
|
||||
input.stream().filter(s -> !s.isEmpty()).forEach(s -> result4.put(TimeUnit.valueOf(s), s));
|
||||
System.out.println(result4);
|
||||
}
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
// "Fix all 'Loop can be collapsed with Stream API' problems in file" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class Test {
|
||||
void testList(List<String> input) {
|
||||
List<String> result = new ArrayList<>(10);
|
||||
for (String s : in<caret>put) {
|
||||
if (!s.isEmpty()) {
|
||||
result.add(s);
|
||||
}
|
||||
}
|
||||
System.out.println(result);
|
||||
|
||||
ArrayList<String> result2 = new ArrayList<>(20);
|
||||
for (String s : input) {
|
||||
if (!s.isEmpty()) {
|
||||
result2.add(s);
|
||||
}
|
||||
}
|
||||
System.out.println(result2);
|
||||
|
||||
// Non-empty
|
||||
ArrayList<String> result3 = new ArrayList<>(input);
|
||||
for (String s : input) {
|
||||
if (!s.isEmpty()) {
|
||||
result3.add(s);
|
||||
}
|
||||
}
|
||||
System.out.println(result3);
|
||||
}
|
||||
|
||||
void testSet(List<String> input) {
|
||||
Set<String> result = new HashSet<>(10);
|
||||
for (String s : input) {
|
||||
if (!s.isEmpty()) {
|
||||
result.add(s);
|
||||
}
|
||||
}
|
||||
System.out.println(result);
|
||||
|
||||
Collection<String> result2 = new LinkedHashSet<>(20, 0.8f);
|
||||
for (String s : input) {
|
||||
if (!s.isEmpty()) {
|
||||
result2.add(s);
|
||||
}
|
||||
}
|
||||
System.out.println(result2);
|
||||
|
||||
// Non-empty
|
||||
AbstractSet<String> result3 = new HashSet<>(input);
|
||||
for (String s : input) {
|
||||
if (!s.isEmpty()) {
|
||||
result3.add(s);
|
||||
}
|
||||
}
|
||||
System.out.println(result3);
|
||||
|
||||
Collection<TimeUnit> result4 = EnumSet.noneOf(TimeUnit.class);
|
||||
for (String s : input) {
|
||||
if (!s.isEmpty()) {
|
||||
result4.add(TimeUnit.valueOf(s));
|
||||
}
|
||||
}
|
||||
System.out.println(result4);
|
||||
}
|
||||
|
||||
void testMap(List<String> input) {
|
||||
Map<Integer, String> result = new HashMap<>(10);
|
||||
for (String s : input) {
|
||||
if (!s.isEmpty()) {
|
||||
result.put(s.length(), s);
|
||||
}
|
||||
}
|
||||
System.out.println(result);
|
||||
|
||||
Map<Integer, String> result2 = new HashMap<>(10, 0.8f);
|
||||
for (String s : input) {
|
||||
if (!s.isEmpty()) {
|
||||
result2.put(s.length(), s);
|
||||
}
|
||||
}
|
||||
System.out.println(result2);
|
||||
|
||||
EnumMap<TimeUnit, String> result3 = new EnumMap<>(TimeUnit.class);
|
||||
for (String s : input) {
|
||||
if (!s.isEmpty()) {
|
||||
result3.put(TimeUnit.valueOf(s), s);
|
||||
}
|
||||
}
|
||||
System.out.println(result3);
|
||||
|
||||
// Non-empty
|
||||
EnumMap<TimeUnit, String> result4 = new EnumMap<>(result3);
|
||||
for (String s : input) {
|
||||
if (!s.isEmpty()) {
|
||||
result4.put(TimeUnit.valueOf(s), s);
|
||||
}
|
||||
}
|
||||
System.out.println(result4);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user