[英]How to format map data by their keys
I'm trying to format the data inside my HashMap according to their key. 我试图根据他们的密钥格式化我的HashMap中的数据。
I have a loop which prints data in the following format 我有一个循环,以以下格式打印数据
icon: "rain"
windBearing: 239
ozone: 339.89
precipType: "rain"
humidity: 0.82
moonPhase: 0.98
windSpeed: 7.37
summary: "Light rain starting in the evening."
visibility: 16.09
cloudCover: 0.62
pressure: 1011.49
dewPoint: 1.26
time: 08-03-2016 00:00:00
temperatureMax: 8.09
All of this data is stored in a HashMap with key (for example) icon and value "rain". 所有这些数据都存储在带有键(例如)图标和值“ rain”的HashMap中。
How can I format all of this data according to their keys? 如何根据其键格式化所有这些数据? I tried something like this 我尝试过这样的事情
private Map formatter(Map data) {
String tmp;
for(int i = 0; i < data.size(); i++) {
if(data.containsKey("temperatureMax")) {
tmp = String.format("%s c TEST", data.get("temperatureMax"));
data.put("temperatureMax", tmp);
}
}
return data;
}
I thought something like this would format 8.09 to simply 8, but it didn't do anything. 我以为这样的话会将8.09格式化为8,但是它什么也没做。 (I attempted to do what the answer here How to update a value, given a key in a java hashmap? states) (我尝试执行此处的答案, 如何在给定Java hashmap中的键的情况下如何更新值? )
This is where I acquire the data. 这是我获取数据的地方。
public Map dailyReport() {
FIODaily daily = new FIODaily(fio);
//In case there is no daily data available
if (daily.days() < 0) {
System.out.println("No daily data.");
} else {
System.out.println("\nDaily:\n");
}
//Print daily data
for (int i = 0; i < daily.days(); i++) {
String[] value = daily.getDay(i).getFieldsArray();
System.out.println("Day #" + (i + 1));
for (String key : value) {
System.out.println(key + ": " + daily.getDay(i).getByKey(key));
dailyData.put(key, daily.getDay(i).getByKey(key));
formatter(dailyData);
}
System.out.println("\n");
}
return dailyData;
}
You should iterate through your map keys using the keySet()
function of Map
您应该使用Map
的keySet()
函数遍历地图键
for (String name:data.keySet()) {
if (name.equals("temperatureMax")) {
//Do your formatting
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.