-4

Simple as the title, I searched it up already, and I didn't find anything about this issue, I'm pretty sure I just misunderstood how a HashMap works with its own elements. Super-simplest code:

HashMap<String, Integer> map = new HashMap<String, Integer>();
    map.put("first key", 1);
    map.put("second key", 2);
    map.put("third key", 3);

    System.out.println(map.toString());

What does the println() method show? It shows this:

{third key=3, first key=1, second key=2}

I tough the HashMap stored elements as the programmer put them in. Even a sorting would sort would place these elements as I put them in origin. I tried changing the words and similar things happen(only the positions changed, but they were "wrong" anyway). :/ Do you know why?

Thanks in advance :/

EDIT: Rohit Jain is actually the first that answered me as comment, and you guys told me about LinkedHashMap, so you helped me to solve, thank you very much :)

Francesco
  • 49
  • 5
  • 3
    what does the doc of `HashMap` tell you? – Rohit Jain Feb 23 '15 at 19:00
  • 1
    "I tough[sic] the HashMap stored elements as the programmer put them in" What made you think that? –  Feb 23 '15 at 19:01
  • As I said, I was pretty sure I misunderstood how it worked, I was right .o. Thank you a lot. It means, that if I would like to sort it, it would be useless? :/ – Francesco Feb 23 '15 at 19:03

6 Answers6

2

If you want to iterate over your keys by insertion order you need to use a LinkedHashMap instead. The HashMap documentation clearly states that

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
1

You are looking for LinkedHashMap which retains insertion order.

xehpuk
  • 7,814
  • 3
  • 30
  • 54
1

HashMap doesn't preserve order:

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time. --javadoc

You want LinkedHashMap:

    LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
    map.put("first key", 1);
    map.put("second key", 2);
    map.put("third key", 3);

    System.out.println(map.toString());

output:

{first key=1, second key=2, third key=3}

If all you care about is the toString, you could also use TreeMap.

mk.
  • 11,360
  • 6
  • 40
  • 54
1

HashMap does not guarantee ordering of any kind.

From the java docs:

This class makes no guarantees as to the order of the map

You can use LinkedHashMap if you want to maintain order.

amit
  • 175,853
  • 27
  • 231
  • 333
1

http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

adrCoder
  • 3,145
  • 4
  • 31
  • 56
0

There are different Map types, giving different guarantees about their key order (see this).

HashMap: Undefined ordering of its elements.

LinkedHashMap: Order defined by insertion.

TreeMap: Order defined by the compareTo() method of their elements.

So, HashMap makes no guarantee about it's iteration order. Once you insert another element, you will get a different ordering. From what you described a LinkedHashMap might be what you need.

Community
  • 1
  • 1
runDOSrun
  • 10,359
  • 7
  • 47
  • 57
  • Even if now I'm having a little problem o.o I'm trying to use TreeMap, just to see how it works, and when I declare a variable of it, it highlights the variable name in blue(I'm using eclipse), it should highlights it in something like brown, and it is not a real variable o.o cause I can't use it, when I pass the cursor throught it, I can see the javadoc, but well I wrote a javadoc before to write it, I know I shouldn't do it for these kind of purposes, but it was just something that only I had to read, I tried putting the same code into another class, and it works :( – Francesco Feb 23 '15 at 22:36
  • 1
    Nvm sorry, it was making me crazy, I just declared it out of a method lol -.- – Francesco Feb 24 '15 at 01:44