Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd say: Remove it! It's duplicate 'code', the Set is doing the same 'contains()' operation so there is no need to preprocess it here. Unless you have a huge input collection and a brilliant O(1) test for the containsAll() ;-)</p> <p>The Set is fast enough. It has a O(n) complexity based on the size of the input (one contains() and (maybe) one add() operation for every String) and if the target.containsAll() test fails, contains() is done twice for each String -> less performant.</p> <p><strong>EDIT</strong></p> <p>Some pseudo code to visualize my answer</p> <pre><code>void copy(source, dest) { bool:containsAll = true; foreach(String s in source) { // iteration 1 if (not s in dest) { // contains() test containsAll=false break } } if (not containsAll) { foreach(String s in source) { // iteration 2 if (not s in dest) { // contains() test add s to dest } } } } </code></pre> <p>If all source elements are in dest, then contains() is called once for each source element. If all but the last source elements are in dest (worst case), then contains() is called (2n-1) times (n=size of source collection). But the total number of contains() test <em>with</em> the extra test is always equal or greater then the same code without the extra test.</p> <p><strong>EDIT 2</strong> Lets assume, we have the following collections:</p> <pre><code>source = {"", "a", "b", "c", "c"} dest = {"a", "b"} </code></pre> <p>First, the containsAll test fails, because the empty String in source is not in dest (this is a small design flaw in your code ;)). Then you create an temporary set which will be <code>{"a", "b", "c"}</code> (empty String and second "c" ignored). Finally you add everthing to dest and assuming, dest is a simple ArrayList, the result is <code>{"a", "b", "a", "b", "c"}</code>. Is that the intention? A shorter alternative:</p> <pre><code>void copy(Collection&lt;String&gt; in, Collection&lt;String&gt; out) { Set&lt;String&gt; unique = new HashSet&lt;String&gt;(in); in.remove(""); out.addAll(unique); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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