HashSet vs. TreeSet vs. Linkedhashset

Source: Internet
Author: User
Tags comparable

A Set contains no duplicate elements. That's one of the major reasons to use a set. There is 3 commonly used implementations of Set:hashset, TreeSet and Linkedhashset. When and which to use are an important question. In brief, if you need a fast set, you should use HashSet; If you need a sorted set and then TreeSet should is used; If you need a set that can is store the insertion order, Linkedhashset should be used.

1. Set Interface

Set interface extends Collection interface. In a set, no duplicates is allowed. Every element in a set must is unique. You can simply add elements to a set, and duplicates'll be removed automatically.

2. HashSet vs. TreeSet vs. Linkedhashset

HashSet is implemented using a hash table. Elements is not ordered. The Add, remove, andcontains methods have constant time complexity O (1).

TreeSet is implemented using a tree structure (red-black tree in algorithm book). The elements in a set is sorted, but the Add, remove, and contains methods have time complexity of O (log (n)). It offers several methods to deal and the ordered set like first (), Last (), HeadSet (), Tailset (), etc.

Linkedhashset is between HashSet and TreeSet. It is implemented as a hash table with a linked list running through it, so it provides the order of insertion. The time complexity of basic methods is O (1).

3. TreeSet Example

treeset<integer> tree = new treeset<integer> (), Tree.add (n), Tree.add (Tree.add); iterator<integer> Iterator = Tree.iterator (); System.out.print ("Tree Set Data:"), while (Iterator.hasnext ()) {    System.out.print (Iterator.next () + "");}

Output is sorted as follows:

Now let ' s define a Dog class as follows:

Class Dog {int size; public Dog (int s) {size = s;} public String toString () {return size + "";}}

Let's add some dogs to TreeSet like the following:

Import Java.util.iterator;import Java.util.TreeSet; public class Testtreeset {public static void main (string[] args) {treeset<dog> dset = new treeset<dog> ();d set. Add (New Dog (2));d Set.add (New Dog (1));d Set.add (New Dog (3)); iterator<dog> Iterator = Dset.iterator (); while (Iterator.hasnext ()) {System.out.print (Iterator.next () + "");}}}

Compile OK, but run-time error occurs:

Exception in thread "main" java.lang.ClassCastException:collection. Dog cannot is cast to Java.lang.Comparableat Java.util.TreeMap.put (Unknown Source) at Java.util.TreeSet.add (Unknown Source) at collection. Testtreeset.main (testtreeset.java:22)

Because TreeSet is sorted, the Dog object need to implement Java.lang.Comparable ' Scompareto () method like the following:

Class Dog implements Comparable<dog>{int size; public Dog (int s) {size = s;} public String toString () {return size + "";} @Overridepublic int compareTo (Dog o) {        return size-o.size;}}

The output is:

4. HashSet Example

hashset<dog> dset = new hashset<dog> ();d Set.add (New Dog (2));d Set.add (New Dog (1));d Set.add (New Dog (3)); Dset.add (New Dog (5));d Set.add (New Dog (4));iterator<dog> Iterator = Dset.iterator (); while (Iterator.hasnext ()) { System.out.print (Iterator.next () + "");}

Output:

Note The order is not certain.

5. Linkedhashset Example

linkedhashset<dog> dset = new linkedhashset<dog> ();d Set.add (New Dog (2));d Set.add (New Dog (1));d Set.add ( New Dog (3));d Set.add (New Dog (5));d Set.add (New Dog (4));iterator<dog> Iterator = Dset.iterator (); while ( Iterator.hasnext ()) {System.out.print (Iterator.next () + "");}

The order of the output is certain and it is the insertion order:

6. Performance Testing

The following method tests the performance of the three class on add () method.

public static void Main (string[] args) { random r = new Random (); hashset<dog> HashSet = new Hashset<do G> (); treeset<dog> TreeSet = new treeset<dog> (); linkedhashset<dog> linkedset = new linkedhashset<dog> ()  //start Timelong startTime = System.nanotime ();  for (int i = 0; i <; i++) {int x = R.nextint (1000-10) + 10;hashset.add (new Dog (x));} End Timelong EndTime = System.nanotime (); Long duration = Endtime-starttime; System.out.println ("HashSet:" + duration);  //Start timestarttime = System.nanotime (); for (int i = 0; i < 1000; i++) {int x = R.nextint (1000-10) + 10;treeset.add (new Dog (x));} End timeendtime = System.nanotime ();d uration = Endtime-starttime; System.out.println ("TreeSet:" + duration);  //Start timestarttime = System.nanotime (); for (int i = 0; i < 1000; i++) {int x = R.nextint (1000-10) + 10;linkedset.add (new Dog (x));} End timeendtime = System.nanotime ();d uration = Endtime-starttime;System.out.println ("Linkedhashset:" + duration);  } 

From the output below, we can clearly wee this HashSet is the fastest one.

hashset:2244768treeset:3549314linkedhashset:2263320

* The test is not a precise, but can reflect the basic idea of TreeSet is much slower because it is sorted.

HashSet vs. TreeSet vs. Linkedhashset

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.