I need to get userId value from the map and that can be present either in user1 key or user2 key or user3 key or user4 key in the map. Only one key will be present in the map at a time, no multiple keys will be present.
So I started off with below code which checks if user1 is present or not and if it is present extract user1 key value otherwise extract user2 key value. So should I keep adding user3 and user4 check as well in it? If yes it will become very long..
Map<String, String> holder = Utils.parseStringToMap(payload);
String userId = holder.containsKey("user1") ? holder.get("user1") : holder.get("user2");
Is there any better way by which I can extract userId value depending on what key is present? I am using Java 7.
Note: here key name in the map for that field can be any name. It won't be userx where x is a number so I cannot use for loop logic here.