CookieCollection implements Collection<Cookie> { @Override public boolean retainAll(Collection<?> collection) { boolean result=false; Iterator<Map.Entry<String, Cookie>> it=cookieMap.entrySet().iterator(); while(it.hasNext()) { Map.Entry<String, Cookie> e=it.next(); if(!collection.contains(e.getKey()) && !collection.contains(e.getValue())) { it.remove(); result=true; } } return result; } CookieCollection();  CookieCollection(Cookie... cookies);  CookieCollection(Collection<? extends Cookie> cookies); @Override int size(); @Override boolean isEmpty(); @Override boolean contains(Object o); @Override Iterator<Cookie> iterator(); Cookie [] toArray(); @Override T[] toArray(T[] ts); @Override boolean add(Cookie cookie); @Override boolean remove(Object o); Cookie get(String name); @Override boolean containsAll(Collection<?> collection); @Override boolean addAll(Collection<? extends Cookie> collection); @Override boolean removeAll(Collection<?> collection); @Override boolean retainAll(Collection<?> collection); @Override void clear();  }
@Test public void retainAll() throws Exception { CookieCollection cookies = new CookieCollection(cookie, brownieCookie, cupsCookie); List<Cookie> content = Arrays.asList(cookie, brownieCookie); boolean modified = cookies.retainAll(content); Assert.assertTrue(modified); Assert.assertEquals(2, cookies.size()); Assert.assertTrue(cookies.containsAll(content)); Assert.assertFalse(cookies.contains(cupsCookie)); Assert.assertFalse(cookies.retainAll(content)); }