Friday, November 2, 2012

Sorted Set in Java

Here is an example of Java's sorted set


package com.giri.test;

import java.util.Iterator;
import java.util.TreeSet;

public class SortedSet {

 public static void main(String[] args) {
  TreeSet s_ss = new TreeSet();
  TreeSet i_ss = new TreeSet();
  
  s_ss.add("peter");
  s_ss.add("jack");
  s_ss.add("gary");
  s_ss.add("mark");
  
  i_ss.add(146);
  i_ss.add(10);
  i_ss.add(115);
  i_ss.add(103);
  
  Iterator s_it = s_ss.iterator();
  Iterator i_it = i_ss.iterator();
  
  System.out.println("Printing String sorted set: ");
  while(s_it.hasNext()) {
   System.out.println("Value : " + (String) s_it.next());
  }
  
  System.out.println("Printing Integer sorted set: ");
  while(i_it.hasNext()) {
   System.out.println("Value : " + (Integer) i_it.next());
  }

 }

}

Output:

Printing String sorted set: 
Value : gary
Value : jack
Value : mark
Value : peter

Printing Integer sorted set: 
Value : 10
Value : 103
Value : 115
Value : 146

No comments :

Post a Comment