EnumSet iterator() in Java

iterator(): This method is available in java.util.EnumSet class of Java.

Syntax:

Iterator<K> java.util.AbstractCollection.iterator()

This method returns an iterator over the elements contained in this collection.

Parameters: NA

Returns: an iterator over the elements contained in this collection.

Exceptions: NA

Approach

Java

import java.util.EnumSet;
import java.util.Iterator;

public class EnumSetiterator {
    public enum Colour {
        RED, GREEN, YELLOW, ORANGE
    };

    public static void main(String[] args) {

        EnumSet<Colour> enumSet = EnumSet.allOf(Colour.class);

        Iterator<Colour> itr = enumSet.iterator();

        while (itr.hasNext())
            System.out.print(itr.next() + " ");

    }
}

Output:

RED GREEN YELLOW ORANGE 

No comments:

Post a Comment