mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-08-26 00:11:26 +07:00
IDEA-163405 Migration from Stream API back to for loops: iteration#2
This commit is contained in:
+23
@@ -0,0 +1,23 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Main {
|
||||
public void test(String... list) {
|
||||
double sum = 0;
|
||||
long count = 0;
|
||||
for (String s : list) {
|
||||
if (Objects.nonNull(s)) {
|
||||
sum += 1.0 / s;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
System.out.println((count == 0 ? 0.0 : sum / count));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Main().test("a", "bbb", null, "cc", "dd", "eedasfasdfs");
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Main {
|
||||
public void test(String... list) {
|
||||
long sum = 0;
|
||||
long count = 0;
|
||||
for (String s : list) {
|
||||
if (Objects.nonNull(s)) {
|
||||
sum += s.length();
|
||||
count++;
|
||||
}
|
||||
}
|
||||
System.out.println((count == 0 ? 0.0 : (double) sum / count));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Main().test("a", "bbb", null, "cc", "dd", "eedasfasdfs");
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static long test(List<String> strings) {
|
||||
long count = 0;
|
||||
for (String s : strings) {
|
||||
if (!s.isEmpty()) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Optional<String> test(List<String> strings) {
|
||||
Comparator<String> comparator = Comparator.naturalOrder();
|
||||
boolean seen = false;
|
||||
String best = null;
|
||||
for (String s : strings) {
|
||||
if (!s.isEmpty()) {
|
||||
if (!seen || comparator.compare(s, best) > 0) {
|
||||
seen = true;
|
||||
best = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (seen ? Optional.of(best) : Optional.empty());
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Main<T> {
|
||||
void test() {
|
||||
Integer acc = 0;
|
||||
for (String s : Arrays.asList("a", "bb", "ccc")) {
|
||||
Integer length = s.length();
|
||||
acc = Integer.sum(acc, length);
|
||||
}
|
||||
Integer totalLength = acc;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.DoubleSummaryStatistics;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static DoubleSummaryStatistics test(List<String> strings) {
|
||||
DoubleSummaryStatistics stat = new DoubleSummaryStatistics();
|
||||
for (String str : strings) {
|
||||
if (Objects.nonNull(str)) {
|
||||
stat.accept(str.length() / 2.0);
|
||||
}
|
||||
}
|
||||
return stat;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList(null, null)));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Double test(List<String> strings) {
|
||||
double sum = 0;
|
||||
for (String string : strings) {
|
||||
if (Objects.nonNull(string)) {
|
||||
sum += string.length();
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList(null, null)));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public void test(List<String> list) {
|
||||
List<String> result = new ArrayList<>();
|
||||
Function<? super String, ? extends String> function = list.size() < 10 ? String::trim : Function.identity();
|
||||
for (String s : list) {
|
||||
String s1 = function.apply(s);
|
||||
result.add(s1);
|
||||
}
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -10,8 +10,8 @@ public class Main {
|
||||
for (List<String> a : list) {
|
||||
if (a != null) {
|
||||
for (String s : a) {
|
||||
long l = s.length();
|
||||
stat.accept(l);
|
||||
long length = s.length();
|
||||
stat.accept(length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -10,8 +10,8 @@ public class Main {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
for (int x : input) {
|
||||
if (x > 0) {
|
||||
Integer integer = x * 2;
|
||||
list.add(integer);
|
||||
Integer i = x * 2;
|
||||
list.add(i);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
|
||||
+6
-6
@@ -7,12 +7,12 @@ import java.util.stream.Stream;
|
||||
public class Main {
|
||||
private static long test(List<? extends String> list) {
|
||||
long count = 0;
|
||||
for (Object o : Arrays.<Object>asList(0, null, "1", list)) {
|
||||
for (Object o1 : Arrays.<Object>asList(o)) {
|
||||
for (Object o2 : Arrays.<Object>asList(o1)) {
|
||||
for (Object o3 : Arrays.<Object>asList(o2)) {
|
||||
for (Object o4 : Arrays.<Object>asList(o3)) {
|
||||
for (Object o5 : Arrays.<Object>asList(o4)) {
|
||||
for (Object o : Arrays.asList(0, null, "1", list)) {
|
||||
for (Object o1 : Arrays.asList(o)) {
|
||||
for (Object o2 : Arrays.asList(o1)) {
|
||||
for (Object o3 : Arrays.asList(o2)) {
|
||||
for (Object o4 : Arrays.asList(o3)) {
|
||||
for (Object o5 : Arrays.asList(o4)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> list) {
|
||||
List<Integer> result = new ArrayList<>();
|
||||
for (String x : list) {
|
||||
if (x != null) {
|
||||
Predicate<Integer> predicate = Predicate.isEqual(x.length());
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Integer integer = i;
|
||||
if (predicate.test(integer)) {
|
||||
result.add(integer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println(result);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbbb", "cccccccccc", "dd", ""));
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -13,8 +13,8 @@ public class Main {
|
||||
for (List<String> lst : l) {
|
||||
if (lst != null) {
|
||||
for (String str : lst) {
|
||||
int i = str.length();
|
||||
stat.accept(i);
|
||||
int length = str.length();
|
||||
stat.accept(length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -10,8 +10,8 @@ public class Main {
|
||||
for (List<String> a : list) {
|
||||
if (a != null) {
|
||||
for (String s : a) {
|
||||
long l = s.length();
|
||||
stat.accept(l);
|
||||
long length = s.length();
|
||||
stat.accept(length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.SplittableRandom;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Integer acc = 0;
|
||||
SplittableRandom splittableRandom = new SplittableRandom(1);
|
||||
for (long count = 100; count > 0; count--) {
|
||||
Integer integer = 500;
|
||||
acc = splittableRandom.nextInt(acc, integer);
|
||||
}
|
||||
int n1 = acc;
|
||||
System.out.println(n1);
|
||||
}
|
||||
}
|
||||
+4
-6
@@ -10,12 +10,10 @@ public class Main {
|
||||
Random r = new Random();
|
||||
DoubleSummaryStatistics stat = new DoubleSummaryStatistics();
|
||||
for (int x = 0; x < 10; x++) {
|
||||
double x1 = x;
|
||||
long limit = (long) x1;
|
||||
while (true) {
|
||||
double v = r.nextDouble() * x1;
|
||||
if (limit-- == 0) break;
|
||||
stat.accept(v);
|
||||
double v = x;
|
||||
for (long count = (long) v; count > 0; count--) {
|
||||
double v1 = r.nextDouble() * v;
|
||||
stat.accept(v1);
|
||||
}
|
||||
}
|
||||
return stat;
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Map<Integer, List<String>> test(List<String> strings) {
|
||||
Map<Integer, List<String>> map = new HashMap<>();
|
||||
for (String str : strings) {
|
||||
map.computeIfAbsent(str.length(), k -> new ArrayList<>()).add(str);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee")));
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Map<Integer, List<String>> test(List<String> strings, int k) {
|
||||
Map<Integer, List<String>> map = new HashMap<>();
|
||||
for (String string : strings) {
|
||||
map.computeIfAbsent(string.length(), key -> new ArrayList<>()).add(string);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee")));
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
private static TreeMap<Integer, LinkedHashSet<String>> getMap(List<String> strings) {
|
||||
TreeMap<Integer, LinkedHashSet<String>> map = new TreeMap<>(Comparator.reverseOrder());
|
||||
for (String string : strings) {
|
||||
map.computeIfAbsent(string.length(), k -> new LinkedHashSet<>()).add(string);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(getMap(Arrays.asList("a", "bbb", "cccc", "dddd", "ee", "e")));
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
Map<Integer, Map<Character, Set<String>>> map = new HashMap<>();
|
||||
for (String s : strings) {
|
||||
map.computeIfAbsent(s.length(), key -> new HashMap<>()).computeIfAbsent(s.charAt(0), k -> new HashSet<>()).add(s);
|
||||
}
|
||||
System.out.println(map);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbb", "cccc", "dddd"));
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
private static TreeMap<Integer, List<Integer>> getMap(List<String> strings) {
|
||||
TreeMap<Integer, List<Integer>> map = new TreeMap<>(Comparator.reverseOrder());
|
||||
for (String string : strings) {
|
||||
Integer len = string.length();
|
||||
Integer integer = len * 2;
|
||||
map.computeIfAbsent(string.length(), k -> new ArrayList<>()).add(integer);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(getMap(Arrays.asList("a", "bbb", "cccc", "dddd", "ee", "e")));
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
Map<Integer, DoubleSummaryStatistics> map = new HashMap<>();
|
||||
for (String string : strings) {
|
||||
if (Objects.nonNull(string)) {
|
||||
map.computeIfAbsent(string.length(), k -> new DoubleSummaryStatistics()).accept(string.length());
|
||||
}
|
||||
}
|
||||
System.out.println(map);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbb", "cccc", "dddd"));
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
Map<Integer, Map<Character, String>> map = new HashMap<>();
|
||||
for (String s : strings) {
|
||||
if (map.computeIfAbsent(s.length(), k -> new HashMap<>()).put(s.charAt(0), s) != null) {
|
||||
throw new IllegalStateException("Duplicate key");
|
||||
}
|
||||
}
|
||||
System.out.println(map);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbb", "cccc", "dddd"));
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
Map<Integer, Set<String>> map = new HashMap<>();
|
||||
for (String string : strings) {
|
||||
if (Objects.nonNull(string)) {
|
||||
map.computeIfAbsent(string.length(), k -> new HashSet<>()).add(string);
|
||||
}
|
||||
}
|
||||
System.out.println(map);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbb", "cccc", "dddd"));
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -9,8 +9,8 @@ public class Main {
|
||||
long limit = 20;
|
||||
for (String x = ""; ; x = x + "a") {
|
||||
if (limit-- == 0) break;
|
||||
int i = x.length();
|
||||
stat.accept(i);
|
||||
int length = x.length();
|
||||
stat.accept(length);
|
||||
}
|
||||
return stat;
|
||||
}
|
||||
|
||||
+2
-2
@@ -11,8 +11,8 @@ public class Main {
|
||||
long limitInner = limit;
|
||||
for (String x = ""; ; x = x + limit) {
|
||||
if (limitInner-- == 0) break;
|
||||
int i = x.length();
|
||||
stat.accept(i);
|
||||
int length = x.length();
|
||||
stat.accept(length);
|
||||
}
|
||||
}
|
||||
return stat;
|
||||
|
||||
+2
-2
@@ -12,8 +12,8 @@ public class Main {
|
||||
long limit = x;
|
||||
for (String s = String.valueOf(x); ; s = s + x) {
|
||||
if (limit-- == 0) break;
|
||||
int i = s.length();
|
||||
stat.accept(i);
|
||||
int length = s.length();
|
||||
stat.accept(length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -7,8 +7,8 @@ public class Main {
|
||||
private static long countNonEmpty(List<String> input) {
|
||||
long count = 0;
|
||||
for (String str : input) {
|
||||
String s = str.trim();
|
||||
if (!s.isEmpty()) {
|
||||
String trim = str.trim();
|
||||
if (!trim.isEmpty()) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
private static void getMap(List<String> strings) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
for (String string : strings) {
|
||||
Integer len = string.length();
|
||||
Integer integer = len * 2;
|
||||
list.add(integer);
|
||||
}
|
||||
System.out.println(list);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
getMap(Arrays.asList("a", "bbb", "cccc", "dddd", "ee", "e"));
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class Main {
|
||||
public static String test(List<String> strings) {
|
||||
Comparator<String> comparator = Comparator.comparing(String::length);
|
||||
boolean seen = false;
|
||||
String best = null;
|
||||
for (String string : strings) {
|
||||
if (!seen || comparator.compare(string, best) > 0) {
|
||||
seen = true;
|
||||
best = string;
|
||||
}
|
||||
}
|
||||
return (seen ? Optional.of(best) : Optional.<String>empty()).orElse(null);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee")));
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.OptionalDouble;
|
||||
|
||||
public class Main {
|
||||
public static double test(List<String> strings) {
|
||||
boolean seen = false;
|
||||
double best = 0;
|
||||
for (String string : strings) {
|
||||
double v = string.length();
|
||||
if (!seen || Double.compare(v, best) > 0) {
|
||||
seen = true;
|
||||
best = v;
|
||||
}
|
||||
}
|
||||
return (seen ? OptionalDouble.of(best) : OptionalDouble.empty()).orElse(-1);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d")));
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class Main {
|
||||
public static String test(List<String> strings, Comparator<String> cmp) {
|
||||
boolean seen = false;
|
||||
String best = null;
|
||||
for (String string : strings) {
|
||||
if (!seen || cmp.compare(string, best) < 0) {
|
||||
seen = true;
|
||||
best = string;
|
||||
}
|
||||
}
|
||||
return (seen ? Optional.of(best) : Optional.<String>empty()).orElse(null);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList(), Comparator.comparing(String::length)));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee"), Comparator.comparing(String::length)));
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class Main {
|
||||
public static String test(List<String> strings, Comparator<CharSequence> comparator) {
|
||||
Comparator<CharSequence> comparator1 = comparator.reversed();
|
||||
boolean seen = false;
|
||||
String best = null;
|
||||
for (String string : strings) {
|
||||
if (!seen || comparator1.compare(string, best) < 0) {
|
||||
seen = true;
|
||||
best = string;
|
||||
}
|
||||
}
|
||||
return (seen ? Optional.of(best) : Optional.<String>empty()).orElse(null);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList(), Comparator.comparing(CharSequence::length)));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee"), Comparator.comparing(CharSequence::length)));
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.OptionalInt;
|
||||
|
||||
public class Main {
|
||||
public static int test(List<String> strings) {
|
||||
boolean seen = false;
|
||||
int best = 0;
|
||||
for (String string : strings) {
|
||||
int i = string.length();
|
||||
if (!seen || i < best) {
|
||||
seen = true;
|
||||
best = i;
|
||||
}
|
||||
}
|
||||
return (seen ? OptionalInt.of(best) : OptionalInt.empty()).orElse(-1);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d")));
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -10,13 +10,13 @@ public class Main {
|
||||
long sum = 0;
|
||||
for (Map.Entry<String, List<String>> e : strings.entrySet()) {
|
||||
if (!e.getKey().isEmpty()) {
|
||||
long l = e.getValue().stream().filter(new Predicate<String>() {
|
||||
long count = e.getValue().stream().filter(new Predicate<String>() {
|
||||
@Override
|
||||
public boolean test(String s) {
|
||||
return e.getKey().equals(s);
|
||||
}
|
||||
}).count();
|
||||
sum += l;
|
||||
sum += count;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
|
||||
+2
-2
@@ -9,8 +9,8 @@ public class Main {
|
||||
long sum = 0;
|
||||
for (Map.Entry<String, List<String>> e : strings.entrySet()) {
|
||||
if (!e.getKey().isEmpty()) {
|
||||
long l = e.getValue().stream().filter(s -> e.getKey().equals(s)).count();
|
||||
sum += l;
|
||||
long count = e.getValue().stream().filter(s -> e.getKey().equals(s)).count();
|
||||
sum += count;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
|
||||
+2
-2
@@ -9,8 +9,8 @@ public class Main {
|
||||
long sum = 0;
|
||||
for (Map.Entry<String, List<String>> s : strings.entrySet()) {
|
||||
if (!s.getKey().isEmpty()) {
|
||||
long l = s.getValue().stream().filter(sx -> s.getKey().equals(sx)).count();
|
||||
sum += l;
|
||||
long count = s.getValue().stream().filter(sx -> s.getKey().equals(sx)).count();
|
||||
sum += count;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Map<Boolean, List<String>> test(List<String> strings) {
|
||||
Map<Boolean, List<String>> map = new HashMap<>();
|
||||
map.put(false, new ArrayList<>());
|
||||
map.put(true, new ArrayList<>());
|
||||
for (String s : strings) {
|
||||
if (!s.isEmpty()) {
|
||||
map.get(s.length() > 1).add(s);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
Map<Boolean, Map<Character, Set<String>>> map = new HashMap<>();
|
||||
map.put(false, new HashMap<>());
|
||||
map.put(true, new HashMap<>());
|
||||
for (String s : strings) {
|
||||
map.get(s.length() > 2).computeIfAbsent(s.charAt(0), k -> new HashSet<>()).add(s);
|
||||
}
|
||||
System.out.println(map);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbb", "cccc", "dddd"));
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.UnaryOperator;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
Map<Boolean, LinkedHashSet<String>> map = new HashMap<>();
|
||||
map.put(false, new LinkedHashSet<>());
|
||||
map.put(true, new LinkedHashSet<>());
|
||||
for (String s : strings) {
|
||||
map.get(s.length() > 2).add(s);
|
||||
}
|
||||
System.out.println(map);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbb", "cccc", "dddd", "ee", "e", "e"));
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.UnaryOperator;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
Map<Boolean, Map<String, Integer>> map = new HashMap<>();
|
||||
map.put(false, new HashMap<>());
|
||||
map.put(true, new HashMap<>());
|
||||
for (String string : strings) {
|
||||
String s = string.trim();
|
||||
if (map.get(s.length() > 2).put(((UnaryOperator<String>) x -> x).apply(s), s.length()) != null) {
|
||||
throw new IllegalStateException("Duplicate key");
|
||||
}
|
||||
}
|
||||
System.out.println(map);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbb", "cccc", "dddd", "ee", "e", "e"));
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -8,8 +8,8 @@ public class Main {
|
||||
int sum = 0;
|
||||
for (String s : list) {
|
||||
System.out.println(s);
|
||||
int i = s.length();
|
||||
sum += i;
|
||||
int length = s.length();
|
||||
sum += length;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
+2
-2
@@ -9,8 +9,8 @@ public class Main {
|
||||
LongSummaryStatistics stat = new LongSummaryStatistics();
|
||||
for (String s : list) {
|
||||
System.out.println(s);
|
||||
long l = s.length();
|
||||
stat.accept(l);
|
||||
long length = s.length();
|
||||
stat.accept(length);
|
||||
}
|
||||
return stat;
|
||||
}
|
||||
|
||||
+2
-2
@@ -8,8 +8,8 @@ public class Main {
|
||||
private static IntSummaryStatistics test() {
|
||||
IntSummaryStatistics stat = new IntSummaryStatistics();
|
||||
for (Number[] nums : Arrays.<Number[]>asList(new Integer[]{1, 2, 3})) {
|
||||
int i = (int) nums[0];
|
||||
stat.accept(i);
|
||||
int num = (int) nums[0];
|
||||
stat.accept(num);
|
||||
}
|
||||
return stat;
|
||||
}
|
||||
|
||||
+3
-3
@@ -6,12 +6,12 @@ import java.util.stream.IntStream;
|
||||
|
||||
public class Main {
|
||||
private static TreeSet<Integer> test() {
|
||||
TreeSet<Integer> collection = new TreeSet<Integer>();
|
||||
TreeSet<Integer> integers = new TreeSet<>();
|
||||
for (int i : new int[]{4, 2, 1}) {
|
||||
Integer integer = i;
|
||||
collection.add(integer);
|
||||
integers.add(integer);
|
||||
}
|
||||
return collection;
|
||||
return integers;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Map<Integer, String> test(List<String> strings) {
|
||||
Map<Integer, String> map = new HashMap<>();
|
||||
for (String str : strings) {
|
||||
if (map.put(str.length(), str) != null) {
|
||||
throw new IllegalStateException("Duplicate key");
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Map<Integer, String> test(List<String> strings) {
|
||||
Map<Integer, String> map = new HashMap<>();
|
||||
for (String s : strings) {
|
||||
if (!s.isEmpty()) {
|
||||
map.merge(s.length(), s, String::concat);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static TreeMap<Integer, String> test(List<String> strings) {
|
||||
TreeMap<Integer, String> map = new TreeMap<>();
|
||||
for (String s1 : strings) {
|
||||
if (!s1.isEmpty()) {
|
||||
map.merge(s1.length(), s1, (s, string) -> s);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Main {
|
||||
public void test(String... list) {
|
||||
System.out.println(Stream.of(list).filter(Objects::nonNull).col<caret>lect(Collectors.averagingDouble(s -> 1.0/s)));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Main().test("a", "bbb", null, "cc", "dd", "eedasfasdfs");
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Main {
|
||||
public void test(String... list) {
|
||||
System.out.println(Stream.of(list).filter(Objects::nonNull).col<caret>lect(Collectors.averagingInt(String::length)));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Main().test("a", "bbb", null, "cc", "dd", "eedasfasdfs");
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static long test(List<String> strings) {
|
||||
return strings.stream().filter(s -> !s.isEmpty()).coll<caret>ect(Collectors.counting());
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Optional<String> test(List<String> strings) {
|
||||
return strings.stream().filter(s -> !s.isEmpty()).c<caret>ollect(Collectors.maxBy(Comparator.naturalOrder()));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Main<T> {
|
||||
void test() {
|
||||
Integer totalLength = Stream.of("a", "bb", "ccc").co<caret>llect(Collectors.reducing(0, String::length, Integer::sum));
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.DoubleSummaryStatistics;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static DoubleSummaryStatistics test(List<String> strings) {
|
||||
return strings.stream().filter(Objects::nonNull).colle<caret>ct(Collectors.summarizingDouble(str -> str.length()/2.0));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList(null, null)));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Double test(List<String> strings) {
|
||||
return strings.stream().filter(Objects::nonNull).co<caret>llect(Collectors.summingDouble(String::length));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList(null, null)));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public void test(List<String> list) {
|
||||
System.out.println(list.stream().map(list.size() < 10 ? String::trim : Function.identity()).colle<caret>ct(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> list) {
|
||||
System.out.println(list.stream()
|
||||
.filter(x -> x != null)
|
||||
.flatMap(s -> IntStream.range(0, 10).boxed().filter(Predicate.isEqual(s.length())))
|
||||
.co<caret>llect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbbb", "cccccccccc", "dd", ""));
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.SplittableRandom;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
int n1 = Stream.generate(() -> 500).limit(100).r<caret>educe(0, new SplittableRandom(1)::nextInt, Integer::sum);
|
||||
System.out.println(n1);
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Map<Integer, List<String>> test(List<String> strings) {
|
||||
return strings.stream().col<caret>lect(Collectors.groupingBy(str -> str.length()));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee")));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Map<Integer, List<String>> test(List<String> strings, int k) {
|
||||
return strings.stream().col<caret>lect(Collectors.groupingBy(String::length));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee")));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
private static TreeMap<Integer, LinkedHashSet<String>> getMap(List<String> strings) {
|
||||
return strings.stream().coll<caret>ect(
|
||||
Collectors.groupingBy(String::length, () -> new TreeMap<>(Comparator.reverseOrder()), Collectors.toCollection(LinkedHashSet::new)));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(getMap(Arrays.asList("a", "bbb", "cccc", "dddd", "ee", "e")));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
System.out.println(strings.stream().coll<caret>ect(Collectors.groupingBy(String::length, Collectors.groupingBy(s -> s.charAt(0), Collectors.toSet()))));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbb", "cccc", "dddd"));
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
private static TreeMap<Integer, List<Integer>> getMap(List<String> strings) {
|
||||
return strings.stream().colle<caret>ct(
|
||||
Collectors.groupingBy(String::length, () -> new TreeMap<>(Comparator.reverseOrder()),
|
||||
Collectors.mapping(String::length, Collectors.mapping(len -> len*2, Collectors.toList()))));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(getMap(Arrays.asList("a", "bbb", "cccc", "dddd", "ee", "e")));
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
System.out.println(strings.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.co<caret>llect(Collectors.groupingBy(String::length, Collectors.summarizingDouble(String::length))));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbb", "cccc", "dddd"));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
System.out.println(strings.stream().co<caret>llect(Collectors.groupingBy(String::length, Collectors.toMap(s -> s.charAt(0), Function.identity()))));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbb", "cccc", "dddd"));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
System.out.println(strings.stream().filter(Objects::nonNull).col<caret>lect(Collectors.groupingBy(String::length, Collectors.toSet())));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbb", "cccc", "dddd"));
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
private static void getMap(List<String> strings) {
|
||||
System.out.println(strings.stream().co<caret>llect(Collectors.mapping(String::length, Collectors.mapping(len -> len*2, Collectors.toList()))));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
getMap(Arrays.asList("a", "bbb", "cccc", "dddd", "ee", "e"));
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public static String test(List<String> strings) {
|
||||
return strings.stream().m<caret>ax(Comparator.comparing(String::length)).orElse(null);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee")));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public static double test(List<String> strings) {
|
||||
return strings.stream().mapToDouble(String::length).m<caret>ax().orElse(-1);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d")));
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public static String test(List<String> strings, Comparator<String> cmp) {
|
||||
return strings.stream().m<caret>in(cmp).orElse(null);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList(), Comparator.comparing(String::length)));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee"), Comparator.comparing(String::length)));
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public static String test(List<String> strings, Comparator<CharSequence> comparator) {
|
||||
return strings.stream().m<caret>in(comparator.reversed()).orElse(null);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList(), Comparator.comparing(CharSequence::length)));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee"), Comparator.comparing(CharSequence::length)));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
public static int test(List<String> strings) {
|
||||
return strings.stream().mapToInt(String::length).mi<caret>n().orElse(-1);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d")));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Map<Boolean, List<String>> test(List<String> strings) {
|
||||
return strings.stream().filter(s -> !s.isEmpty()).co<caret>llect(Collectors.partitioningBy(s -> s.length() > 1));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
System.out.println(strings.stream().c<caret>ollect(Collectors.partitioningBy((String s) -> s.length() > 2, Collectors.groupingBy(s -> s.charAt(0), Collectors.toSet()))));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbb", "cccc", "dddd"));
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.function.UnaryOperator;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
System.out.println(strings.stream().co<caret>llect(Collectors.partitioningBy(s -> s.length() > 2, Collectors.toCollection(LinkedHashSet::new))));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbb", "cccc", "dddd", "ee", "e", "e"));
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.function.UnaryOperator;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static void test(List<String> strings) {
|
||||
System.out.println(strings.stream().map(x -> x.trim()).colle<caret>ct(Collectors.partitioningBy(s -> s.length() > 2,
|
||||
Collectors.toMap(s -> ((UnaryOperator<String>) x -> x).apply(s), String::length))));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
test(Arrays.asList("a", "bbb", "cccc", "dddd", "ee", "e", "e"));
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Map<Integer, String> test(List<String> strings) {
|
||||
return strings.stream()
|
||||
.co<caret>llect(Collectors.toMap(String::length, str -> str));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static Map<Integer, String> test(List<String> strings) {
|
||||
return strings.stream().filter(s -> !s.isEmpty())
|
||||
.coll<caret>ect(Collectors.toMap(String::length, Function.identity(), String::concat));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Main {
|
||||
public static TreeMap<Integer, String> test(List<String> strings) {
|
||||
return strings.stream().filter(s -> !s.isEmpty())
|
||||
.col<caret>lect(Collectors.toMap(String::length, s -> s, (s, string) -> s, TreeMap::new));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(test(Arrays.asList()));
|
||||
System.out.println(test(Arrays.asList("a", "bbb", "cc", "d", "eee", "")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user