Home » Java programming language

Java EnumSet range() Method with Example

EnumSet Class range() method: Here, we are going to learn about the range() method of EnumSet Class with its syntax and example.
Submitted by Preeti Jain, on February 13, 2020

EnumSet Class range() method

  • range() method is available in java.util package.
  • range() method is used to create an enum set and assign all the elements in the range of the given two parameters st (starting position) and en (ending position).
  • range() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
  • range() method may throw an exception at the time of returning enum set.
    • NullPointerException: This exception may throw when the given any one of the parameters is null exists.
    • IllegalArgumentException: This exception may throw when the given first parameter is greater than the second parameter.

Syntax:

    public static EnumSet range(Enumset st, Enumset en);

Parameter(s):

  • Enumset st – represents the starting element in the enum set.
  • Enumset en – represents the ending element in this enum set.

Return value:

The return type of this method is EnumSet, it returns an enum set with elements of the given range defined.

Example:

// Java program is to demonstrate the example of
// range() method of EnumSet

import java.util.*;

public class RangeOfEnumSet {
    // Initialize a enum variable
    // with some constants

    public enum Colors {
        RED,
        BLUE,
        GREEN,
        PURPLE,
        YELLOW
    };

    public static void main(String[] args) {
        // Here , we are creating two EnumSet
        EnumSet < Colors > all_of = null;

        // By using allOf() method is to 
        // get all of the elements of an enum 
        // and put into an es
        all_of = EnumSet.allOf(Colors.class);

        // Display Modified EnumSet
        System.out.println("EnumSet.allOf(Colors.class): " + all_of);

        // By using range() method is to 
        // get all of the elements defined in the 
        // given range of an EnumSet 
        EnumSet range = EnumSet.range(Colors.GREEN, Colors.YELLOW);

        // Display Modified EnumSet
        System.out.println("EnumSet.range(Colors.GREEN, Colors.YELLOW): " + range);
    }
}

Output

EnumSet.allOf(Colors.class): [RED, BLUE, GREEN, PURPLE, YELLOW]
EnumSet.range(Colors.GREEN, Colors.YELLOW): [GREEN, PURPLE, YELLOW]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.