0

I have an enum with custom values:

enum DaysOfExercise{

   MONDAY ("legs Workout"),
   WEDNESDAY ("back workout"),
   SATURDAY ("running") ;

   private String exercise ; 

   private DaysOfExercise(String exercise){
       this.exercise = exercise ;
   }

   public String getExercise(){
       return this.exercise ;
   }
}

I need to add a search feature that returns a DaysOfExercise based on an entered exercise name.

I know that there is is the .values() method in the Enum to return the list of DaysOfExercise values to easily iterate over, but in my case I want to return the embedded values list to compare with what the user has typed in.

Is there any built-in method that could return the list of the enum custom values instead of the enum values?

Note: It is not that I'm stuck with this problem. I can easily solve it with a couple of loops. I'm just looking for an optimized solution.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
redd77
  • 307
  • 5
  • 11

1 Answers1

5

Use a stream to map an array of enum values to a list of strings.

List<String> exercises = Stream.of(DaysOfExercise.values())
    .map(DaysOfExercise::getExercise)
    .collect(Collectors.toList());
John Kugelman
  • 349,597
  • 67
  • 533
  • 578