What is the difference between these two object initializations in java?

If I use:

    HashMap<String, Integer> test = new HashMap<String, Integer>();

Or I use:

    HashMap test = new HashMap();

Is there any difference in further methods that I can apply to the test object. e.g. test.put (), test.get (), etc., if they are initialized differently.

Also, if I put something in a test object, for example:

    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:

Three: 3 One: 5 Two: 1

What rule does he follow? Is this normal output behavior randomly displayed?

+3
source share
5 answers

Hashmap , . . .

, HashMap . , LinkedHashMap.

+7

, - , .

, Java Generics.

+2

, HashMap "" . , LinkedHashMap.

+1

, .

anytype.

HashMap HashSet . , , , LinkedHashMap. fooobar.com/questions/100130/...

0

, , , String as Key Integer , , , - - , .

0

Source: https://habr.com/ru/post/1777341/


All Articles