Java) Stream.of / Arrays.stream / Integer <-> int
This post was migrated from Tistory. You can find the original here.
Stream
Stream.of/Arrays.stream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int[] a = {1,2,3};
int[] b = {4,5};
String a = "hi my name is";
String[] b = a.split(" +");
Stream<String> b1 = Stream.of(b);
//for reference
IntStream stream = Arrays.stream(a);
Stream<int[]> a1 = Stream.of(a);
int[] c2 = Stream.of(a, b).flatMapToInt((v)->{
System.out.println(Arrays.toString(v));
return Arrays.stream(v);
}).toArray();
System.out.println(Arrays.toString(c));
result
[1, 2, 3]
[4, 5]
[1, 2, 3, 4, 5]
+)
1
2
3
4
Stream<String> b1 = Stream.of(b);
Stream<String> stream = Arrays.stream(b);
Stream<Stream<String>> streamStream = stream.map(v -> Stream.of(v));
Stream<String> stringStream = stream.flatMap(v -> Stream.of(v));
Arrays.stream looks like this: public static <T> Stream<T> stream(T[] array).
So int[] -> Stream<int>(=IntStream).
Stream.of looks like this: public static<T> Stream<T> of(T t).
So int[] -> Stream<int[]>.
+) Meanwhile String[] -> Stream<String>.
The difference is whether the stream has one layer of the array peeled off, or whether the array itself becomes the stream as-is.
flatMap/flatMapToInt
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
If the lambda expression returns the input T as a Stream<T>, flatMap flattens all of those returned Stream<T>s into a single Stream<T>.
IntStream flatMapToInt(Function<? super T, ? extends IntStream> mapper);
flatMapToInt directly returns all the IntStreams combined into one IntStream.
Integer[][] -> int[][]
1
2
3
4
5
6
7
Integer[][] input = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] output = Arrays.stream(input)
.map(row -> Arrays.stream(row)
.mapToInt(Integer::intValue)
.toArray())
.toArray(int[][]::new);
You need to peel off one layer and apply mapToInt inside it before the final toArray(int[][]::new) can work.
The mapToInt lambda has to produce an int.
1
2
Integer[][] test = new Integer[2][2];
System.out.println(Arrays.deepToString(test)); //[[null, null], [null, null]]
Watch out! The default initial value of Integer is null.
int[] -> Integer[]
1
2
3
4
5
6
int[] a = {1,2,3,4};
Integer[] b = Arrays.stream(a).boxed().toArray(Integer[]::new)
//for reference
IntStream stream = Arrays.stream(a);
Stream<Integer> boxed = Arrays.stream(a).boxed();
Convert the IntStream to a Stream<Integer> first, then proceed.
1
2
3
4
5
Integer[] b = Arrays.stream(a).boxed().toArray(Integer[]::new);
//Integer[] b = Arrays.stream(a).boxed().toArray(new Integer[]{}); Err
ArrayList<Integer> c = new ArrayList<Integer>();
c.toArray(new Integer[]{});
(1) (ArrayList).toArray and (2) (stream).toArray are different.
(1) also accepts an object instance as an argument, but (2) takes an IntFunction as its argument.
+α
You can keep following a static method by ctrl+clicking on it.
Is there a way to jump to the implementing class instead of just the interface?
e.g. Following stream-related methods only ever leads to the interface.
blabla
When solving algorithm problems in Java,
just convert the input to Wrapper types first, solve the problem, and convert back to the desired form at the final return.