用Java 8实现集合的Cartesian产品

sta*_*ror 8 java generics collections java-8 java-stream

现在我只能实现两个集合的笛卡尔积,这里是代码:

public static <T1, T2, R extends Collection<Pair<T1, T2>>>
R getCartesianProduct(
        Collection<T1> c1, Collection<T2> c2,
        Collector<Pair<T1, T2>, ?, R> collector) {
    return c1.stream()
            .flatMap(e1 -> c2.stream().map(e2 -> new Pair<>(e1, e2)))
            .collect(collector);
}
Run Code Online (Sandbox Code Playgroud)

这段代码在IntelliJ中工作正常,但在Eclipse中没有(编译器合规级别为1.8):

The method collect(Collector<? super Object,A,R>) 
in the type Stream<Object> is not applicable for 
the arguments (Collector<Pair<T1,T2>,capture#5-of ?,R>)
Run Code Online (Sandbox Code Playgroud)

这是Pair.java:

public class Pair<T1, T2> implements Serializable {
    protected T1 first;
    protected T2 second;
    private static final long serialVersionUID = 1360822168806852921L;

    public Pair(T1 first, T2 second) {
        this.first = first;
        this.second = second;
    }

    public Pair(Pair<T1, T2> pair) {
        this(pair.getFirst(), pair.getSecond());
    }

    public T1 getFirst() {
        return this.first;
    }

    public T2 getSecond() {
        return this.second;
    }

    public void setFirst(T1 o) {
        this.first = o;
    }

    public void setSecond(T2 o) {
        this.second = o;
    }

    public String toString() {
        return "(" + this.first + ", " + this.second + ")";
    }

    @Override
    public boolean equals(Object o) {
        if(!(o instanceof Pair))
            return false;
        Pair p = (Pair) o;
        if(!this.first.equals(p.first))
            return false;
        if(!this.second.equals(p.second))
            return false;
        return true;

    }

    @Override
    public int hashCode() {
        int hash = 1;
        hash = hash * 31 + this.first.hashCode();
        hash = hash * 31 + this.second.hashCode();
        return hash;
    }

}
Run Code Online (Sandbox Code Playgroud)

如何解决这个错误?

是否有一种优雅的方式来实现几个集合的笛卡尔积?(假设我们上课tuple)

Mis*_*sha 8

Eclipse在类型推断方面存在问题.如果添加类型提示 .<Pair<T1,T2>>flatMap,则编译正常.

如果我可以提出不同的方法,请考虑让您的cartesianProduct不要完成整个流和集合,而只是作为帮助flatMap:

static <T1, T2, R> Function<T1, Stream<R>> crossWith(
         Supplier<? extends Stream<T2>> otherSup, 
         BiFunction<? super T1, ? super T2, ? extends R> combiner
) {
    return t1 -> otherSup.get().map(t2 -> combiner.apply(t1, t2));
}
Run Code Online (Sandbox Code Playgroud)

现在你只需创建一个Pairif你想要的结果包含Pairs,你可以通过flatMap多次应用来做一个更高阶的笛卡尔积:

List<String> letters = Arrays.asList("A", "B", "C");
List<Integer> numbers = Arrays.asList(1, 2, 3);

List<Pair<String, Integer>> board = letters.stream()
                .flatMap(crossWith(numbers::stream, Pair::new))
                .collect(toList());


List<String> ops = Arrays.asList("+", "-", "*", "/");

List<String> combinations = letters.stream()
                .flatMap(crossWith(ops::stream, String::concat))
                .flatMap(crossWith(letters::stream, String::concat))
                .collect(toList());   // triple cartesian product
Run Code Online (Sandbox Code Playgroud)