I have an ArrayList<String> and I need to loop through it, and what is present in the list has to perform certain actions. I wish to optimize the iteration and comparisons, can it be done?
My code:
Person class (includes getters and setters):
private String employeeID;
private String firstName;
private String lastName;
private String gender;
private String mobileNo;
private String emailID;
My computed ArrayList, myList- [firstName, gender, mobileNo, emailID]. How do I optimize my logic shown below to avoid so many if conditions?
Person p =new Person ();
for(String element:myList)
{
if("employeeid".equalsIgnoreCase(element))
{
p.setemployeeID("");
}
if("firstname".equalsIgnoreCase(element))
{
p.setfirstName("");
}
if("lastName".equalsIgnoreCase(element))
{
p.setlastName("");
}
if("gender".equalsIgnoreCase(element))
{
p.setgender("");
}
if("mobileno".equalsIgnoreCase(element))
{
p.setMobileNo("");
}
if("emailid".equalsIgnoreCase(element))
{
p.setEmailID("");
}
}