Does Java SE 8 have Pairs or Tuples?

前端 未结 9 861
独厮守ぢ
独厮守ぢ 2020-11-28 18:37

I am playing around with lazy functional operations in Java SE 8, and I want to map an index i to a pair / tuple (i, value[i]), then <

9条回答
  •  遥遥无期
    2020-11-28 19:25

    It appears that the full example can be solved without the use of any kind of Pair structure. The key is to filter on the column indexes, with the predicate checking the entire column, instead of mapping the column indexes to the number of false entries in that column.

    The code that does this is here:

        System.out.println(
            IntStream.range(0, acyclic_graph.length)
                .filter(i -> IntStream.range(0, acyclic_graph.length)
                                      .noneMatch(j -> acyclic_graph[j][i]))
                .boxed()
                .collect(toList()));
    

    This results in output of [0, 2, 4] which is I think the correct result requested by the OP.

    Also note the boxed() operation that boxes the int values into Integer objects. This enables one to use the pre-existing toList() collector instead having to write out collector functions that do the boxing themselves.

提交回复
热议问题