Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Every Java <a href="http://download.oracle.com/javase/6/docs/api/java/util/Collection.html" rel="nofollow"><code>Collection</code></a> has an <a href="http://download.oracle.com/javase/6/docs/api/java/util/Collection.html#add%28E%29" rel="nofollow"><code>add()</code></a> and a <a href="http://download.oracle.com/javase/6/docs/api/java/util/Collection.html#remove%28java.lang.Object%29" rel="nofollow"><code>remove()</code></a> method that do what you want.</p> <p>Since <code>Collection</code> is an interface, you'll need to chose one of the implementations (which are usually either <a href="http://download.oracle.com/javase/6/docs/api/java/util/Set.html" rel="nofollow"><code>Set</code></a> or <a href="http://download.oracle.com/javase/6/docs/api/java/util/List.html" rel="nofollow"><code>List</code></a> implementations as well). <a href="http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html" rel="nofollow"><code>ArrayList</code></a> is a pretty common choice.</p> <p>A <a href="http://download.oracle.com/javase/6/docs/api/java/util/HashSet.html" rel="nofollow"><code>HashSet</code></a> is another possible candidate.</p> <pre><code>Colllection&lt;Foo&gt; mc = new ArrayList&lt;Foo&gt;(); mc.add(new Foo()); mc.add(someFoo1=new Foo()); mc.add(new Foo()); mc.add(someFoo2=new Foo()); mc.remove(someFoo1); mc.remove(someFoo2); </code></pre> <p>Which specific implementation you want, will depend on what other properties you want.</p> <p>If you want to keep a defined order and access objects by index, then you will need a <code>List</code> implementation. The "default" <code>List</code> is <code>ArrayList</code>. If you often remove/add elements anywhere <em>except</em> the end, then a <code>LinkedList</code> will provide better performance (but most of the time you won't notice the difference).</p> <p>If you want to avoid duplicate objects, then you will need a <code>Set</code> implementation. The "default" <code>Set</code> is <code>HashSet</code>, but a <a href="http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html" rel="nofollow"><code>TreeSet</code></a> is also used occasionally, providing the added benefit of having a defined order (but it requires you to either provide a <a href="http://download.oracle.com/javase/6/docs/api/java/util/Comparator.html" rel="nofollow"><code>Comparator</code></a> or ensure that all elements are <a href="http://download.oracle.com/javase/6/docs/api/java/lang/Comparable.html" rel="nofollow"><code>Comparable</code></a>).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload