我正在Java SE 8中使用惰性函数操作,我想将一个索引I映射到一个对/元组(I,值[I]),然后根据第二个值[I]元素进行筛选,最后只输出索引。
我还必须忍受这个问题:Java中的c++ Pair<L,R>等价于什么?在大胆的新时代的lambdas和溪流?
更新:我给出了一个相当简单的例子,在下面的一个答案中,@dkatzel提供了一个简洁的解决方案。然而,它并不能一概而论。因此,让我补充一个更普遍的例子:
package com.example.test;
import java.util.ArrayList;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
boolean [][] directed_acyclic_graph = new boolean[][]{
{false, true, false, true, false, true},
{false, false, false, true, false, true},
{false, false, false, true, false, true},
{false, false, false, false, false, true},
{false, false, false, false, false, true},
{false, false, false, false, false, false}
};
System.out.println(
IntStream.range(0, directed_acyclic_graph.length)
.parallel()
.mapToLong(i -> IntStream.range(0, directed_acyclic_graph[i].length)
.filter(j -> directed_acyclic_graph[j][i])
.count()
)
.filter(n -> n == 0)
.collect(() -> new ArrayList<Long>(), (c, e) -> c.add(e), (c1, c2) -> c1.addAll(c2))
);
}
}
这给出了不正确的输出[0,0,0],它对应于三列的计数都是假的。我需要的是这三列的下标。正确的输出应该是[0,2,4]。我怎样才能得到这个结果?
遗憾的是,Java 8没有引入对或元组。当然,您总是可以使用org.apache.commons.lang3.tuple(我个人在与Java 8结合使用时使用它),或者您可以创建自己的包装器。或者使用地图。或者类似的东西,就像你链接的那个问题的公认答案所解释的那样。
更新:JDK 14将记录类作为预览特性引入,JDK 16将它们作为标准语言特性引入。它们不是元组,但可以用来解决许多相同的问题。在上面的具体例子中,可能是这样的:
public class Jdk14Example {
record CountForIndex(int index, long count) {}
public static void main(String[] args) {
boolean [][] directed_acyclic_graph = new boolean[][]{
{false, true, false, true, false, true},
{false, false, false, true, false, true},
{false, false, false, true, false, true},
{false, false, false, false, false, true},
{false, false, false, false, false, true},
{false, false, false, false, false, false}
};
System.out.println(
IntStream.range(0, directed_acyclic_graph.length)
.parallel()
.mapToObj(i -> {
long count = IntStream.range(0, directed_acyclic_graph[i].length)
.filter(j -> directed_acyclic_graph[j][i])
.count();
return new CountForIndex(i, count);
}
)
.filter(n -> n.count == 0)
.collect(() -> new ArrayList<CountForIndex>(), (c, e) -> c.add(e), (c1, c2) -> c1.addAll(c2))
);
}
}
当使用——enable-preview标志或JDK 16或更高版本编译并运行JDK 14时,你会得到以下结果:
[CountForIndex[index=0, count=0], CountForIndex[index=2, count=0], CountForIndex[index=4, count=0]]
遗憾的是,Java 8没有引入对或元组。当然,您总是可以使用org.apache.commons.lang3.tuple(我个人在与Java 8结合使用时使用它),或者您可以创建自己的包装器。或者使用地图。或者类似的东西,就像你链接的那个问题的公认答案所解释的那样。
更新:JDK 14将记录类作为预览特性引入,JDK 16将它们作为标准语言特性引入。它们不是元组,但可以用来解决许多相同的问题。在上面的具体例子中,可能是这样的:
public class Jdk14Example {
record CountForIndex(int index, long count) {}
public static void main(String[] args) {
boolean [][] directed_acyclic_graph = new boolean[][]{
{false, true, false, true, false, true},
{false, false, false, true, false, true},
{false, false, false, true, false, true},
{false, false, false, false, false, true},
{false, false, false, false, false, true},
{false, false, false, false, false, false}
};
System.out.println(
IntStream.range(0, directed_acyclic_graph.length)
.parallel()
.mapToObj(i -> {
long count = IntStream.range(0, directed_acyclic_graph[i].length)
.filter(j -> directed_acyclic_graph[j][i])
.count();
return new CountForIndex(i, count);
}
)
.filter(n -> n.count == 0)
.collect(() -> new ArrayList<CountForIndex>(), (c, e) -> c.add(e), (c1, c2) -> c1.addAll(c2))
);
}
}
当使用——enable-preview标志或JDK 16或更高版本编译并运行JDK 14时,你会得到以下结果:
[CountForIndex[index=0, count=0], CountForIndex[index=2, count=0], CountForIndex[index=4, count=0]]