Skip to main content
Notice removed Draw attention by candied_orange
Bounty Ended with JimmyJames's answer chosen by candied_orange
deleted 29 characters in body
Source Link
candied_orange
  • 119.7k
  • 27
  • 233
  • 369

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. Please someone help on this.

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");
}

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. Please someone help on this.

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");
}

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");
}
Notice added Draw attention by candied_orange
Bounty Started worth 100 reputation by candied_orange
Source Link

Map same POJO values to different variables based on Key value without switch case

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. Please someone help on this.

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");
}