我想求两个Collection的交集,源代码如下:CollectionmsgCollection=getMessageCollection();CollectionothMsgCollection=othRouter.getMessageCollection();Colle...
我想求两个Collection的交集,源代码如下:
Collection msgCollection = getMessageCollection();
Collection othMsgCollection = othRouter.getMessageCollection();
Collection intersectionMsgCollection = this.intersection(msgCollection,othMsgCollection);
public Collection intersection(Collection collection1, Collection collection2) {
//Collection collection = new Collection(); //an error?
Collection collection = new ArrayList(); //How would it be??
for (T t : collection1) {
if(collection2.contains(t)) {
collection.add(t);
}
}
return collection;
}
但结果不对,举例如下:
[M116, M140, M250, M120, M98]
[M116, M140, M250, M187, M98, M120]
[]
我想 intersection()有问题,求指点!
展开