I want to shorten the if conditions. Is there any alternative other than switch case?
public void check(String name){
String parentFolder = "";
if(name.matches("birds"))
parentFolder = birdPFUuid;
if (name.matches("dogs"))
parentFolder = dogPFUuid;
if (name.matches("cats"))
parentFolder = catPFUuid;
if (name.matches("vehicles"))
parentFolder = vehiclesPFUuid;
}
Thank you
CodePudding user response:
String parentFolder = "";
parentFolder = (((name.matches("birds"))?"birdPFUuid":
(name.matches("dogs"))?"dogsFUuid":
(name.matches("cats"))?"catsFUuid":
(name.matches("vehicles"))?"vehiclesFUuid":""));
CodePudding user response:
You can use a map data structure to map the parent folder UUID to the dataset name.
Map<String, String> uuidMap = new HashMap<>();
uuidMap.put("birds", birdPFUuid);
uuidMap.put("dogs", dogPFUuid);
uuidMap.put("cats", catPFUuid);
uuidMap.put("vehicles", vehiclesPFUuid);
public void check(String name){
String parentFolder = uuidMap.get(name);
}