怎么对一个Map<LocalDate, Integer>进行排序?

huangapple go评论91阅读模式
英文:

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&lt;LocalDate, Integer&gt; commitsPerDate = new HashMap&lt;&gt;();

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() + &quot; = &quot; + 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&lt;LocalDate, Integer&gt; commitsPerDate = new TreeMap&lt;&gt;();
		commitsPerDate.put(LocalDate.parse(&quot;2020-08-31&quot;), 1);
		commitsPerDate.put(LocalDate.parse(&quot;2020-09-30&quot;), 3);
		commitsPerDate.put(LocalDate.parse(&quot;2020-09-29&quot;), 1);
		commitsPerDate.put(LocalDate.parse(&quot;2020-09-28&quot;), 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&lt;LocalDate, Integer&gt; commitsPerDate = new TreeMap&lt;&gt;(Collections.reverseOrder());
		commitsPerDate.put(LocalDate.parse(&quot;2020-08-31&quot;), 1);
		commitsPerDate.put(LocalDate.parse(&quot;2020-09-30&quot;), 3);
		commitsPerDate.put(LocalDate.parse(&quot;2020-09-29&quot;), 1);
		commitsPerDate.put(LocalDate.parse(&quot;2020-09-28&quot;), 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

huangapple
  • 本文由 发表于 2020年10月25日 18:29:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64522681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定