HashMap根据key值对集合进行排序

时间:2022-11-04 19:19:23
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


public class HashMapSortByKey {
public static void main(String args[]) {
Map<String, String> maps = new HashMap<String, String>();
maps.put("2010-09-11", "当前日期:2010-09-11");
maps.put("2014-11-24", "当前日期:2014-11-24");
maps.put("2008-07-07", "当前日期:2008-07-07");
maps.put("2015-12-22", "当前日期:2015-12-22");
maps.put("2009-02-19", "当前日期:2009-02-19");
maps.put("2011-06-05", "当前日期:2011-06-05");
maps.put("2004-10-30", "当前日期:2004-10-30");
Object[] key = maps.keySet().toArray();
Arrays.sort(key);
for (int i = 0; i < key.length; i++) {
System.out.println("排序后:" + maps.get(key[i]));
}
}
}