Month Sorting in Java using HashMap

Program:

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()));
		}
	}
}

Output:

Jan
Apr
Sep
Dec

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

534 total views, 0 views today

Leave a Reply

Your email address will not be published. Required fields are marked *