简体   繁体   English

是否没有通过LinkedHashMap值进行迭代?

[英]Iterating through LinkedHashMap Values Isn't Ordered?

For a problem I'm designing in Java, I'm given a list of dates and winning lottery numbers. 对于我用Java设计的问题,我给出了日期和中奖彩票的列表。 I'm supposed to do things with them, and spit them back out in order. 我应该和他们一起做事,然后将它们按顺序吐回来。 I decided to choose a LinkedHashMap to solve it, the Date containing the date, and int[] containing the array of winning numbers. 我决定选择一个LinkedHashMap来解决它,Date包含日期,而int []包含中奖号码数组。

Thing is, when I run the .values() function, I'm noticing the numbers are no longer ordered (by insertion). 问题是,当我运行.values()函数时,我注意到数字不再是按顺序排列(通过插入)。 The code I'm running is: 我正在运行的代码是:

    for(int i = 0; i < 30; i++){ //testing first 30 to see if ordered
        System.out.println(Arrays.toString((int [])(winningNumbers.values().toArray()[i])));
    }   

Can anyone see what exactly I'm doing wrong? 谁能看到我到底在做什么错? Tempted almost to just use .get() and iterate through the dates, since the dates do go in some order, but that might make using a LinkedHashMap moot. 由于日期确实按一定顺序排列,因此几乎倾向于使用.get()并遍历日期,但这可能会使用LinkedHashMap模拟。 Might as well be using a 2-D ArrayList[][] in that case. 在这种情况下,也可以使用二维ArrayList [] []。 Thanks in advance! 提前致谢!

EDIT: Adding code for further examination! 编辑:添加代码以供进一步检查!

Lottery Class: http://pastebin.com/9ezF5U7e Text file: http://pastebin.com/iD8jm7f8 彩票类: http : //pastebin.com/9ezF5U7e文本文件: http : //pastebin.com/iD8jm7f8

To test, call checkOldLTNums(). 要进行测试,请调用checkOldLTNums()。 Just pass it any int[] array, it doesn't utilize it, at least relevant to this problem. 只要将它传递给任何int []数组,它就不会使用它,至少与这个问题有关。 The output is different from the first lines in the .txt, which is organized. 输出与组织的.txt中的第一行不同。 Thanks! 谢谢!

EDIT2: 编辑2:

I think I figured out why it fails. 我想我知道了为什么失败。 I used a smaller .txt file, and the code worked perfectly. 我使用了一个较小的.txt文件,并且代码运行正常。 It might be that it isn't wise to load 1900 entries of stuff into memory. 将1900个条目装入内存可能不明智。 I suppose it's better to just load individual lines and compare them instead of grabbing everything at once. 我想最好是加载单个行并进行比较,而不是一次抓取所有内容。 Is this logic sound? 这听起来合理吗? Any advice going from here would be useful. 从这里得到的任何建议都会很有用。

The values() method will return the Map 's values in the same order they were inserted , that's what ordered means, don't confuse it with sorted , that means a different thing - that the items follow the ordering defined by the value's Comparator or its natural ordering if no comparator was provided. values()方法将按插入时的顺序返回Map的值,这就是有序的意思,不要将它与sorted混淆,这意味着另一件事-这些项遵循值的Comparator所定义的顺序或其自然顺序(如果未提供比较器)。

Perhaps you'd be better off using a SortedMap that keeps the items sorted by key. 也许最好使用SortedMap来按键对项目进行排序。 To summarise: LinkedHashMap is ordered and TreeMap is sorted . 总结一下: LinkedHashMap排序TreeMap排序

Try using a TreeMap instead. 尝试改用TreeMap You should get the values in ascending key order that way. 您应该以这种方式以升序键顺序获取值。

I can't believe I missed this. 我简直不敢错过。 I did some more troubleshooting, and realized some of the Date keys were replacing others. 我进行了更多的故障排除,并意识到某些日期键正在替换其他键。 After doing some testing, I finally realized that the formatting I was using was off: "dd/MM/yyyy" instead of "MM/dd/yyyy". 经过一些测试之后,我终于意识到我使用的格式已关闭:“ dd / MM / yyyy”而不是“ MM / dd / yyyy”。 That's what I get for copy-pasting, haha. 这就是我粘贴粘贴所获得的,哈哈。 Thanks to all who helped! 感谢所有的帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM