gpt4 book ai didi

java - 根据日期对 HashMap 进行排序,如何将其重建回来?

转载 作者:行者123 更新时间:2023-12-01 08:07:23 33 4
gpt4 key购买 nike

我正在尝试根据日期对 HashMap 进行排序。 (仅月和日,年与排序无关,但与键相关)。我的 HashMap :

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 以便对其进行排序?

最佳答案

要进行排序,请使用 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]

关于java - 根据日期对 HashMap 进行排序,如何将其重建回来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20468470/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com