简体   繁体   English

我正在尝试获取集合中的最后一个date元素,但是它一直返回第一个?

[英]I am trying to get the last date element in a set, however It keeps returning the first one?

Below is my code, I get the keys of my Map and then I iterate through them in order to get the last date, However it does not appear to get the last date. 下面是我的代码,我获取了Map的键,然后遍历它们以获取最后一个日期,但是它似乎没有得到最后一个日期。 The values on this line Set<Date> keys = date.keySet(); 这行上的值Set<Date> keys = date.keySet(); are 10.31.18 and 11.17.18. 是10.31.18和11.17.18。 I would expect lastDate to equal 11.17.18 however it equals 10.31.18. 我希望lastDate等于11.17.18,但等于10.31.18。 Any ideas what I am doing wrong here. 任何想法我在这里做错了。

Map<Date, List<Integer>> date = date(dates, noPupils);
                Set<Date> keys = date.keySet();
                for (Iterator<Date> it = keys.iterator(); it.hasNext();) {
                    while (it.hasNext()) {
                        Date lastDate = it.next();

It seems you followed this answer . 看来您遵循了这个答案 It already says: 它已经说:

A Collection is not a necessarily ordered set Collection不一定是有序集合

In your case it doesn't work because 在您的情况下,它不起作用, 因为

The order of a map is defined as the order in which the iterators on the map's collection views return their elements. 映射的顺序定义为映射的集合视图上的迭代器返回其元素的顺序。 Some map implementations, like the TreeMap class, make specific guarantees as to their order; 一些地图实现(例如TreeMap类)对其顺序做出特定的保证。 others, like the HashMap class, do not. 其他的(例如HashMap类)则没有。

And apparently the Map date doesn't guarantee a specific order. 显然, Map date不能保证特定的订单。 This applies also to Set<Date> keys = date.keySet() as this 这也适用于Set<Date> keys = date.keySet() 因为这

Returns a Set view of the keys contained in this map ... is backed by the map ... 返回此映射中包含的键的Set视图...由映射支持...

But the solution is simple and comes out of the box: 但是解决方案很简单,可以立即使用:

Date lastDate = Collections.max(keys);

This works at once because Date implements Comparable . 由于Date实现Comparable因此可以立即工作。

Are you using LinkedHashMap or HashMap? 您正在使用LinkedHashMap还是HashMap?

HashMap is not ordered Map. HashMap没有排序的Map。 LinkedHashMap is ordered Map which reserves insert order. LinkedHashMap是有序地图,保留插入顺序。

But I think you should use TreeSet which can sort your dates. 但是我认为您应该使用TreeSet来对日期进行排序。 As a result you don't need to care about what kind of Map :) 因此,您无需关心哪种Map :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试将日期作为路径变量传递给邮递员,但是我收到错误 - Trying to pass a date as a path variable on postman however i get error 我试图让这个文件在预定时间之后运行,但是它只运行了一次 - I am trying to get this file to run after scheduled time, however it only ran once 添加第一个元素或删除最后一个元素时触发事件的设置 - Set that fires event when first element is added, or last one is removed 在 java 中,我尝试使用 &amp; 与过滤器一起获取 integer 的每个字节,它适用于最后 3 个字节,但第一个会引发数字格式异常 - in java i am trying to use & with a filter to get each byte of an integer it works for the last 3 bytes but the first throws a number format exception java中使用递归的数组中元素的最后一个索引。 但是我怀疑这段代码不应该返回第一个索引吗? - last index of an element in an array in java using recursion. However i have doubts shouldnt this code return the first index? 我正在尝试更新mySQL数据库,但是当我尝试更新多个列时,发生错误 - I am trying to update my mySQL database, however when I try update more than one column an error occurs 我正在尝试捕获NumberFormatException,但是异常仍在传递 - I am trying to catch a NumberFormatException however the exception is still passing 我试图解析这个字符串并得到最后一个词 - I am trying to parse this string and get the last word 我正在尝试将字符串中的模式“我们的”替换为“或”但是这不起作用 - I am trying to replace the pattern "our" in the string to become "or" however this is not working Apache poi Java。 我正在尝试添加 upper_letter 编号,但结果是所有部分都编号为 A - Apache poi Java. I am trying to add upper_letter numbering, however the result is that all sections get numbering A
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM