2017-06-04 65 views

回答

4

Arrays.stream將通過各int[]int[][]。 您可以將int[]轉換爲IntStream。 然後,爲了將int s的流轉換爲List<Integer>, ,您首先需要將它們裝箱。 一旦裝箱到Integer s,您可以將它們收集到一個列表中。 最後將List<Integer>的流收集到列表中。

List<List<Integer>> list = Arrays.stream(data) 
    .map(row -> IntStream.of(row).boxed().collect(Collectors.toList())) 
    .collect(Collectors.toList()); 

Demo.

+0

沒有理由將代碼更改爲'IntStream.of'; 'Arrays.stream'工作正常。 OP的代碼中缺少的是調用'boxed()'... – Holger

0

嘗試StreamEx

StreamEx.of(data).map(a -> IntStreamEx.of(a).boxed().toList()).toList(); 

或者AbacusUtil,簡單和最有效的:

Stream.of(data).map(a -> IntList.of(a).boxed()).toList(); 

// Or: 
Seq.of(data).map(a -> IntList.of(a).boxed()); 

他們都是空安全。如果data爲空,則返回一個空列表

相關問題