【发布时间】:2020-05-29 19:22:40
【问题描述】:
我有一张从 Firebase 实时数据库更新的地图,所以我事先不知道地图的大小。
在地图中,我有一个字符串作为键,一个 Joda DateTime 作为值。
我不知道如何遍历地图以返回最近的日期时间。
我会尽量解释得更好:
//on returning results from Realtime Database
Map<String, DateTime> myMap = new HashMap<>();
if(dataSnapshot.exists){
for(DataSnapshot data:dataSnapshot.getChildren()){
String key = data.getKey();
DateTime dateTime = // I get the data and convert it to Datetime; no problem here; I can do it.
myMap.put(key, dateTime);
}
//outside the Loop
//HERE IS WHAT I NEED HELP WITH
for(DateTime date:myMap.values()){
// 1 - check if date is after the next date in map
// 2 - if it is, then keep it
// 3 - if it is not, then remove
// 4 - in the end, only one pair of key/value with the most recent date is left
}
}
你们能帮帮我吗? 非常感谢
编辑:对不起,还有一件事。我在 Android 中使用的最小 sdk 不允许我使用 Java 8。我必须使用 Java 7 功能。
【问题讨论】:
标签: java android hashmap jodatime