java吧 关注:1,313,630贴子:12,897,549
  • 2回复贴,共1页

求帮忙看一下,不用retainAll来求两个collection的交集

取消只看楼主收藏回复

public static Collection<Account> getMixedCollect(Collection<Account> accountsOne ,Collection<Account> accountsTwo){
Collection<Account> mixedAccounts = new ArrayList<Account>();
if (accountsOne != null && accountsOne.size() > 0 && accountsTwo !=null && accountsTwo.size() > 0) {
for (Iterator<Account> iteratorOne = accountsOne.iterator() ; iteratorOne.hasNext();) {
Account accountO = (Account)iteratorOne.next();
System.out.println("readyOne");
for(Iterator<Account> iteratorTwo = accountsTwo.iterator(); iteratorTwo.hasNext();){
Account accountT = (Account)iteratorTwo.next();
System.out.println("readyTwo");
if (accountO.equals(accountT)) {
System.out.println("success");
mixedAccounts.add(accountT);
}
}
}
}
return mixedAccounts ;
}


1楼2013-08-07 21:23回复
    为什么第一个collection没问题,第二个有问题,前提是两个collection都不为空,也就是输出了readyOne 但是剩下的都没有输出


    2楼2013-08-07 21:25
    回复
      2026-11-29 08:47:30
      广告
      不感兴趣
      开通SVIP免广告
      好吧。我知道了,在第二个for循环里面每次都新建一个迭代器是不合逻辑,此贴结贴


      3楼2013-08-07 21:37
      回复