3

Today I came across writing this code:

if("Name".equals(fieldName) || "ID".equals(fieldName) 
                 || "password".equals(fieldName) || "email".equals(fieldName)){
     //do something.
}else{
     //for other 76 properties do something.
}

I am comparing fieldName with four properties hence I have written four conditions. Is there a way in Java to combine all four conditions and write it as a single condition which satisfies my requirement. For instance

if(fieldName IN ("Name", "ID", "password", "email"){
    //do something. .
}
.
.
.

Are there any methods or workaround to achieve this?

Mr ASquare
  • 391
  • 1
  • 8
  • 22
  • You could simply your statement to `if (false) { ... }`. Please read http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java to understand why. To answer your real question, Java doesn't have the kind of syntax you're looking for, but you can use a [Set](http://docs.oracle.com/javase/8/docs/api/java/util/Set.html). – ajb Feb 18 '17 at 05:48

4 Answers4

10

You could define a List of possible values that you are interested in and check if the required value is present in the list.

List<String> fieldValues = Arrays.asList("Name","ID","password","email");
if(fieldValues.contains(fieldName)) {

}

One liner : if(Arrays.asList("Name","ID","password","email").contains(fieldName)) { }

Note Don't use == to check for String equality.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
1

First, and most important, this code is incorrect.

    if  ("Name" == fieldName || "ID" == fieldName 
             || "password" == fieldName || "email" == fieldName) {

It is almost always INCORRECT to use == to compare strings.

Having said that, the most efficient (correct) way to write the test is:

switch (fieldName) {
case "Name": case "ID": case "password": case "email":
    // do something
    break;
default:
    // do something else
}

This should use a hash set behinds the scenes if the compiler thinks that is the most efficient way, but the set is only constructed once. With more concise solutions, you will most likely construct and discard a data structure representing a temporary set / list each time you do the test.

Graham
  • 7,431
  • 18
  • 59
  • 84
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

If you are using some static properties that won't change frequently. I would suggest you should populate those properties into a map as same key and value.

if(someMap.get(fieldName)!=null)
{
   //do something
}
else
{
   //do something else
}

You can also write these properties to some file and load them using Properties class at program startup and use it.

Atul
  • 1,536
  • 3
  • 21
  • 37
0

No method I am aware of. It is possible to define and use a function. See below code:

boolean stringInArray(String src, String[] strArray) {
  if (strArray == null || src == null)
    return false;
  for (int i = 0; i < strArray.length; i++) {
    if (src.equals(strArray[i]))
      return true;
  }
  return false;
}

// Print false
System.out.println(stringInArray("John",
  new String[] { "Name", "ID", "password", "email"}));

// Print true
System.out.println(stringInArray("ID",
  new String[] { "Name", "ID", "password", "email"}));
HLie
  • 40
  • 3