0

Based on the string "key", I want to get the latest and previous values assigned without using the Switch case. I need some mapper method that it get the key value as input and map the values for different Data objects and return a list of latest and previous values without using the switch case.

switch(key) {
        
        case "pValue" :
            latestPValue = getLatestData().getPValue();
            previousPValue = getPreviousData().getPValue();
            break;
            
        case "aValue" :
            latestAValue = getLatestData().getAValue();
            previousAValue = getPreviousData().getAValue();
            break;
            
        case "tValue" :
            latestTValue = getLatestData().getTValue();
            previousTValue = getPreviousData().getTValue();
            break;
            
        default:
            break;
        }


public class Data {
    
    public String aValue;
    public String pValue;
    public String tValue;

---- Constructors, getters and setters---

}

public class MainClass {
public static void main(String args[]) {

Data latestData = new Data("1","2","3");
Data previousData = new Data("4","5","6");
}
2
  • 3
    Why do you not want to use a basic language construct for exactly the purpose that it serves? There is something here that you're not telling us. Please edit your question to add the reason. Mar 15 at 8:52
  • 1
    Any better ways depend on information you are not giving. Where does the the "key" string come from? Why is it a string? Why is the Data class built up like that? Why not use a Map?
    – RoToRa
    Mar 20 at 18:36
3

Well you can do it with an if-else-if chain but... yuck.

If we can ditch the variables and do some restructuring*:

latestValueMap.put(key, getLatestData().getValue(key));  
previousValueMap.put(key, getPreviousData().getValue(key));

* Assumes a, p, and t values are all of the same type. A detail not made clear in the question.

This allows you to control moving the information the same way, with the key. It’s held differently but it’s all still there.

The advantage is that it’s now simpler to introduce new key values since knowledge of key values is isolated. Something the switch can’t do for you.

1
+100

Without changing your POJO approach you can use function references to accomplish this:

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class MainClass
{
  static Data previousData = new Data("4","5","6");
  static Data latestData = new Data("1","2","3");
  static Map<String, Function<Data, String>> map = new HashMap<>();
  
  static {
    map.put("a", Data::getAValue);
    map.put("p", Data::getPValue);
    map.put("t", Data::getTValue);
  }
  
  public static void main(String args[]) {
    display("a");
    display("p");
    display("t");
  }
  
  private static void display(String key) {
    Function<Data, String> func = map.get(key);
    System.out.println(key + " " 
        + func.apply(latestData) + " <- " 
        + func.apply(previousData));
  }
}

class Data {
  private String aValue;
  private String pValue;
  private String tValue;
  
  Data(String aValue, String pValue, String tValue)
  {
    this.aValue = aValue;
    this.pValue = pValue;
    this.tValue = tValue;
  }

  public String getAValue()
  {
    return aValue;
  }

  public String getPValue()
  {
    return pValue;
  }

  public String getTValue()
  {
    return tValue;
  }
}

I want to caution you that this seems like overkill for this situation. The 'right' answer is to stop using POJOs (a misnomer as well as a terrible approach) and do something maybe along the lines of what candied_orange suggests.

That said, perhaps you don't have that as an option. In the case where you can't modify the design of classes like this, or don't have the time to do so, this is a nice technique to know.

0

What do you think about the following approach:

public class Data {
    
    public HashMap <String,String> data;
    
    public Data(String a, String p, String t){
    
    data = new HashMap();
    data.put("a", a);
    data.put("p", p);
    data.put("t", t);
    }

    public String getValue(String key){
         return data.get(key);
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.