收集多维数组的列来设置
collecting column of multidimensional array to set
我有一个属性 this.sudoku
,它是一个 int[9][9]
数组。
我需要将其中的一列放入集合中。
Set<Integer> sudoku_column = IntStream.range(0, 9)
.map(i -> this.sudoku[i][column])
.collect(Collectors.toSet());
我希望此集合中有一个列值。但它说 Collectors.toSet()
不能应用于链中的这个 collect 函数。有人可以解释为什么吗?
IntStream#map
consumes an IntUnaryOperator
which represents an operation on a single int-valued operand that produces an int-valued result thus the result is an IntStream
, however IntStream
does not have the collect
overload you're attempt to use, which means you have a couple of options; i.e. either use IntStream#collect
:
IntStream.range(0, 9)
.collect(HashSet::new, (c, i) -> c.add(sudoku[i][column]), HashSet::addAll);
或使用 mapToObj
将 IntStream
转换为 Stream<Integer>
,然后您可以调用 .collect(Collectors.toSet())
。
IntStream.range(0, 9)
.mapToObj(i -> this.sudoku[i][column])
.collect(Collectors.toSet());
IntStream#map
takes an IntUnaryOperator
是一个将 int
转换为另一个 int
的函数。
如果您想继续 IntStream
. But if you need to collect the stream into a Set<Integer>
, you need to turn your IntStream
into a stream of boxed int
s, a Stream<Integer>
, by IntStream#boxed
也没关系。
.map(i -> this.sudoku[i][column])
.boxed()
.collect(Collectors.toSet());
Collectors.toSet()
cannot be applied to this collect
function in the chain
Collectors.toSet()
return 一个 Collector
不符合 IntStream
中单个 collect(Supplier, ObjIntConsumer, BiConsumer)
方法的签名。不过,它适合 Stream.collect(Collector)
.
我有一个属性 this.sudoku
,它是一个 int[9][9]
数组。
我需要将其中的一列放入集合中。
Set<Integer> sudoku_column = IntStream.range(0, 9)
.map(i -> this.sudoku[i][column])
.collect(Collectors.toSet());
我希望此集合中有一个列值。但它说 Collectors.toSet()
不能应用于链中的这个 collect 函数。有人可以解释为什么吗?
IntStream#map
consumes an IntUnaryOperator
which represents an operation on a single int-valued operand that produces an int-valued result thus the result is an IntStream
, however IntStream
does not have the collect
overload you're attempt to use, which means you have a couple of options; i.e. either use IntStream#collect
:
IntStream.range(0, 9)
.collect(HashSet::new, (c, i) -> c.add(sudoku[i][column]), HashSet::addAll);
或使用 mapToObj
将 IntStream
转换为 Stream<Integer>
,然后您可以调用 .collect(Collectors.toSet())
。
IntStream.range(0, 9)
.mapToObj(i -> this.sudoku[i][column])
.collect(Collectors.toSet());
IntStream#map
takes an IntUnaryOperator
是一个将 int
转换为另一个 int
的函数。
如果您想继续 IntStream
. But if you need to collect the stream into a Set<Integer>
, you need to turn your IntStream
into a stream of boxed int
s, a Stream<Integer>
, by IntStream#boxed
也没关系。
.map(i -> this.sudoku[i][column])
.boxed()
.collect(Collectors.toSet());
Collectors.toSet()
cannot be applied to thiscollect
function in the chain
Collectors.toSet()
return 一个 Collector
不符合 IntStream
中单个 collect(Supplier, ObjIntConsumer, BiConsumer)
方法的签名。不过,它适合 Stream.collect(Collector)
.