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. HashSetOUTPUThashSet = 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); } }
HashSet Elements [D, E, F, A, B, C] TreeSet Elements [A, B, C, D, E, F]
No comments:
Post a Comment