开发者

Remove objects from a java set

开发者 https://www.devze.com 2023-04-06 06:51 出处:网络
How do i remove all the objects from one set, that exist also in an other set. for exaple:开发者_如何学Go

How do i remove all the objects from one set, that exist also in an other set. for exaple:

开发者_如何学Go
SET 1 = { a,b,c,d,e,f)  |
                        |---> NEW SET 1= (a,b,d,e}
SET 2 = {c,f)           |


I'm not sure what the problem is

  set1.removeAll(set2);


code from org.apache.commons.collections.CollectionUtils

  public static Collection subtract(final Collection a, final Collection b) {
        ArrayList list = new ArrayList( a );
        for (Iterator it = b.iterator(); it.hasNext();) {
            list.remove(it.next());
        }
        return list;
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消