[英]java.util.stream.Collectors: Why is the summingInt implemented with an array?
標准收集器summingInt
內部創建一個長度為1的數組:
public static <T> Collector<T, ?, Integer>
summingInt(ToIntFunction<? super T> mapper) {
return new CollectorImpl<>(
() -> new int[1],
(a, t) -> { a[0] += mapper.applyAsInt(t); },
(a, b) -> { a[0] += b[0]; return a; },
a -> a[0], CH_NOID);
}
我想知道是否不可能只定義:
private <T> Collector<T, Integer, Integer> summingInt(ToIntFunction<? super T> mapper) {
return Collector.of(
() -> 0,
(a, t) -> a += mapper.applyAsInt(t),
(a, b) -> a += b,
a -> a
);
}
然而這不起作用,因為累加器似乎被忽略了。 誰能解釋這種行為?
Integer
是不可變的,而Integer[]
數組是可變的。 累加器應該是有狀態的。
想象一下,你有2個引用2個Integer
對象。
Integer a = 1;
Integer b = 2;
本質上,您引用的實例是不可變的:一旦創建它們就無法修改它們。
Integer a = 1; // {Integer@479}
Integer b = 2; // {Integer@480}
你已決定使用a
作為累加器。
a += b;
值a
目前持股滿足你。 這是3
。 但是, a
不再是指您在開始時曾經擁有的{Integer@479}
。
我將調試語句添加到Collector
並使事情變得清晰。
public static <T> Collector<T, Integer, Integer> summingInt(ToIntFunction<? super T> mapper) {
return Collector.of(
() -> {
Integer zero = 0;
System.out.printf("init [%d (%d)]\n", zero, System.identityHashCode(zero));
return zero;
},
(a, t) -> {
System.out.printf("-> accumulate [%d (%d)]\n", a, System.identityHashCode(a));
a += mapper.applyAsInt(t);
System.out.printf("<- accumulate [%d (%d)]\n", a, System.identityHashCode(a));
},
(a, b) -> a += b,
a -> a
);
}
如果你使用它,你會發現一個類似的模式
init [0 (6566818)]
-> accumulate [0 (6566818)]
<- accumulate [1 (1029991479)]
-> accumulate [0 (6566818)]
<- accumulate [2 (1104106489)]
-> accumulate [0 (6566818)]
<- accumulate [3 (94438417)]
其中0 (6566818)
沒有被改變,盡管所有使用+=
中止嘗試。
如果您將其重寫為使用AtomicInteger
public static <T> Collector<T, AtomicInteger, AtomicInteger> summingInt(ToIntFunction<? super T> mapper) {
return Collector.of(
() -> {
AtomicInteger zero = new AtomicInteger();
System.out.printf("init [%d (%d)]\n", zero.get(), System.identityHashCode(zero));
return zero;
},
(a, t) -> {
System.out.printf("-> accumulate [%d (%d)]\n", a.get(), System.identityHashCode(a));
a.addAndGet(mapper.applyAsInt(t));
System.out.printf("<- accumulate [%d (%d)]\n", a.get(), System.identityHashCode(a));
},
(a, b) -> { a.addAndGet(b.get()); return a;}
);
}
你會看到一個真正的累加器(作為可變減少的一部分)在行動
init [0 (1494279232)]
-> accumulate [0 (1494279232)]
<- accumulate [1 (1494279232)]
-> accumulate [1 (1494279232)]
<- accumulate [3 (1494279232)]
-> accumulate [3 (1494279232)]
<- accumulate [6 (1494279232)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.