tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Collection > EnumSet > EnumSet Range

EnumSet Range 

The following example shows how to create an enum set containing all of the elements in the range defined by the two specified elements. The range specified is inclusive of elements. EnumSet provides the following APIs,

public static <E extends Enum<E>> EnumSet<E> range(E paramE1, E paramE2)
 - Returns enum set containing all of the elements in the range 
   defined by the two specified elements. 


File Name  :  
com/bethecoder/tutorials/utils/enums/EnumSetRangeTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.utils.enums;

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

public class EnumSetRangeTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    EnumSet<Fruit> enumSet = EnumSet.range(Fruit.BANANA, Fruit.MANGO);
    System.out.println(enumSet);
    
    Iterator<Fruit> it = enumSet.iterator();
    Fruit fruit = null;
    
    while (it.hasNext()) {
      fruit = it.next();
      System.out.println(fruit + "-->" + fruit.ordinal());
    }
    
  }

}
   


File Name  :  
com/bethecoder/tutorials/utils/enums/Fruit.java 
   
package com.bethecoder.tutorials.utils.enums;

public enum Fruit {
  APPLE,
  ORANGE,
  BANANA,
  CHERRY,
  MANGO
}
   

It gives the following output,
[BANANA, CHERRY, MANGO]
BANANA-->2
CHERRY-->3
MANGO-->4



 
  


  
bl  br