我想排序一组日期值对。我的密钥是日期和值是一个字符串。所以我选择了一个TreeMap。
现在,
以下迭代器是否按照类似于TreeMaps的关键字进行排序。我尝试了几个循环,但仍然有疑问
Iterator iter = policyMap.keySet().iterator();
有什么办法可以在不增加迭代器索引的情况下获得下一个键。
在java 6之前是否有此policyMap.higherKey(cpDate))的等号方式。
最后我为我的自我感到惭愧,我也试着追随。
TreeMap policySubMap =
new TreeMappolicyMap.tailMap(cpDate));
policySubMap.remove(policySubMap.firstKey());
System.out.println(" | amount > " + policySubMap.firstKey());这是我完整的代码:
public void controller(){
TreeMap policyMap = new TreeMap();
Calendar cal = Calendar.getInstance();
policyMap.put(addDate(cal, 2).getTime(), "Amount is 10");
policyMap.put(addDate(cal, 10).getTime(), "Amount is 10");
policyMap.put(addDate(cal, -10).getTime(), "Amount is -10");
policyMap.put(addDate(cal, 11).getTime(), "Amount is 11");
policyMap.put(addDate(cal, -11).getTime(), "Amount is -11");
policyMap.put(addDate(cal, -12).getTime(), "Amount is -12");
Iterator iter = policyMap.keySet().iterator();
while (iter.hasNext()) {
Date cpDate = iter.next();
System.out.print("From "+cpDate + " to " + policyMap.get(cpDate));
// if(iter.hasNext())System.out.println(" | amount > " + policyMap.higherKey(cpDate)); // This is not supporting in before java 6
if(iter.hasNext()){
TreeMap policySubMap = new TreeMap(policyMap.tailMap(cpDate));
policySubMap.remove(policySubMap.firstKey());
System.out.println(" | amount > " + policySubMap.firstKey());
}
else System.out.println("Checking date");
}
}
public Calendar addDate(Calendar cal, int amount) {
cal.add(Calendar.DATE, amount);
return cal;
}