26

I have a enum for various operations, I want to iterate through the enum Operations

public enum Operations 
{
create,update,delete,view,compare,login
}

How can i achieve this?

AabinGunz
  • 12,109
  • 54
  • 146
  • 218
  • It is important--not just convenient--to iterate through an enum's values. Doing so prevents future failures when values are added or deleted. – aliteralmind Feb 26 '15 at 17:39

1 Answers1

49
for (Operations op : Operations.values()) {
    ...
}
Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
  • Also when i try `for (Operations op : Operations.values()) { if(arr[0].equalsIgnoreCase(op.name())) { op=Operations.valueOf(arr[0].toLowerCase()); log.info(arr[0].toString()); } }` it does not seem to work – AabinGunz May 30 '11 at 09:59
  • 1
    @Abhishek Simon: What is that code supposed to do? What is `arr[0]`? Why are you reassigning `op`? – Joonas Pulakka May 30 '11 at 10:14
  • arr[0] is coming from parameterMap `String[] arr=(String[])me.getValue();` I am supposing that `op=Operations.valueOf(arr[0].toLowerCase())` means it is the current selected `operation` What i am trying to do is, if parameterMap contains any of the operations then execute set of statements – AabinGunz May 30 '11 at 10:26
  • @Abhishek Simon: The assignment is redundant: you're checking already in the `if` statement whether `arr[0]` equals the current `op`'s name (case ignored), so there you have the current selected operation - it's `op`. How is it not working? Try print statements to see what `arr[0]` actually contains. – Joonas Pulakka May 30 '11 at 10:32