【发布时间】:2015-01-25 16:37:02
【问题描述】:
我正在尝试将以下 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
【问题讨论】:
-
查看此链接以了解 java 数组推送方法:stackoverflow.com/questions/16240014/…