Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you have no problems with using the standard library (always preferable, why reinvent the wheel) try</p> <pre><code>List&lt;String&gt; uniques = new ArrayList&lt;String&gt;(new HashSet&lt;String&gt;(oldList)); </code></pre> <p>The <code>HashSet</code> will only contain unique strings and the <code>ArrayList</code> constructor takes any <code>Collection</code> (including a <code>HashSet</code>) to build a list from.</p> <p>Judging from your comments it seems like you are trying to implement an <a href="http://en.wikipedia.org/wiki/Associative_array" rel="nofollow">Associative Array</a> with unique keys using an ArrayList. The better approach is to use a <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Map.html" rel="nofollow">Map</a> implementation like HashMap to pair IDs with their associated Strings.</p> <pre><code>Map&lt;Integer, String&gt; map = new HashMap&lt;&gt;(); map.put(1, "This string corresponds to ID=1"); map.put(3, "Donald Ducks Nephews"); map.put(7, "Is a Prime"); </code></pre> <p>Then to get a value associated with an ID:</p> <pre><code>int key = someObject.getID(); String value = map.get(key); </code></pre> <p>All the Map implementations use unique keys so there is no need for you to check for redundant IDs, if you try to add a new (key,value) pair the value associated with the ID will be replaced if the map contains the key.</p> <pre><code>map.put(1, "New String"); String s = map.get(1); //s will no longer be "This string corresponds to ID=1" </code></pre> <p>If you don't want this behavior you have the choice of either subclassing one of the Map implementations to ignore <code>.put(key, value)</code> if the map contains <code>key,value</code> or delegating <code>.put(key,value)</code> to some other class.</p> <p><strong>Subclassing:</strong></p> <pre><code>public class UniqueValueHashMap&lt;K,V&gt; extends HashMap&lt;K, V&gt;{ @Override public V put(K key, V value) { if (containsKey(key)) return null; return super.put(key, value); } </code></pre> <p><strong>Delegating</strong></p> <pre><code> public class SomeClass { private Map&lt;Integer, String&gt; map = new HashMap&lt;&gt;(); // ...stuff this class does public String put(int key, String value) { if (map.containsKey(key)) return null; return map.put(key, value); } // ...more stuff this class does } </code></pre> <p>Delegation is the better approach, notice how you can change the map implementation (using maybe a TreeMap instead of HashMap) without introducing a new class where you override the <code>.put(key,value)</code> of TreeMap.</p>
    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