2 回答

TA貢獻1801條經驗 獲得超16個贊
我問這個問題太蠢了。。。
map.get("RequestsServed") 會給我一個 LinkedTreeMap。我只需要將它解析為我選擇的 HashMap 即可。
將 map.get("RequestsServed") 序列化為 json 并將結果值解析為 HashMap 將輕松為我提供所需的結果。
謝謝大家的寶貴時間。

TA貢獻1776條經驗 獲得超12個贊
我寫了一個虛擬類用于測試目的:
public class TempObject {
private String date;
private Double value;
public TempObject(String date, Double value) {
this.date = date;
this.value = value;
}
public String getDate() {
return date;
}
public Double getValue() {
return value;
}
}
這是您的解決方案:
// Initializing the Map (I'm using LinkedHashMap here for a reason)
Map<String, Object> map = new LinkedHashMap<>();
map.put("RequestsServed", new TempObject("2019-06-28T00:00:00Z", 0.0));
map.put("PullRequests", new TempObject("2019-06-28T00:00:00Z", 0.1));
// Using Collectors.toMap() with mergeFunction (to handle duplicate keys)
Map<String, Object> result = map.values().stream().map(object -> (TempObject) object).collect(Collectors.toMap(TempObject::getDate, TempObject::getValue, (existingValue, newValue) -> newValue));
System.out.println(result); // printing the value of result map: {2019-06-28T00:00:00Z=0.1}
您還可以將此 String 值解析為LocalDateTime.
添加回答
舉報