4

How do I get the treeset to sort alphabetically? And remove duplicates.. it's been driving me nuts for a day. Maybe I need to get some sleep..

public static void main(String[] args) {
        String fileName = args[0];
        String words;
        Scanner s = null;
        Iterator itr;

        try {
            s = new Scanner(new BufferedReader(new FileReader(fileName)));
                while (s.hasNext()) {
                    words = s.next();

                    TreeSet<String> ts = new TreeSet<String>();
                    ts.add(words);

                    System.out.println(ts);
                }
            } catch (FileNotFoundException fnfe) {
            System.exit(0);
        } finally {
               if (s != null) {
                   s.close();
                }
            }
    }        
A C
  • 417
  • 3
  • 5
  • 17

1 Answers1

7

TreeSet holds the set in a tree structure which is automatically sorted in natural order. Every class that implements the Comparable interface will be sorted. The String class implements the Comparable interface already so you don't have to do anything to sort it, just add it to the TreeSet.

Sets can't contain duplicates if the hashCode() and equals() methods are implemented how they should.

EDIT: The TreeSet<String> ts = new TreeSet<String>(); is located in the while() loop scope. You are initializing it every loop and loosing the data drom the previous one. Declare it outside the loop and don't use Collection.sort()

SkyWalker
  • 28,384
  • 14
  • 74
  • 132
emd
  • 740
  • 4
  • 12
  • 2
    And for `String`, `hashCode` and `equals` are already implemented (correctly) for you... – Louis Wasserman Mar 19 '13 at 20:06
  • 1
    Sure for 99.999% of the cases but it still can happen that two different string have a hash collision (the same hash). – emd Mar 19 '13 at 20:11
  • 2
    @emd - the possibility of hash collision is the reason the `equals` method is also required to be implemented, and properly. – Perception Mar 19 '13 at 20:14
  • I'm using a text file to import words which are "this is a test of the import function function". (so there's a dupe word). But if it was automatically sorted, why does it show up in the sequence in the text file and not alphabetical, and the dupe word isn't removed either? Sorry if that's a noob question! – A C Mar 19 '13 at 20:16
  • The `main` program you've given us doesn't print anything. Give us an example of the code you're actually using and some sample input on which it fails. – Louis Wasserman Mar 19 '13 at 20:19
  • The TreeSet ts = new TreeSet(); is located in the while() loop scope. You are initializing it every loop and loosing the data drom the previous one. Declare it outside the loop and dont use Collection.sort() – emd Mar 19 '13 at 20:20