Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Thursday, 26 January 2017

Write a Java Program to add a HashSet to a TreeSet and iterate it in ascending and descending order.

import java.util.*;

public class ABC {

public static void main(String[] args)

{   

HashSet h=new HashSet();   

 h.add(1);   /*insert any desired value*/

h.add(12);

h.add(13);

h.add(14);

h.add(16);

h.add(67);   

TreeSet t= new TreeSet(h);  

 TreeSet t1=new TreeSet();  

 System.out.println(t);

Iterator i=t.iterator();

System.out.println("In ascending Order--------");

while(i.hasNext())

{ System.out.print(i.next()+ " ");}

System.out.println();

System.out.println("In descending Order---------");

t1=(TreeSet) t.descendingSet();

Iterator i1=t1.iterator();

while(i1.hasNext()){System.out.print(i1.next() +" "); }

}

}

No comments:

Post a Comment