Java求两Collection的交集
我想求两个Collection的交集,源代码如下:Collection<Message>msgCollection=getMessageCollection();Coll...
我想求两个Collection的交集,源代码如下:
Collection<Message> msgCollection = getMessageCollection();
Collection<Message> othMsgCollection = othRouter.getMessageCollection();
Collection<Message> intersectionMsgCollection = this.intersection(msgCollection,othMsgCollection);
public <T> Collection<T> intersection(Collection<T> collection1, Collection<T> collection2) {
//Collection<T> collection = new Collection<T>(); //an error?
Collection<T> collection = new ArrayList<T>(); //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()有问题,求指点! 展开
Collection<Message> msgCollection = getMessageCollection();
Collection<Message> othMsgCollection = othRouter.getMessageCollection();
Collection<Message> intersectionMsgCollection = this.intersection(msgCollection,othMsgCollection);
public <T> Collection<T> intersection(Collection<T> collection1, Collection<T> collection2) {
//Collection<T> collection = new Collection<T>(); //an error?
Collection<T> collection = new ArrayList<T>(); //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()有问题,求指点! 展开
3个回答
展开全部
intersection()没问题。使用1.7测试代码如下:
import java.util.ArrayList;
import java.util.Collection;
public class Test {
public static void main(String[] args) {
String[] arr1 = {"M116","M140","M250","M120","M98"};
String[] arr2 = {"M116","M140","M250","M187","M98","M120"};
Collection<String> collection1 = toCollection(arr1);
Collection<String> collection2 = toCollection(arr2);
Collection<String> collection = intersection(collection1, collection2);
for (String s : collection) {
System.out.println(s);
}
}
public static <T> Collection<T> intersection(Collection<T> collection1,
Collection<T> collection2) {
Collection<T> collection = new ArrayList<T>(); // How would it be??
for (T t : collection1) {
if (collection2.contains(t)) {
collection.add(t);
}
}
return collection;
}
public static <T>Collection<T> toCollection(T[] arr){
Collection<T> collection = new ArrayList<T>();
for (T t : arr) {
collection.add(t);
}
return collection;
}
}
追问
谢谢您的回答。我刚才测了下,的确没问题。
看来问题在这,我的msgCollection是这样得来的:(区别在于String与HashMap???)
private HashMap messages;
messages.values()
在线等进一步指点!
追答
不明白什么意思。
展开全部
不管还是问题还是 回答 都是涨见识了
我习惯的做法是转换为list 然后比较 给个意见 这方法中不
List<T> list = new ArrayList(c);
我习惯的做法是转换为list 然后比较 给个意见 这方法中不
List<T> list = new ArrayList(c);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这个变量名起了真是啰嗦,是intersection方法写的不对 Collection集合可以用for便利吗?
需要遍历器 具体看看这个网址
http://zhidao.baidu.com/link?url=lf57n3xqeGtrAHWBhxahaBBiIRAeZ7fwKy3kltKpVCsyxdvA63qozeEuj06-8OUoqIeMotXtniYLZRgos3RP0q
需要遍历器 具体看看这个网址
http://zhidao.baidu.com/link?url=lf57n3xqeGtrAHWBhxahaBBiIRAeZ7fwKy3kltKpVCsyxdvA63qozeEuj06-8OUoqIeMotXtniYLZRgos3RP0q
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询