beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How to convert a HashSet to a TreeSet

By Chaitanya Singh | Filed Under: Java.util package

Description

Program to convert a HashSet to a TreeSet

Program

Here is the complete code for HashSet to TreeSet conversion. We have a HashSet of Strings and we are creating a TreeSet of strings by copying all the elements of HashSet to TreeSet.

import java.util.HashSet;
import java.util.TreeSet;
import java.util.Set;
class ConvertHashSettoTreeSet{ 
  public static void main(String[] args) {
     // Create a HashSet
     HashSet<String> hset = new HashSet<String>();
 
     //add elements to HashSet
     hset.add("Element1");
     hset.add("Element2");
     hset.add("Element3");
     hset.add("Element4");
 
     // Displaying HashSet elements
     System.out.println("HashSet contains: "+ hset);
 
     // Creating a TreeSet of HashSet elements
     Set<String> tset = new TreeSet<String>(hset);
 
     // Displaying TreeSet elements
     System.out.println("TreeSet contains: ");
     for(String temp : tset){
        System.out.println(temp);
     }
  }
}

Output:

HashSet contains: [Element1, Element2, Element3, Element4]
TreeSet contains: 
Element1
Element2
Element3
Element4

Enjoyed this post? Try these related posts

  1. How to copy one Set to another Set
  2. Difference between HashSet and TreeSet
  3. Clone a HashMap in Java
  4. LinkedList push() and pop() methods – Java
  5. How to check if a HashMap is empty or not?
  6. Java – LinkedList peek(), peekFirst() and peekLast() methods

Comments

  1. Rohit Chakrapani says

    September 10, 2015 at 7:42 PM

    This example is fine… but if you would have given some unsorted input to HashSet and then converted it into TreeSet,then it would have being more beneficial for the beginners as it will also clear the concept of indirect sorting using HashSet.

    Reply
  2. Rohit Chakrapani says

    September 10, 2015 at 7:45 PM

    You have done a great job !!! Many are benefited from this site. Thanks a lot.

    Reply
  3. Bhanu ravali says

    February 8, 2016 at 1:29 AM

    It’s beneficial but in the topic convert HashSet to TreeSet, I did not get to covert HashSet to TreeSet ,created a variable to Set interface and then address of treeSet object stored in that variable.why not we did not create a variable for treeSet.if you give some more explanation, it’s beneficial for beginners.

    Set s= new TreeSet(hSet);
    Why not like below
    TreeSet t=new TreeSet(hset);

    Reply
    • Sud says

      November 28, 2017 at 10:22 PM

      Because HashSet gives better performance (faster) than TreeSet for the operations like add, remove, contains, size etc. HashSet offers constant time cost while TreeSet offers log(n) time cost for such operations.

      Reply
  4. Nagababu says

    February 19, 2016 at 7:43 AM

    in above program (string temp) for what purpose we use this

    Reply
    • venkatesh says

      May 26, 2016 at 3:56 AM

      That is for temporary reference variable to display map values

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap