2

Im currently working on a program, where im using a TreeSet to store unique keys. I'm using a TreeSet, because I want them sorted.

As of now, I have been making a TreeSet object, and added the strings one-at-a-time, when needed.

TreeSet set = new TreeSet();
set.add("How");
set.add("Are");
set.add("You");

Supposedly, the TreeSet uses the compareTo method in the Compareable interface, to sort the strings. That would mean that the TreeSet would have to do a sort each time I add a String to it.

Now my question is this: Would it be more efficient to create a HashSet and then create the TreeSet after all strings are added to the HashSet?

TreeSet<String> treeSet = new TreeSet<String>(set);

My thoughts are going about whether the TreeSet would only need to do a single sort that way.

Thanks in advance

Daniel Mac
  • 400
  • 2
  • 14
  • 1
    No; you're misunderstanding BSTs. – SLaks Aug 26 '13 at 20:47
  • 1
    `TreeSet` doesn't need to sort every time it inserts a string. More likely, it uses `compareTo` to figure out whether to take the left or right subtree when traversing the tree to insert the new element. – Dennis Meng Aug 26 '13 at 20:47
  • 2
    Imagine you have a bookshelf, where all the books are sorted in alphabetical order. If you buy a new book, and want to put it on the shelf in the right place, do you have to re-sort all the books that are already there? – Dawood ibn Kareem Aug 26 '13 at 20:48
  • And moreover, creating a TreeSet from a HashSet will simply add all the keys one by one anyway. – JB Nizet Aug 26 '13 at 20:49
  • I see your point, thanks :) – Daniel Mac Aug 26 '13 at 20:50

3 Answers3

5

TreeSet is a Self-balancing binary search tree. That means it will do log(n, 2) comparisons for each insert. It makes no difference if you add the elements independently or create the TreeSet from another collection.

nosid
  • 48,932
  • 13
  • 112
  • 139
0

It won't make any difference as it internally calls the "addAll()" method, which in turn, adds one element at a time.

public boolean addAll(Collection<? extends E> c) {
    boolean modified = false;
    Iterator<? extends E> e = c.iterator();
    while (e.hasNext()) {
        if (add(e.next()))    //< -- Add one element at a time
           modified = true;
    }
    return modified;
}
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
0

ConcurrentSkipListSet might offer better performance, depending.

This is based on an implementation of a sorted SkipList. Asymptotic performance is O(log n) like a TreeSet, but it is supposed to be faster in general, and more storage-compact. Plus, java includes the implementation already!

It might very well be faster to create the skiplist list from a sorted arraylist, depending on the implementation. Bulk additions are usually faster than one-by-one (depending on the implementation). The TreeSet represents something of an exception. Profile and find out!

BobMcGee
  • 19,824
  • 10
  • 45
  • 57