Right now I can only implement the Cartesian product of two collections, here is the code:
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);
}
This code works fine in IntelliJ, but not in Eclipse. Both with compiler compliance level of 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>)
Here is 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;
}
}
How to fix this error?
Is there an elegant way to implement the Cartesian product of several collections? Suppose we have class tuple
.