Java beginner: How is a key sorted in hashmaps?

I am new to java and have been studying hashmap concepts.

I am confused how keys are sorted in hashmaps. I realized that it is based on the length of the string. but I got confused how the data is sorted when the string length is the same.

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapExample
{

    public static void main(String args[])
    {
        Map<String,String> map = new HashMap<String,String>(20);//SPECIFYING THE TYPE FOR FINDING HASH CODES.


        //Adding values to the HashMap
        map.put("key value a", "test value 1");
        map.put("key value b", "test value 2");
        map.put("key value c", "test value 3");

        System.out.println("Retrieving values from HashMap");
        retrieveValuesFromListMethod(map);
        System.out.println("**********************");


    }

    /*This method retrieves values from Map
     */
    public static void retrieveValuesFromListMethod(Map map)
    {
        Set keys = map.keySet();
        Iterator itr = keys.iterator();

        String key;
        String value;

        while(itr.hasNext())
        {
            key = (String)itr.next();
            value = (String)map.get(key);
            System.out.println(key + " - "+ value); 
        }
    }
}

this is my code.

output

Retrieving values from HashMap
key value c- test value 3
key value b- test value 2
key value a- test value 1
**********************

but instead of a, b, c, if I give aa, ab, ac, the output is different

Retrieving values from HashMap
key value ab - test value 2
key value aa - test value 1
key value ac - test value 3
**********************

for 1,2,3

Retrieving values from HashMap
key value 1 - test value 1
key value 2 - test value 2
key value 3 - test value 3
**********************

how is sorting done in hashmap? Please, help!

Thanks in advance.

+3
source share
6 answers

java.util.HashMap is unordered; you cannot and should not accept anything other than this.

; , , .

java.util.LinkedHashMap .

HashMap , , . list , ( ).

java.util.TreeMap, SortedMap, , .

, , , .

+2
java beginner : How key gets sorted in hashmaps?

,

TreeMap:

Map<String, Float> map = new TreeMap<String, Float>(yourMap);

, . , .

, HashMap - .

, , LinkedHashMap.

+2

,

//Adding values to the HashMap
    map.put("key value a", "test value 1");
    map.put("key value b", "test value 2");
    map.put("key value c", "test value 3");

-

Data in Hashmap after insertion of key value

HashMap Array of Entry Map, hashcode, , , , . hashmap, , , , , . Iterator snap for iterating over the above hashmap (: Iterator , ) . TreeMap SortedMap , .

, .

+2
source

Hashmap is not sorted. If you need to save an order, you must use, for example,LinkedHashMap<K, V>

0
source

Try TreeMap,

Map<String, String> sampleMap = new TreeMap<String, String>(map);

use TreeMapall keys are in sorted order

0
source

hashmap is not sorted, the output is different from the fact that the String hash does not match.

0
source

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


All Articles