排序键是哈希图中的日期条目
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你可以使用像
You can use like
IMO 的最佳解决方案是对键使用不同的数据类型 - 该数据类型实际上表示日期,并按自然日期顺序排序。除非另有限制,否则我会使用 Joda Time 的
LocalDate
类型,该类型准确地代表您想要的内容(只是日期,而不是日期/时间等)。如果您确实想使用字符串键但可以更改它们的格式,则可以使用 yyyy-MM-dd 格式,这自然是可排序的。
或者,您可以将
Comparator
传递给TreeMap
构造函数,其中比较器在需要时解析两个字符串。比较它们,并根据解析出的年/月/日值进行比较。不过,没有一个构造函数可以同时接受自定义比较器和现有地图,因此您需要类似以下内容:如果您有大量数据,则此方法将相对较慢(由于重复解析),而且写起来有点繁琐 - 如果可以的话,我会使用更合适的数据类型。
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 theTreeMap
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: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.
我需要对日期进行反向排序(首先是最近的日期)。我通过使用下面的代码使其工作:
调用
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:
Calling
dateMap.keySet()
will result in aSet
with keys, in which the most recent dates are returned first.您必须将自定义比较器传递给 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).
创建比较器:
并将比较器与 TreeMap 结合使用,
Map 中的所有日期都将被排序。
Create comparator:
And use comparator with TreeMap
All dates in you Map will be sorted.
您可能想要使用
TreeMap
而不是HashMap
并使用提供排序的自定义Comparator
创建 Map。这是匿名比较器的草稿(不会将字符串解析为可比较的日期对象):
You may want to use a
TreeMap
instead of aHashMap
and create the Map with a customComparator
that provides the sorting.Here's a draft for an anonymous comparator (that does not parse the String into a comparable date object):