我已經轉換一個二維int數組到流:如何按升序對IntStream進行排序?
IntStream dataStream = Arrays.stream(data).flatMapToInt(x -> Arrays.stream(x));
現在,我要對列表進行排序升序排列。我已經試過這樣:
dataStream.sorted().collect(Collectors.toList());
我感到困惑這一點,因爲在例子我見過,類似的事情都沒有錯誤完成。
我已經轉換一個二維int數組到流:如何按升序對IntStream進行排序?
IntStream dataStream = Arrays.stream(data).flatMapToInt(x -> Arrays.stream(x));
現在,我要對列表進行排序升序排列。我已經試過這樣:
dataStream.sorted().collect(Collectors.toList());
我感到困惑這一點,因爲在例子我見過,類似的事情都沒有錯誤完成。
與
dataStream.sorted().boxed().collect(Collectors.toList());
因爲collect(Collectors.toList())
嘗試並不適用於IntStream
。
我也認爲這應該稍微好一點,首先表現要求sorted()
,然後boxed()
。
IntStream.collect()
方法具有以下特徵:
<R> R collect(Supplier<R> supplier,
ObjIntConsumer<R> accumulator,
BiConsumer<R, R> combiner);
如果你真的想用這個你可以:
.collect(IntArrayList::new, MutableIntList::add, MutableIntList::addAll);
由於這裏建議:
的問題是你正試圖轉換一個int
流到列表,但Collectors.toList
只適用於對象流,不適用於基元流。
你需要收集入名單前框數組:
dataStream.sorted().boxed().collect(Collectors.toList());