Home » Java programming language

Differences between HashSet and TreeSet in Java

HashSet vs TreeSet in Java: Here, we are going to learn what are the differences between the HashSet and TreeSet in Java programming language?
Submitted by Preeti Jain, on August 11, 2019

HashSet vs TreeSet in Java

First, we will see how TreeSet differs from HashSet in Java?

TreeSet

  • TreeSet is available in java.util package.
  • TreeSet is an implementation class of Set interface.
  • The underlying data structure of TreeSet is Balanced Tree.
  • In TreeSet "insertion order of the elements" is not preserved because elements will be inserted in TreeSet according to some ascending sorting order or we can say in other words "insertion order of the elements" is not needed to be the same as the "retrieval order of the elements".
  • In TreeSet Object is represented as a group of individual elements as a single entity in term of values only.
  • In TreeSet "duplicate elements are not allowed" that means it is not possible to insert duplicate elements in TreeSet.
  • In TreeSet "null insertion is not possible " for a non-empty set.
  • In TreeSet "null insertion is possible" for empty Set as the first element and if we insert null after inserting the first element then it is not possible or invalid.
  • In TreeSet "Heterogenous objects" are not allowed and if will insert forcefully then we will get an exception "ClassCastException".

Example:

// Java program to demonstrate the behavior of TreeSet

import java.util.*;

class TreeSetClass {
    public static void main(String[] args) {
        // Creating an instance of TreeSet
        TreeSet ts = new TreeSet();

        // By using add() to add elements in TreeSet
        ts.add(10);
        ts.add(30);
        ts.add(40);
        ts.add(20);

        /*  ts.add(30); 
            Here will not get any exception or errors 
            but it will be ignored because duplicate 
            insertion is not possible */

        /*  ts.add(null); 
            here we will get an exception NullPointerException 
            because we are inserting null for non-empty set */

        /*  ts.add("Java"); 
            here we will get an exception ClassCastException 
            because we are inserting hetrogenous object in TreeSet */

        // Display Current TreeSet
        System.out.println("Current TreeSet is :" + ts);
    }
}

Output

E:\Programs>javac TreeSetClass.java

E:\Programs>java TreeSetClass
Current TreeSet is :[10, 20, 30, 40]

Second, we will see how HashSet differs from TreeSet in Java?

HashSet

  • HashSet is available in java.util package.
  • HashSet is an implementation class of Set interface.
  • HashSet is a parent class of LinkedHashSet.
  • The underlying data structure of TreeSet is Hashtable.
  • In HashSet "insertion order of the elements" is not preserved or we can say in other words "insertion order of the elements" is not needed to be the same as the "retrieval order of the elements".
  • In HashSet Object is represented as a group of individual elements as a single entity in term of values only.
  • In HashSet "duplicate elements are not allowed" that means it is not possible to insert duplicate elements in HashSet.
  • In HashSet "null insertion is possible " for the non-empty and empty set.
  • In HashSet "Heterogenous objects" are allowed and if will insert forcefully then we will not get any exception.

Example:

// Java program to demonstrate the behavior of HashSet

import java.util.*;

class HashSetClass {
    public static void main(String[] args) {
        // Creating an instance of HashSet
        HashSet hs = new HashSet();

        // By using add() method to add elements in HashSet
        hs.add(10);
        hs.add(30);
        hs.add(40);
        hs.add(20);

        /*  Here we will not get any exception because 
            null insertion is possible in HashSet
        */
        hs.add(null);

        /*  Here will not get any exception or errors 
            but it will be ignored because duplicate insertion
            is not possible 
        */
        hs.add(30);

        /*  Here we will not get any exception because hetrogenous 
            object insertion is possible in HashSet
        */
        hs.add("Java");

        // Display Current HashSet
        System.out.println("Current HashSet is :" + hs);
    }
}

Output

E:\Programs>javac HashSetClass.java

E:\Programs>java HashSetClass
Current HashSet is :[null, 20, 40, 10, Java, 30]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.