Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes. And it can be done in just 1 (elegant) line:</p> <pre><code>List&lt;String&gt; noDups = new ArrayList&lt;String&gt;(new LinkedHashSet&lt;String&gt;(list)); </code></pre> <p>The intermediate <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Set.html" rel="nofollow noreferrer"><code>Set</code></a> ensures no duplicates. The <a href="http://docs.oracle.com/javase/7/docs/api/java/util/LinkedHashSet.html" rel="nofollow noreferrer"><code>LinkedHashSet</code></a> implementation of <code>Set</code> was chosen to preserve the order of the list.</p> <p><br/> Also, on a style note:</p> <ul> <li>name your methods and parameters with names starting with a lowercase letter</li> <li>always refer to the abstract (ie <code>List</code>) rather than the concrete (ie <code>ArrayList</code>) when specifying method signatures</li> </ul> <p>Your whole method is then:</p> <pre><code>private static List&lt;String&gt; killDups(List&lt;String&gt; list) { return new ArrayList&lt;String&gt;(new LinkedHashSet&lt;String&gt;(list)); } </code></pre> <p>For extra <a href="http://en.wikipedia.org/wiki/Brownie_points" rel="nofollow noreferrer">brownie points</a> make the method generic, so it works with <em>any</em> type of <code>List</code>:</p> <pre><code>private static &lt;T&gt; List&lt;T&gt; killDups(List&lt;T&gt; list) { return new ArrayList&lt;T&gt;(new LinkedHashSet&lt;T&gt;(list)); } </code></pre> <p>If you wanted to ignore certain characters, I'd create a class for that and have a list of those. Both the <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()" rel="nofollow noreferrer"><code>hashCode()</code></a> and the <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)" rel="nofollow noreferrer"><code>equals()</code></a> methods are relied upon by <a href="http://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html" rel="nofollow noreferrer"><code>HashSets</code></a> to remove dups:</p> <pre><code>public class MungedString { // simplified code String s; public boolean equals(Object o) { // implement how you want to compare them here } public int hashCode() { // keep this consistent with equals() } } </code></pre> <p>then</p> <pre><code>List&lt;MungedString&gt; list; List&lt;MungedString&gt; noDupList = killDups(list); </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