我的问题基本上归结为将List
简化为链表,但从reduce函数推断的类型似乎不正确。
我的列表看起来像这样
[0, 1, 2]
我希望reduce函数在每个reduce步骤都这样做
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
然而,reduce函数似乎认为这不会工作,因为我猜它不认为身份是一个节点。
这是我的代码。
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();
}
}
我做错了什么?
编辑在接受的答案之后,我的代码看起来像这样:
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();
}
}
现在输出
Will happen
Will happen
Will happen
Node{value=2, next=Node{value=1, next=Node{value=0, next=null}}}
我仍然不知道为什么Java不能告诉第三个参数的减少函数是不必要的,它永远不会被调用,但这是另一天的问题。
第二个编辑
可以为这样的reduce操作创建一个新方法,因为reduce的第三个参数可以是一个什么都不做的函数。
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));
}
您可以使用另一个减少操作符,执行
static Node reverse(List<Integer> list) {
return list.stream()
.reduce(
(Node) null, //the empty element
(n, i) -> new Node(i, n) , //combining a Node and an Integer
(n1, n2) -> new Node(n1.value, n2)); // could be anything
}
编辑:使它与parallelStream
工作:
public static Node merge(Node n1, Node n2) {
if (n1 == null) {
return n2;
} else {
return new Node(n1.value, merge(n1.next, n2));
}
}
static Node reverse(List<Integer> list) {
return list.stream()
.reduce(
(Node) null, //the empty element
(n, i) -> new Node(i, n) , //combining a Node and an Integer
(n1, n2) -> merge(n1, n2)); // combining two Nodes
}
问题是期望reduce返回它所累积的相同类型。在这种情况下,null
是Integer
, a
也是
你可以做的是将每个Integer
映射到一个Node
,然后将节点减少到一个链表。
static Node reverse(List<Integer> list) {
return list.stream()
.map(i -> new Node(i, null))
.reduce(null, (a, b) -> {
b.next = a;
return b;
});
}
void run() {
List<Integer> list = IntStream.range(0, 3)
.boxed()
.collect(Collectors.toList());
Node reversed = reverse(list);
for(Node n = reversed; n != null ; n = n.next)
System.out.println(n.value);
}
打印
2
1
0