developer
developer

Reputation: 9478

need suggestion in optimizing below code

I have an array and length is 3 but it may or may not contain data in 3 index , i have to append three queries based on the data in the array. Let me put an example.

array[0]="A";
array[1]="B";
array[2]="C";
String strqry1 = "so and so";
String strqry2 = "so and so";
String strqry3 = "so ans so";
String mainQuery = "";
int arrayLength = array.legth;
if(arrayLength == 1 ){
   if(array[0].equals("A")){
     mainQuery = strqry1 ;
   }else if(array[0].equals("B")){
     mainQuery = strqry2 ;
   }else if(array[0].equals("c")){
     mainQuery = strqry3 ;
   }
}
if(arrayLength == 2){
   if(array[0].equals("A") && array[1].equals("B")){
      mainQuery = strqry1 +"union all"+ strqry2 ;
   }else if(array[0].equals("A") && array[1].equals("C")){
      mainQuery = strqry1 +"union all"+ strqry3 ;
   }else if(array[0].equals("B") && array[1].equals("C")){
      mainQuery = strqry2 +"union all"+ strqry3 ;
   }
}
if(arrayLength == 3){
   if(array[0].equals("A") && array[1].equals("B") && array[2].equals("C")){
      mainQuery = strqry1 +"union all"+strqry2 +"union all"+ strqry3 ;
   }
}

I need suggestion on , do i need write such permutation and combinations for the 3 sets of If conditions. Any other way i can optimize the code. Please suggest me.

Upvotes: 0

Views: 78

Answers (3)

Sorin Totuarez
Sorin Totuarez

Reputation: 114

Do you have to use Arrays? It seems more plausible to use a Java Hashmap to do, what you want: Link "A" to strqry1, "B" to strqry2 and "C" to strqry3.

import java.lang.StringBuilder;
import java.util.Hashmap;

Map<String, String> myQuery = new Hashmap<String,String>();
myQuery.put("A", strqry1);
myQuery.put("B", strqry2);
myQuery.put("C", strqry3);

and then build the needed query:

StringBuilder sb = new StringBuilder();
String queryString;

queryString = myQuery.get(array[0]);
if(queryString != null) {
    sb.append(queryString);
}

// now skip the first entry, proceed the rest
for(int i=1; i<array.length; i++) {
    sb.append(" union all "); // If you want to build a query, watch the whitespaces
    sb.append(queryString);
}

This will construct the necessary string without any superfluous "union all" in the string. Your result query will be stored in the StringBuilder sb.

Upvotes: 1

Tomas Narros
Tomas Narros

Reputation: 13468

If there's a correlation between the order of the input values, the constants to check, and the resulting query, you could handle these as arrays also:

    //constant values to check
    String[] constants={"A", "B", "C"};
    String[] queries={"so and so 1", "so and so 2", "so and so 3"};

    String mainQuery = "";
    String union="";

    for(int i=0; i<array.length && i<constants.length; i++) {

        if(array[i].equals(constants[i])) {
            mainQuery=mainQuery+union+queries[i]+" ";
            union=" union all ";
        }
    }

    System.out.println(mainQuery);

New constants and queries could be appended to them, dinamically (as input parameters or object properties...). Also, if you can define them as external configurable values (via injection, properties files, etc), you could change them without rewriting and redeploying your code.

Upvotes: 1

talex
talex

Reputation: 20436

I suggest to make method that check one value and return query if value matches. Someting like this.

String check(String value) {
    if("A".equals(value)) return strqry1;
    if("B".equals(value)) return strqry2;
    if("C".equals(value)) return strqry3;
    return null;
}

And then call this function in loop:

String result = "";
for(String value: array) {
    String qry = check(value);
    if(qry==null) continue;
    if(!gry.isEmpty()) gry += "union all";
    result += qry;
}

Upvotes: 2

Related Questions