用java集合写产品信息_用Java 8实现集合的Cartesian产品

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

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;

}

}

如何解决此错误?

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值