Program to Sort Set by using TreeSet(by default elements are sorted in ascending order), where elements are Integer type















TreeSet is sorted by natural order of elements (i.e. in ascending order).
import java.util.TreeSet;
import java.util.Set;
/**
* @author AnkitMittal
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
* Main class
*/
public class SortSet
{
   public static void main(String...a){
       Set<Integer> treeSet = new TreeSet<Integer>();
       treeSet.add(3);
       treeSet.add(1);
       treeSet.add(2);
       System.out.println("treeSet : "+treeSet);
      
   }
}
/*OUTPUT
treeSet : [1, 2, 3]
*/

No comments:

Post a Comment