Rajiv Gupta
Rajiv Gupta

Reputation: 111

Fetch all Enum constants

How to retrieve all values of enums at once? Expected Values : ["Male", "Female"] Output of GenderEnum.values() : ["MALE", "FEMALE"]

    public enum SPGenderEnum {
        MALE("Male"),
        FEMALE("Female");

        private SPGenderEnum(final String type) {
            this.type = type;
        }

        private final String type;

        public String getType() {return type;}

        @Override
        public String toString() {
            return type;
        }
    }

Upvotes: 2

Views: 276

Answers (4)

chriopp
chriopp

Reputation: 957

Well, if you need the type string only for representational purposes you could as well overwrite the enum's toString method to return the capitalized version of the value.

public enum SPGenderEnum {

    MALE, FEMALE;

   @Override
   public String toString() {
        // call toString of supertype
        String name =  super.toString();
        // capitalize the uppercase enum value
        return name.substring(0,1) + name.substring(1).toLowerCase();
    }
}

or define the enum values differently in the first place:

public enum SPGenderEnum {

    Male, Female;

}

Upvotes: 0

Mike Strobel
Mike Strobel

Reputation: 25623

If you're using Java 8, the streams API is probably the easiest way to retrieve the values you want:

Stream.of(GenderEnum.values())       // create stream of enum constants
      .map(e -> e.getType())         // for each constant, retrieve getType()
      .collect(Collectors.toList())  // collect results into a list

The expression e -> e.getType() is called a lambda expression, and it is a short-hand way of declaring a very simple function. The map operator applies that function to every element in the stream, replacing each element with the function's return value. In this case, the lambda returns the result of calling e.getType(), where e is assumed to be a GenderEnum value.

Simple lambdas like this one could be rewritten in a more concise form called a method reference, as in @ifly6's answer.

You could collect the results into a String[] instead by replacing collect(Collectors.toList()) with toArray(String[]::new).

Upvotes: 4

xxxvodnikxxx
xxxvodnikxxx

Reputation: 1277

This is what you want, it should also works with call toString() in your case

    SPGenderEnum[]  values = SPGenderEnum.values();

    //to print eg.
    for (SPGenderEnum spgItem : values) {
        System.out.println(spgItem.getType());
    }

    //to store in String array
    String[] strVals = new String[values.length];
    for (int i = 0; i < strVals.length; i++) {
        strVals[i]= values[i].getType();
    }

Upvotes: -1

ifly6
ifly6

Reputation: 5331

Seemingly, you want your enums as a list of strings? That seemingly is what you expect from the constructor call. However it is, if you want that, I would use something like this:

Arrays.stream(YourEnum.values())  // prefer Arrays#stream to Stream#of due to 
                                  // lazy loading and primitive type handling
    .map(YourEnum::toString)      // prefer method reference to lambda
    .collect(Collectors.toList()) // prefer lists to arrays

Upvotes: 3

Related Questions