Java Collection: EnumSet - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

It is a specialized implementation of a Set interface for enumeration data types.

A few important facts about EnumSet are as follows:

  1. It is not synchronized.
  2. It is much faster than HashSet.
  3. All keys must be keys of a single enum type.
  4. 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()); } } }

Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.