Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>HashSet is much faster than TreeSet (constant-time versus log-time for most operations like add, remove and contains) but offers no ordering guarantees like TreeSet.</strong></p> <h2><a href="http://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html" rel="noreferrer">HashSet</a></h2> <ul> <li>the class offers constant time performance for the basic operations (add, remove, contains and size). </li> <li>it does not guarantee that the order of elements will remain constant over time</li> <li>iteration performance depends on the <em>initial capacity</em> and the <em>load factor</em> of the HashSet. <ul> <li>It's quite safe to accept default load factor but you may want to specify an initial capacity that's about twice the size to which you expect the set to grow.</li> </ul></li> </ul> <h2><a href="http://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html" rel="noreferrer">TreeSet</a></h2> <ul> <li>guarantees log(n) time cost for the basic operations (add, remove and contains)</li> <li>guarantees that elements of set will be sorted (ascending, natural, or the one specified by you via its constructor) (implements <a href="http://docs.oracle.com/javase/8/docs/api/java/util/SortedSet.html" rel="noreferrer"><code>SortedSet</code></a>)</li> <li>doesn't offer any tuning parameters for iteration performance</li> <li>offers a few handy methods to deal with the ordered set like <a href="http://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html#first--" rel="noreferrer"><code>first()</code></a>, <code>last()</code>, <a href="http://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html#headSet-E-" rel="noreferrer"><code>headSet()</code></a>, and <a href="http://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html#tailSet-E-" rel="noreferrer"><code>tailSet()</code></a> etc</li> </ul> <h2>Important points:</h2> <ul> <li>Both guarantee duplicate-free collection of elements</li> <li>It is generally faster to add elements to the HashSet and then convert the collection to a TreeSet for a duplicate-free sorted traversal.</li> <li>None of these implementations are synchronized. That is if multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally.</li> <li><strong>LinkedHashSet</strong> is in some sense intermediate between <code>HashSet</code> and <code>TreeSet</code>. Implemented as a hash table with a linked list running through it, however,<strong>it provides insertion-ordered iteration which is not same as sorted traversal guaranteed by TreeSet</strong>.</li> </ul> <p>So a choice of usage depends entirely on your needs but I feel that even if you need an ordered collection then you should still prefer HashSet to create the Set and then convert it into TreeSet.</p> <ul> <li>e.g. <code>SortedSet&lt;String&gt; s = new TreeSet&lt;String&gt;(hashSet);</code></li> </ul>
    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