0

i am trying rewrite the following php code in to java

   foreach ($configValues as $data) {
    $temp['config_key'] = is_null($data->getConfigKey()) ? '' : $data->getConfigKey();
    $temp['config_value'] = is_null($data->getConfigValue()) ? '' : $data->getConfigValue();
    array_push($configArray, $temp);
}
foreach ($configPricing as $data) {
    $temp1['config_key'] = is_null($data->getType()) ? '' : $data->getType();
        $temp1['config_value'] = is_null($data->getPrice()) ? '' : $data->getPrice();
        array_push($configArray, $temp1);
    }

the above code push one associative array in to another how can in write this in to java i tried in this way :

List<String> configList=new ArrayList();

        List<Config> config=configRepository.findAll();
        System.out.println("Config size:"+config.size());
        List<ConfigPricing> configPricing=configPricingRepository.findAll();
        System.out.println("configPricing size:"+configPricing.size());

        Map<String, String> response = new HashMap<String, String>();   
        ListMultimap<String, String> tempHashMap = ArrayListMultimap.create();      
        ArrayList configArray = new ArrayList();    



        for(Config data:config){

            if(data.getConfigKey() !=null && !"".equals(data.getConfigKey()))
            tempHashMap.put("config_key", data.getConfigKey() );

            if(data.getConfigValue() !=null && !"".equals(data.getConfigValue()))
            tempHashMap.put("config_value", data.getConfigValue().isEmpty() ? "" : data.getConfigValue());

        }
//      configArray.add(tempHashMap);

        for(ConfigPricing configPricingData:configPricing){
            if(configPricingData.getType() !=null && !"".equals(configPricingData.getType()))
            tempHashMap.put("config_key", configPricingData.getType().isEmpty() ? "" : configPricingData.getType() );

            if(configPricingData.getPrice() !=null && !"".equals(configPricingData.getPrice()))
            tempHashMap.put("config_value", configPricingData.getPrice().toString().isEmpty() ? "":configPricingData.getPrice().toString());
//          configArray.add(tempHashMap);

        }

but it is storing data something like this:

samekey1:valueone,valuetwo ,talue theree, samekey2:valueone,valuetwo ,talue theree,

insted of this i would like to store those in the following way:

samekey1:value
samekey2:value 

samekey1:value 
samekey2:value 

samekey1:value 
samekey2:value 
1

1 Answer 1

0

This may be useful,

// no key
array_push($configArray, $value);
// same as:
$configArray[] = $value;

// key already known
$configArray[$samekey1] = $value;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.