BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to convert a HashSet to a TreeSet

By Chaitanya Singh | Filed Under: java

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

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 *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap