Java.util.EnumSet.range() Method

Advertisements


Description

The java.util.EnumSet.range(E from,E to) method creates an enum set initially containing all of the elements in the range defined by the two specified endpoints.

Declaration

Following is the declaration for java.util.EnumSet.range() method

public static <E extends Enum<E>> EnumSet<E> range(E from,E to)

Parameters

  • from -- the first element in the range

  • to -- the kast element in the range

Return Value

This method returns an enum set initially containing all of the elements in the range defined by the two specified endpoints.

Exception

  • NullPointerException-- if first or last are null

  • IllegalArgumentException-- if first > last

Example

The following example shows the usage of java.util.EnumSet.range() method.

package com.tutorialspoint;

import java.util.*;

public class EnumSetDemo {

   // create an enum
   public enum Numbers {

      ONE, TWO, THREE, FOUR, FIVE
   };

   public static void main(String[] args) {

      // create a set
      EnumSet<Numbers> set;

      // add one element
      set = EnumSet.range(Numbers.TWO, Numbers.FIVE);

      // print the set
      System.out.println("Set:" + set);

   }
}


Let us compile and run the above program, this will produce the following result:

Set:[TWO, THREE, FOUR, FIVE]



Advertisements
Advertisements