title: Creating a TreeSet that would ignore the case
body: <p>An exercise from the Java tutorial on Java Collections Framework requires that a SortedSet be used to eliminate duplicates of the arguments and a Comparator be specified so that case is ignored when sorting and identifying set elements.</p>
  
  <p>Here is the exact requirement:
  "Take the FindDupsexample and modify it to use a SortedSet instead of a Set. Specify a Comparator so that case is ignored when sorting and identifying set elements."</p>
  
  <p>And here is the FindDupsExample:</p>
  
  <pre><code>import java.util.*;
  public class FindDups {
  public static void main(String[] args) {
      Set&lt;String&gt; s = new HashSet&lt;String&gt;();
      for (String a : args)
             s.add(a);
             System.out.println(s.size() + " distinct words: " + s);
  }
  }
  </code></pre>
  
  <p>The most I could come up with to achieve the desired behavior (eliminating duplicates by considering a word written once with small caps and another time with big caps as being a duplicate) is the below code, BUT I am clueless on how to use the Comparator and the SortedSet. I am using the SortedSet in my example, but I could very well use a simple Set:</p>
  
  <pre><code>public class FindDups {
  public static void main(String[] args) {
      Set&lt;String&gt; s = new HashSet&lt;String&gt;();
      List&lt;String&gt; list = new ArrayList&lt;String&gt;();
      SortedSet&lt;String&gt; eliminatedDups = null;
  
      for (String a : args) {
             s.add(a);
             list.add(a.toLowerCase());
      }
      eliminatedDups = new TreeSet&lt;String&gt;(list);
  
      System.out.println(s.size() + " distinct words by taking into consideration the case: " + s);
      System.out.println(list.size() + " initial list translated into all small caps: " + list);
      System.out.println(eliminatedDups.size() + " distinct words by ignoring case: " + eliminatedDups);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>How could I use a SortedSet and a Comparator to obtain the desired effect?</p>
  
  <p>Thanks,</p>
  
  <hr>
  
  <p>Inspired by SJuan76, have finally come up with:</p>
  
  <pre><code>public class FindDupsFinal {
  public static void main(String[] args) {
      SortedSet&lt;String&gt; eliminatedDups2 = new TreeSet&lt;String&gt;(IGNORE_CASE);        
  
      for (String a : args) {
             eliminatedDups2.add(a);
      }
      System.out.println(eliminatedDups2.size() + " distinct words by ignoring case: " + eliminatedDups2);
  }
  
  static final Comparator&lt;String&gt; IGNORE_CASE = new Comparator&lt;String&gt;() {
      public int compare(String s1, String s2) {
          return s1.compareToIgnoreCase(s2);
      }
  };
  </code></pre>
  
  <p>}</p>