It is a specialized implementation of a Set interface for enumeration data types.
A few important facts about EnumSet are as follows:
- It is not synchronized.
- It is much faster than HashSet.
- All keys must be keys of a single enum type.
- It doesn't allow a null key, though null values are permitted.
Hierarchy of EnumSet class
It extends AbstractSet and implements Set Interface in Java.
Source code of TestMain.java
package com.t4b.test;
import java.util.EnumSet;
import java.util.Iterator;
public class TestMain {
public enum WeekDays {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
};
public static void main(String[] args) {
EnumSet<WeekDays> enumset = EnumSet.of(WeekDays.Sunday, WeekDays.Saturday);
enumset.add(WeekDays.Wednesday);
enumset.add(WeekDays.Tuesday);
Iterator<WeekDays> itr = enumset.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.