Home Tutorial Java Iterator Java Set iterator with example

 
 

Share on Google+Share on Google+
Java Set iterator with example
Posted on: November 3, 2009 at 12:00 AM
Advertisement
In this tutorial we will see how to use the Java iterator with Set interface . We will create an example to display the contents of the set collection.

  • Java Set Interface keeps the data without duplicate value.
  • Its one subtype Treeset always returns sorted data.
  • But the subtype HashSet doesnot return sorted data.
  • It uses iterator() method to traverse the data


Example of Java Set Iterator
import java.util.*;
public class setiterator {

	public static void main(String[] args) {
		Set s = new TreeSet();
		s.add(1000);
		s.add(400);
		s.add(900);
		s.add(700);
		s.add(400);
		Iterator it = s.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		} 
	}
}

Output

400 700 900 1000
Advertisement

Related Tags for Java Set iterator with example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 3, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com