0

For a problem I'm designing in Java, I'm given a list of dates and winning lottery numbers. 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.

Thing is, when I run the .values() function, I'm noticing the numbers are no longer ordered (by insertion). 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. Might as well be using a 2-D ArrayList[][] in that case. Thanks in advance!

EDIT: Adding code for further examination!

Lottery Class: http://pastebin.com/9ezF5U7e Text file: http://pastebin.com/iD8jm7f8

To test, call checkOldLTNums(). Just pass it any int[] array, it doesn't utilize it, at least relevant to this problem. The output is different from the first lines in the .txt, which is organized. Thanks!

EDIT2:

I think I figured out why it fails. I used a smaller .txt file, and the code worked perfectly. It might be that it isn't wise to load 1900 entries of stuff into memory. 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.

Luiserebii
  • 55
  • 2
  • 11

3 Answers3

3

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.

Perhaps you'd be better off using a SortedMap that keeps the items sorted by key. To summarise: LinkedHashMap is ordered and TreeMap is sorted.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • I apologize, I should have been more careful with how I worded things. My problem is that regardless, they don't appear in the same order they were inserted. .values() is yielding a random sort of my map instead of by insertion order. – Luiserebii Jan 13 '16 at 22:56
  • @Luiserebii try using `entrySet()` or `keySet()` to iterate over the `Map`. – Óscar López Jan 13 '16 at 23:00
  • Hmm, just checked keySet, and the keys are out of insertion order as well. Any ideas as to why this might be happening? Should I upload my code somewhere? – Luiserebii Jan 13 '16 at 23:24
  • The only possible explanation is that you're not actually using a LinkedHashMap, because the behavior you're describing doesn't correspond with the class contract. Check the part where you're instantiating the LinkedHashMap, make sure that you're not copying data to a different Map, etc. – Óscar López Jan 14 '16 at 00:20
  • Hmm, here's my instantiation: Map winningNumbers = new LinkedHashMap(); – Luiserebii Jan 14 '16 at 00:22
  • @Luiserebii, you should provide your whole code then, because something has to be going wrong in the middle somewhere. – Louis Wasserman Jan 14 '16 at 00:39
  • Looks good, dates in `winningNumbers` _have to appear_ in the same order they were inserted. Better write a standalone example showing the problem, because I can't think of any other way to help you. – Óscar López Jan 14 '16 at 00:40
  • Ok! Here's my code: Lottery Class: http://pastebin.com/9ezF5U7e Text file: http://pastebin.com/iD8jm7f8 To test, call checkOldLTNums(). Just pass it any int[] array, it doesn't utilize it, at least relevant to this problem. – Luiserebii Jan 14 '16 at 02:03
  • You're only adding `tempArrList.get(0)`, what about the rest of the elements in `tempArrList`? Also, the way you're iterating over the `values()` is incorrect, use an iterator, there's no need to convert the values to an array. – Óscar López Jan 14 '16 at 02:14
  • tempAttList.get(0) is only referenced to extract the date. For the rest of the elements, please see the following for loop, which puts them into finalInt[]. As for the iteration over values, I am unfamiliar with an iterator? – Luiserebii Jan 14 '16 at 02:34
  • Sorry, I can't find the problem. Good luck. – Óscar López Jan 14 '16 at 02:44
  • Wait, what were you talking about with using an iterator? Perhaps that is the key to my problem, how do you iterate properly? Link would be most helpful :) – Luiserebii Jan 14 '16 at 02:47
  • I don't think so, the code to fill the Map seems more likely to be causing the problem, but it's too messy to understand. Start from the beginning, write a little example, see that it works and then start refactoring your code until it works like the little example – Óscar López Jan 14 '16 at 02:49
  • I think I figured out why it fails. I used a smaller .txt file, and the code worked perfectly. It might be that it isn't wise to load 1900 entries of stuff into memory. 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. – Luiserebii Jan 14 '16 at 04:06
  • After further examination, I've realized that some of the keys for Date is replacing others for some reason. I'll try to figure that issue out now. – Luiserebii Jan 14 '16 at 07:48
0

Try using a TreeMap instead. You should get the values in ascending key order that way.

John Sensebe
  • 1,386
  • 8
  • 11
0

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". That's what I get for copy-pasting, haha. Thanks to all who helped!

Luiserebii
  • 55
  • 2
  • 11