My problem basically boils down to reducing a List
into a linked list, but the inferred types from the reduce function don't seem right.
My list will look like this
[0, 1, 2]
I expect the reduce function to do this at each reduce step
null // identity (a Node)
Node(0, null) // Node a = null, int b = 0
Node(1, Node(0, null)) // Node a = Node(0, null), int b = 1
Node(2, Node(1, Node(0, null))) // Node a = Node(1, Node(0, null)), int b = 2
However, the reduce function seems to think that this won't work because I guess it doesn't think the identity is a Node.
Here is my code.
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Example {
static class Node {
int value;
Node next;
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
}
static Node reverse(List<Integer> list) {
return list.stream()
.reduce(null, (a, b) -> new Node(b, a)); // error: thinks a is an integer
}
void run() {
List<Integer> list = IntStream.range(0, 3)
.boxed()
.collect(Collectors.toList());
Node reversed = reverse(list);
}
public static void main(String[] args) {
new Example().run();
}
}
What am I doing wrong?
EDIT After the accepted answer, my code looks like this:
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Example {
static class Node {
int value;
Node next;
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
", next=" + next +
'}';
}
}
static Node reverse(List<Integer> list) {
return list.stream()
.reduce(null, (n, i) -> {
System.out.println("Will happen"); // to demonstrate that this is called
return new Node(i, n);
}, (n1, n2) -> {
System.out.println("Won't happen"); // and this never is
return new Node(n1.value, n2);
});
}
void run() {
List<Integer> list = IntStream.range(0, 3)
.boxed()
.collect(Collectors.toList());
Node reversed = reverse(list);
System.out.println(reversed);
}
public static void main(String[] args) {
new Example().run();
}
}
And it now prints
Will happen
Will happen
Will happen
Node{value=2, next=Node{value=1, next=Node{value=0, next=null}}}
I still don't know why Java can't tell that third argument to the reduce function is unnecessary and it will never get called, but that's a question for another day.
Second Edit
It's possible to just create a new method for reduce operations like this because the third argument to reduce can just be a function that does nothing.
static <T, U> U reduce(Stream<T> stream, U identity, BiFunction<U, ? super T, U> accumulator) {
return stream.reduce(identity, accumulator, (a, b) -> null);
}
static Node reverse(List<Integer> list) {
return reduce(list.stream(), null, (n, i) -> new Node(i, n));
}