Home Tutorial Java Iterator Java Set Iterator

 
 

Share on Google+Share on Google+
Java Set Iterator
Posted on: November 4, 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.

  • 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


Java Set Iterator Example
import java.util.*;

public class set1 {

    public static void main(String[] args) {
        Set s = new HashSet();
        s.add("car");
        s.add("scooter");
        s.add("bike");
        s.add("aeroplane");
        s.add("car");
        Iterator it = s.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }
}

Output

car bike aeroplane scooter
Advertisement

Related Tags for Java Set Iterator :


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 4, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com