Why does toString function of a HashMap prints itself with a different order?
I have this very simple piece of code, and I was just trying to play a bit
with different kind of objects inside a Map.
//There's a bit of spanish, sorry about that
//just think 'persona1' as an object with
//a string and an int
Map mapa = new HashMap();
mapa.put('c', 12850);
mapa.put(38.6, 386540);
mapa.put("Andrés", 238761);
mapa.put(14, "Valor de 14");
mapa.put("p1", persona1);
mapa.put("Andrea", 34500);
System.out.println(mapa.toString());
And then I expect from console something like:
{c=12850, 38.6=386540, Andrés=238761, 14=Valor de 14, p1={nombre: Andres
Perea, edad: 10}, Andrea=34500}
But susprisingly for me I got same data in different order:
{38.6=386540, Andrés=238761, c=12850, p1={nombre: Andres Perea, edad: 10},
Andrea=34500, 14=Valor de 14}
It doesn't matter if I try other kind of objects, even just Strings or
numeric types, it always does the same, it makes a different
without-apparently-any-sense order.
Can someone give me a hint why this happens? Or may be something too
obvious I'm missing?
I'm in Java 1.7 and Eclipse Juno.
No comments:
Post a Comment