mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-08-26 00:11:26 +07:00
IDEA-165397 Replace stream API with loop: support lambdas as toArray argument
This commit is contained in:
+16
@@ -0,0 +1,16 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.*;
|
||||
|
||||
public class Main {
|
||||
private static <A> A[] toArraySkippingNulls(List<?> list, IntFunction<A[]> generator) {
|
||||
List<Object> result = new ArrayList<>();
|
||||
for (Object o : list) {
|
||||
if (Objects.nonNull(o)) {
|
||||
result.add(o);
|
||||
}
|
||||
}
|
||||
return result.toArray(generator.apply(0));
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
private static Number[] test(int[] numbers) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
for (int number : numbers) {
|
||||
Integer integer = number;
|
||||
list.add(integer);
|
||||
}
|
||||
return list.toArray(new Integer[0]);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(Arrays.asList(test(new int[] {1,2,3})));
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.*;
|
||||
|
||||
public class Main {
|
||||
private static <A> A[] toArraySkippingNulls(List<?> list, IntFunction<A[]> generator) {
|
||||
return list.stream().filter(Objects::nonNull).toAr<caret>ray(generator);
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// "Replace Stream API chain with loop" "true"
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Main {
|
||||
private static Number[] test(int[] numbers) {
|
||||
return Arrays.stream(numbers).boxed().toArr<caret>ay(size -> new Integer[size]);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(Arrays.asList(test(new int[] {1,2,3})));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user