排序键是哈希图中的日期条目

发布于 2024-11-14 08:23:15 字数 521 浏览 3 评论 0原文

我有一个 hashMap,它具有以下值作为键 value(sql date , integer) 对:

a.put("31-05-2011",67);
a.put("01-06-2011",89);
a.put("10-06-2011",56);
a.put("25-05-2011",34);

当我尝试使用以下命令根据键对 hashMap 进行排序时: 地图modified_a=new TreeMap(a); 并显示键,如下所示:

01-06-2011,10-06-2011,25-05-2011, 31-05-2011

但我希望对键进行排序,因为

31-05-2011,25-05-2011,01-06-2011 ,10-06-2011

我可以看到这些值是根据前 2 位数字(即日期值)进行排序的,但我还需要考虑月份值首先根据月份排序,然后针对每个月对相应的日期进行排序。 有什么线索吗??

I have a hashMap which has the following values as key value(sql date , integer) pairs:

a.put("31-05-2011",67);
a.put("01-06-2011",89);
a.put("10-06-2011",56);
a.put("25-05-2011",34);

when i try to sort the hashMap based on the keys using :
Map modified_a=new TreeMap(a);
and display the keys it is as follows :

01-06-2011,10-06-2011,25-05-2011, 31-05-2011

but I want the keys to be sorted as

31-05-2011,25-05-2011,01-06-2011 ,10-06-2011

I can see that the values are being sorted based on the first 2 digits( which is the date value) but I need the month value to also be considered and sort based on months first and then for each month sort the corresponding days.
Any clues ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

白昼 2024-11-21 08:23:15

你可以使用像

Map<Date, Integer> m = new HashMap<Date, Integer>(); 

    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");

    m.put(new java.sql.Date(dateFormat.parse("31-05-2011").getTime()),67);
    m.put(new java.sql.Date(dateFormat.parse("01-06-2011").getTime()),89);
    m.put(new java.sql.Date(dateFormat.parse("10-06-2011").getTime()),56);
    m.put(new java.sql.Date(dateFormat.parse("25-05-2011").getTime()),34);


    Map<Date, Integer> m1 = new TreeMap(m);
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

    for (Map.Entry<Date, Integer> entry : m1.entrySet())
    {
        System.out.println(df.format(entry.getKey()));
    }

You can use like

Map<Date, Integer> m = new HashMap<Date, Integer>(); 

    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");

    m.put(new java.sql.Date(dateFormat.parse("31-05-2011").getTime()),67);
    m.put(new java.sql.Date(dateFormat.parse("01-06-2011").getTime()),89);
    m.put(new java.sql.Date(dateFormat.parse("10-06-2011").getTime()),56);
    m.put(new java.sql.Date(dateFormat.parse("25-05-2011").getTime()),34);


    Map<Date, Integer> m1 = new TreeMap(m);
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

    for (Map.Entry<Date, Integer> entry : m1.entrySet())
    {
        System.out.println(df.format(entry.getKey()));
    }
沫雨熙 2024-11-21 08:23:15

IMO 的最佳解决方案是对键使用不同的数据类型 - 该数据类型实际上表示日期,并按自然日期顺序排序。除非另有限制,否则我会使用 Joda TimeLocalDate 类型,该类型准确地代表您想要的内容(只是日期,而不是日期/时间等)。

如果您确实想使用字符串键但可以更改它们的格式,则可以使用 yyyy-MM-dd 格式,这自然是可排序的。

或者,您可以将 Comparator 传递给 TreeMap 构造函数,其中比较器在需要时解析两个字符串。比较它们,并根据解析出的年/月/日值进行比较。不过,没有一个构造函数可以同时接受自定义比较器和现有地图,因此您需要类似以下内容:

Map<String, Integer> modified = new TreeMap<String, Integer>(customComparator);
modified.putAll(a);

如果您有大量数据,则此方法将相对较慢(由于重复解析),而且写起来有点繁琐 - 如果可以的话,我会使用更合适的数据类型。

The best solution IMO would be to use a different data type for the keys - a data type which actually represents a date, and which sorts in natural date order. Unless otherwise constrained, I'd use Joda Time's LocalDate type, which represents exactly what you want (just a date, not a date/time etc).

If you really want to use string keys but can change the format of them, you could use a yyyy-MM-dd format, which is naturally sortable.

Alternatively, you could pass in a Comparator<String> to the TreeMap constructor, where the comparator is one which parses two strings when it's asked to compare them, and performs the comparison based on the parsed year/month/day values. There isn't a constructor which takes both a custom comparator and an existing map though, so you'd need something like:

Map<String, Integer> modified = new TreeMap<String, Integer>(customComparator);
modified.putAll(a);

This approach will be relatively slow if you have a lot of data (due to the repeated parsing), and slightly fiddly to write - I'd use a more appropriate data type if you possibly can.

谜兔 2024-11-21 08:23:15

我需要对日期进行反向排序(首先是最近的日期)。我通过使用下面的代码使其工作:

Map<Date, Integer> dateMap = new TreeMap<Date, Integer>(new Comparator<Date>() {
    public int compare(Date date1, Date date2) {
        return date2.compareTo(date1);
    }
});

调用 dateMap.keySet() 将生成一个带有键的 Set,其中首先返回最近的日期。

I had a requirement to reverse sort the dates (most recent date first). I made it work by using the code below:

Map<Date, Integer> dateMap = new TreeMap<Date, Integer>(new Comparator<Date>() {
    public int compare(Date date1, Date date2) {
        return date2.compareTo(date1);
    }
});

Calling dateMap.keySet() will result in a Set with keys, in which the most recent dates are returned first.

清风不识月 2024-11-21 08:23:15

您必须将自定义比较器传递给 TreeMap 构造函数,它将您的键作为日期而不是字符串进行比较(或使用 java.util.Date 作为键,在这种情况下,它将立即发生,因为日期实现了 Comparable)。

You have to pass a custom comparator to the TreeMap constructor, that will compare your keys as dates instead of strings(or use java.util.Date as key, in which case it will happen out of box as date implements Comparable).

┊风居住的梦幻卍 2024-11-21 08:23:15

创建比较器:

public class DateComparator implements Comparator<Date> {
    public int compare(Date date1, Date date2) {
        return date1.compareTo(date2);
    }
}

并将比较器与 TreeMap 结合使用,

Map<Date, Integer> comparedDates = new TreeMap<Date, Integer>(new DateComparator());
// here fill you <Date, Integer> map like:
comparedDates.put(new Date(System.currentTimeMillis()), 123);

Map 中的所有日期都将被排序。

Create comparator:

public class DateComparator implements Comparator<Date> {
    public int compare(Date date1, Date date2) {
        return date1.compareTo(date2);
    }
}

And use comparator with TreeMap

Map<Date, Integer> comparedDates = new TreeMap<Date, Integer>(new DateComparator());
// here fill you <Date, Integer> map like:
comparedDates.put(new Date(System.currentTimeMillis()), 123);

All dates in you Map will be sorted.

枕头说它不想醒 2024-11-21 08:23:15

您可能想要使用 TreeMap 而不是 HashMap 并使用提供排序的自定义 Comparator 创建 Map。

这是匿名比较器的草稿(不会将字符串解析为可比较的日期对象):

new Comparator<String>() {

    @Override
    public int compare(String date1, String date2) {
        // skipping tests! Assuming, all date are well formatted

        String[] parts1 = date1.split("-");
        String[] parts2 = date2.split("-");

        String reordered1 = parts1[2] + parts1[1] + parts1[0];
        String reordered2 = parts2[2] + parts2[1] + parts2[0];

        return reordered1.compareTo(reordered2);
    }
}

You may want to use a TreeMap instead of a HashMap and create the Map with a custom Comparator that provides the sorting.

Here's a draft for an anonymous comparator (that does not parse the String into a comparable date object):

new Comparator<String>() {

    @Override
    public int compare(String date1, String date2) {
        // skipping tests! Assuming, all date are well formatted

        String[] parts1 = date1.split("-");
        String[] parts2 = date2.split("-");

        String reordered1 = parts1[2] + parts1[1] + parts1[0];
        String reordered2 = parts2[2] + parts2[1] + parts2[0];

        return reordered1.compareTo(reordered2);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文