I want to get the key from value / and value from key ,can anyone help to do in better way ? or can we implement the below code using java enum or spring or apache enumutils or java generic ?
Note : I used to get string from frond end(.jsp) and I want to store store this value into database table as integer.
Frond end Database table
Admin --- 1
Manager --- 2
Employee --- 3
import java.util.HashMap;
import java.util.Map;
public class MyRole {
static HashMap<Integer,String> result= new HashMap<Integer,String>();
static{
result.put(1,"Admin");
result.put(2,"Manager");
}
public static int getId(String role){
Integer key=-1;
for(Map.Entry entry: result.entrySet()){
if(role.equals(entry.getValue())){
key = (Integer) entry.getKey();
break;
}
}
return key;
}
public static String getRole(int id){
return result.get(id);
}
public static void main(String args[]){
String result=MyRole.getRole(1);
System.out.println("role name"+result);
int roleId=MyRole.getId("Admin");
System.out.println("role id "+roleId);
}
}
I want make sure , whether the above program is fine or can we achieve via Java enum or generic or spring api or apache enumutils api ?