Add an object to a java structure only if it isn't already there <p>I have an arrayList that I want to add to, as long as the value isn't already stored in it. For example if my array is like this: </p> <pre><code>H LK KL LS </code></pre> <p>And I have the value LS, it wouldn't be added. But if the value was A, it would be. I don't care about order or sorting. Just whether or not the information is in there.</p> <p>I was thinking that the code should look something like this:</p> <pre><code>value = A; no = 0; for (int o; arraylist.length; o++) if (arraylist.get(o).equals(value)){ continue; }else{ no++; } } if (arraylist.length = no){ arraylist.add(value); } </code></pre> <p>But there has to be an easier way to do it. Does anyone know a more concise way to do this?</p>