英文:
How can I sort a Map<LocalDate, Integer>?
问题
我只想知道如何按时间顺序对一个Map<LocalDate,Integer>进行排序。
这是我代码的第一行:
Map<LocalDate, Integer> commitsPerDate = new HashMap<>();
然后,我会用任何值填充我的Map(但不是按时间顺序)。当我显示Map的值时,它是按照以下顺序显示的:
for(var item : commitsPerDate.entrySet()) {
System.out.println(item.getKey() + " = " + item.getValue());
}
2020-08-31 = 1
2020-09-30 = 3
2020-09-29 = 1
2020-09-28 = 5
2020-09-27 = 5
2020-08-27 = 4
2020-09-25 = 3
2020-10-21 = 1
2020-10-18 = 1
2020-10-17 = 5
2020-10-16 = 4
2020-10-15 = 5
2020-10-14 = 6
2020-09-14 = 1
2020-10-13 = 2
2020-09-13 = 2
我希望它按照时间顺序排序,以便显示顺序相同。
谢谢。
英文:
I just want to know how I can sort by chnrological order a Map<LocalDate, Integer>.
Here's the first line of my code :
Map<LocalDate, Integer> commitsPerDate = new HashMap<>();
Afterwards, I fill in my Map with any value (but not in chronological order).
And when I display the values of my Map, it is displayed in this order :
for(var item : commitsPerDate.entrySet()) {
System.out.println(item.getKey() + " = " + item.getValue());
}
2020-08-31 = 1
2020-09-30 = 3
2020-09-29 = 1
2020-09-28 = 5
2020-09-27 = 5
2020-08-27 = 4
2020-09-25 = 3
2020-10-21 = 1
2020-10-18 = 1
2020-10-17 = 5
2020-10-16 = 4
2020-10-15 = 5
2020-10-14 = 6
2020-09-14 = 1
2020-10-13 = 2
2020-09-13 = 2
And I would like it to be sorted in chronological order so that the display is in the same order.
Thank you.
答案1
得分: 2
正如Pshemo已经提到的,如果您希望通过键来对映射排序,您可以使用TreeMap
代替HashMap
。
示例:
import java.time.LocalDate;
import java.util.Map;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Map<LocalDate, Integer> commitsPerDate = new TreeMap<>();
commitsPerDate.put(LocalDate.parse("2020-08-31"), 1);
commitsPerDate.put(LocalDate.parse("2020-09-30"), 3);
commitsPerDate.put(LocalDate.parse("2020-09-29"), 1);
commitsPerDate.put(LocalDate.parse("2020-09-28"), 5);
System.out.println(commitsPerDate);
}
}
输出:
{2020-08-31=1, 2020-09-28=5, 2020-09-29=1, 2020-09-30=3}
按相反顺序:
import java.time.LocalDate;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Map<LocalDate, Integer> commitsPerDate = new TreeMap<>(Collections.reverseOrder());
commitsPerDate.put(LocalDate.parse("2020-08-31"), 1);
commitsPerDate.put(LocalDate.parse("2020-09-30"), 3);
commitsPerDate.put(LocalDate.parse("2020-09-29"), 1);
commitsPerDate.put(LocalDate.parse("2020-09-28"), 5);
System.out.println(commitsPerDate);
}
}
输出:
{2020-09-30=3, 2020-09-29=1, 2020-09-28=5, 2020-08-31=1}
如果出于任何原因您必须使用HashMap
,请参阅https://stackoverflow.com/questions/922528/how-to-sort-map-values-by-key-in-java。
英文:
As Pshemo has already mentioned, if you want the map to order elements by key then you can use TreeMap
instead of HashMap
.
Demo:
import java.time.LocalDate;
import java.util.Map;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Map<LocalDate, Integer> commitsPerDate = new TreeMap<>();
commitsPerDate.put(LocalDate.parse("2020-08-31"), 1);
commitsPerDate.put(LocalDate.parse("2020-09-30"), 3);
commitsPerDate.put(LocalDate.parse("2020-09-29"), 1);
commitsPerDate.put(LocalDate.parse("2020-09-28"), 5);
System.out.println(commitsPerDate);
}
}
Output:
{2020-08-31=1, 2020-09-28=5, 2020-09-29=1, 2020-09-30=3}
In the reverse order:
import java.time.LocalDate;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Map<LocalDate, Integer> commitsPerDate = new TreeMap<>(Collections.reverseOrder());
commitsPerDate.put(LocalDate.parse("2020-08-31"), 1);
commitsPerDate.put(LocalDate.parse("2020-09-30"), 3);
commitsPerDate.put(LocalDate.parse("2020-09-29"), 1);
commitsPerDate.put(LocalDate.parse("2020-09-28"), 5);
System.out.println(commitsPerDate);
}
}
Output:
{2020-09-30=3, 2020-09-29=1, 2020-09-28=5, 2020-08-31=1}
For any reason, if you have to use HashMap
, check https://stackoverflow.com/questions/922528/how-to-sort-map-values-by-key-in-java
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论