Month Sorting in Java using HashMap

Program:

[java]package agn;

import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Sorting {

public static void main(String[] args) throws ParseException {
Map<Date, Integer> m = new HashMap<Date, Integer>();
DateFormat dateFormat = new SimpleDateFormat("MMM");
m.put(new java.sql.Date(dateFormat.parse("Apr").getTime()),67);
m.put(new java.sql.Date(dateFormat.parse("Dec").getTime()),89);
m.put(new java.sql.Date(dateFormat.parse("Jan").getTime()),56);
m.put(new java.sql.Date(dateFormat.parse("Sep").getTime()),34);
Map<Date, Integer> m1 = new TreeMap(m);
DateFormat df = new SimpleDateFormat("MMM");
for (Map.Entry<Date, Integer> entry : m1.entrySet())
{
System.out.println(df.format(entry.getKey()));
}
}
}[/java]

Output:

Jan
Apr
Sep
Dec

Thanks for reading this post……….!!!

Leave a Reply