我正在尝试将以下php代码重写为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);
}
上面的代码将一个关联数组推入另一个如何将这个写入java中我试过这样:
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);
}
但它存储的数据如下:
samekey1:valueone,valuetwo,talue theree, samekey2:valueone,valuetwo,talue theree,
我想要以下列方式存储这些内容:
samekey1:value
samekey2:value
samekey1:value
samekey2:value
samekey1:value
samekey2:value
答案 0 :(得分:0)
这可能很有用,
// no key
array_push($configArray, $value);
// same as:
$configArray[] = $value;
// key already known
$configArray[$samekey1] = $value;