If i use:
HashMap<String, Integer> test = new HashMap<String, Integer>();
Or i use:
HashMap test = new HashMap();
Is there any difference on further methods that i can apply on test object. like test.put(), test.get() etc if initialized differently??
Also if i put something in test object e.g like:
test.put("One", new Integer(5));
test.put("Two", new Integer(4));
test.put("Three", new Integer(3));
and display it as:
Set set = tokens.entrySet();
Iterator ik = test.iterator();
while(ik.hasNext()){
Map.Entry me = (Map.Entry)ik.next();
System.out.println(me.getKey() + " : " + me.getValue() );
The result is not sorted, restul is:
Three: 3 One: 5 Two: 1
What rule it does follow?? Is this normal behavior for the output to be randomly displayed??
In the first case Hashmap keys must be Strings and values must be Integers. The compiler will perform the respective type checking. In the second case any kind of objects can be used.
This is completely normal that your HashMap entries are printed in random order. If you want to preserve the order use LinkedHashMap instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With