现在我只能实现两个集合的笛卡尔积,这里是代码:
public static >>
R getCartesianProduct(
Collection c1, Collection c2,
Collector, ?, R> collector) {
return c1.stream()
.flatMap(e1 -> c2.stream().map(e2 -> new Pair<>(e1, e2)))
.collect(collector);
}
此代码在IntelliJ中工作正常,但在Eclipse中没有(编译器合规性级别为1.8):
The method collect(Collector super Object,A,R>)
in the type Stream is not applicable for
the arguments (Collector,capture#5-of ?,R>)
这是Pair.java:
public class Pair 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 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;
}
}
如何解决此错误?
是否有一种优雅的方式来实现几个集合的笛卡尔积? (假设我们有类元组)