我正在尝试根据日期对哈希地图进行排序。 (只有月份和日期,年份与排序无关,但与密钥相关)。 我的哈希地图:
static HashMap<Date, List<String>> Hash = new HashMap<Date, List<String>>();
static List<String> li = new ArrayList<String>();
逻辑:
public static void main(String[] args){
li.add("1");
li.add("2");
li.add("3");
li.add("4");
li.add("5");
Date D1 = funx("1999-04-04");
Date D2 = funx("1999-01-03");
Date D3 = funx("1999-03-03");
Date D4 = funx("1999-08-05");
Date D5 = funx("1999-12-25");
Hash.put(D1, li);
Hash.put(D2, li);
Hash.put(D3, li);
Hash.put(D4, li);
Hash.put(D5, li);
for (Map.Entry<Date, List<String>> entry : Hash.entrySet()) {
Date key = entry.getKey();
List value = entry.getValue();
System.out.println("The key: "+key);
System.out.println("The Comparison: "+compx(key));
}
}
public static Date funx(String S){
String DateStr = S;
Date d = null;
try {
d = new SimpleDateFormat("yyyy-MM-dd").parse(DateStr);
} catch (ParseException e) {
e.printStackTrace();
}
java.sql.Date d1 = new java.sql.Date(d.getTime());
return d1;
}
public static int compx(Date o){
Calendar cal1 = Calendar.getInstance();
cal1.setTime(getCurrentDate());
Calendar cal2 = Calendar.getInstance();
cal2.setTime(o);
int month1 = cal1.get(Calendar.MONTH)+1;
int month2 = cal2.get(Calendar.MONTH)+1;
System.out.println("Current Month: "+month1);
System.out.println("Comparing With Month: "+month2);
if(month1 < month2)
return -1;
else if(month1 == month2) {
System.out.println("Current Month"+cal1.get(Calendar.DAY_OF_MONTH));
System.out.println("Is ot be subtracted"+cal2.get(Calendar.DAY_OF_MONTH));
return cal1.get(Calendar.DAY_OF_MONTH) - cal2.get(Calendar.DAY_OF_MONTH);
}
else return 1;
}
public static Date getCurrentDate() {
String formattedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
String DateStr = formattedDate;
Date d = null;
try {
d = new SimpleDateFormat("yyyy-MM-dd").parse(DateStr);
} catch (ParseException e) {
e.printStackTrace();
}
java.sql.Date dx = new java.sql.Date(d.getTime());
return dx;
}
}
现在我得到返回值,用于比较,但是如何重新构建Map以便对其进行排序?
答案 0 :(得分:3)
HashMaps本身并不是有序的,因此取出元素,对它们进行排序,然后将它们放回HashMap中将删除顺序。您可以使用TreeMap(已订购),或使用List或其他一些有序机制。您甚至可以根据您的要求执行更复杂的操作,例如键列表(维护顺序)和键,对象(用于快速检索)。
答案 1 :(得分:3)
要进行排序,请使用TreeMap
和自定义比较器来考虑月和日,
Map<Date, List<String>> Hash= new TreeMap<>(new Comparator<Date>() {
@Override
public int compare(Date o1, Date o2) {
Calendar first = Calendar.getInstance();
first.setTime(o1);
Calendar second = Calendar.getInstance();
second.setTime(o2);
int month1 = first.get(Calendar.MONTH);
int month2 = second.get(Calendar.MONTH);
if (month1 != month2) {
return month1 - month2;
} else {
int day1 = first.get(Calendar.DATE);
int day2 = second.get(Calendar.DATE);
return day1 - day2;
}
}
});
public static void main(String[] args) {
List<String> li = new ArrayList<String>();
li.add("1");
li.add("2");
li.add("3");
li.add("4");
li.add("5");
Map<Date, List<String>> Hash = new TreeMap<>(new Comparator<Date>() {
@Override
public int compare(Date o1, Date o2) {
Calendar first = Calendar.getInstance();
first.setTime(o1);
Calendar second = Calendar.getInstance();
second.setTime(o2);
int month1 = first.get(Calendar.MONTH);
int month2 = second.get(Calendar.MONTH);
if (month1 != month2) {
return month2 - month1;
} else {
int day1 = first.get(Calendar.DATE);
int day2 = second.get(Calendar.DATE);
return day2 - day1;
}
}
});
Date D1 = funx("1999-04-04");
Date D2 = funx("1999-01-03");
Date D3 = funx("1999-03-03");
Date D4 = funx("1999-08-05");
Date D5 = funx("1999-12-25");
Hash.put(D1, li);
Hash.put(D2, li);
Hash.put(D3, li);
Hash.put(D4, li);
Hash.put(D5, li);
for (Map.Entry<Date, List<String>> entry: Hash.entrySet()) {
System.out.println( entry.getKey() +" "+entry.getValue());
}
}
public static Date funx(String S) {
String DateStr = S;
Date d = null;
try {
d = new SimpleDateFormat("yyyy-MM-dd").parse(DateStr);
} catch (ParseException e) {
e.printStackTrace();
}
java.sql.Date d1 = new java.sql.Date(d.getTime());
return d1;
}
1999-12-25 [1, 2, 3, 4, 5]
1999-08-05 [1, 2, 3, 4, 5]
1999-04-04 [1, 2, 3, 4, 5]
1999-03-03 [1, 2, 3, 4, 5]
1999-01-03 [1, 2, 3, 4, 5]