Reputation: 135
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("");
}
}
Upvotes: 1
Views: 84
Reputation: 15310
There isn't so much to be done here, but maybe a switch
can be more aesthetically pleasing. Though you will have to make sure your input is either only lowercase or only uppercase:
for(String element:myList) {
switch (element.toLowerCase()){
case "employeeid": p.setemployeeID(""); break;
case "firstname" : p.setfirstname(""); break;
.
.
.
}
}
If you decide you wish to keep your if
design, you should definitely use if-else
so you won't have to run through all your if
s in every iteration.
And as a final note, this probably isn't the best way to design whatever it is you are trying to do. You should consider something of a more OO nature, like passing the parameters of a Person
object to the constructor when you instantiate a Person
object.
Edit: I see you want to use an Enum
, you would want to actually define it like so:
public enum MyEnum {
EMPLOYEE_ID("employeeid"),
FIRST_NAME("firstname")
.
.
.
;
private final String text;
private MyEnum(final String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
}
Upvotes: 3
Reputation: 36
I didn´t test, but (if the string and the method name are the same) you could use reflection, something like this:
Person p = new Person();
for(String element : myList) {
// method params
Class[] paramTypes = new Class[1];
paramTypes[0] = String.class;
// method
String methodName = "set" + element.indexOf(0,1).toUpperCase() + element.indexOf(1);
Method m = c.getDeclaredMethod(methodName, paramTypes);
// call method
m.invoke(p, "");
}
Upvotes: 0
Reputation: 2319
The compiler is able to optimize switch statements into a lookup table and perform compile-time checking for literals when dealing with enumerations. That being said, you should always use switch statements for over 3 comparisons. Hence your code becomes-
private static final String EMPLOYEE_ID = "employeeid";
private static final String FIRST_NAME = "firstname";
private static final String LAST_NAME = "lastName";
private static final String GENDER = "gender";
private static final String MOBILE_NO = "mobileno";
private static final String EMAIL_ID = "emailid";
Person p =new Person ();
for(String element: myList) {
switch (element.toLowerCase()) {
case EMPLOYEE_ID: p.setemployeeID("");
break;
case FIRST_NAME: p.setfirstName("");
break;
case LAST_NAME: p.setlastName("");
break;
case GENDER: p.setgender("");
break;
case MOBILE_NO: p.setMobileNo("");
break;
case EMAIL_ID: p.setEmailID("");
break;
}
}
Upvotes: 3