Converting HashSet in to TreeSet using java



In following program you can easily analyze the difference between TreeSet  andHashSet.  
// Comment

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
packagecom.allinalljava;
importjava.util.*;
/**
 *
 * @author allinalljava
 */
public class JavaHashSettoTreeSet {
    // Demonstrate HashSet.


public static void main(String args[]) {
// Create a hash set.
HashSethashSet = new HashSet();
// Add elements to the hash set.
hashSet.add("B");
hashSet.add("A");
hashSet.add("D");
hashSet.add("E");
hashSet.add("C");
hashSet.add("F");
System.out.println(" HashSet Elements "+hashSet);
TreeSetts =new TreeSet(hashSet);
System.out.println("TreeSet Elements "+ts);

}
}


OUTPUT 
HashSet Elements [D, E, F, A, B, C] TreeSet Elements [A, B, C, D, E, F]
raja P Web Developer

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...