我的問題基本上歸結爲將List
減少到鏈接列表,但從reduce函數推斷的類型似乎不正確。Java 8減少列表到鏈接列表
我的名單看起來是這樣
[0, 1, 2]
我預計減少功能在每次做這個簡步驟
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
然而,減少功能似乎認爲這是行不通的因爲我猜想它不認爲身份是一個節點。
這是我的代碼。
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函數的第三個參數是不必要的,它不會被調用,但這是另一個問題。
第二個編輯
這可能只是創建一個新的方法用於減少這樣的操作,因爲減少的第三個參數可以只是一個什麼也不做的函數。
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));
}
爲什麼不使用'收集'而不是'減少'? 'collect'更適合從Stream創建Collection。 – Eran
@Eran我不喜歡它需要我實現3種方法 – michaelsnowden
@michaelsnowden嘗試並行運行'Stream',然後您需要組合器。 – Flown