如何根据日期对哈希集中的数据进行排序
我有一个程序,我将在其中以这种格式从 mongodb 获取数据2015-01-17
,稍后我会将其转换为MMM dd yyyy
格式,现在如何在这里对数据进行排序?
这是我的程序
public class MyObject {
public static void main(String args[]) {
String sym = "BAC";
Set<String> set = myDAO.getInstance().getMeAllExpirationDates(sym);
for (String String : set) {
System.out.println(String);
}
}
public Set<String> getMeAllExpirationDates(String sym) throws Exception {
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("MMM dd yyyy");
Set<String> set = new HashSet<String>();
BasicDBObject query = new BasicDBObject();
query.put("symbol", sym);
Security sec = null;
DBCursor cursor = collection.find(query);
while (cursor.hasNext()) {
sec = (Security) cursor.next();
Date date = sdf1.parse(sec.getTkExpirationDate());
String result = sdf2.format(date);
set.add(result);
}
return set;
}
}