Thursday, 19 September 2013

Sort the hashset based on date

Sort the hashset based on date

How can i sort the data inside a hashset based on date
I have a program in which i will get the data from mongodb in this format
2015-01-17 and later i will convert it to MMM dd yyyy format and now how
can i sort the data here ??
This is my program
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;
}
}

No comments:

Post a Comment