0

I have a HashMap to store my data of <Integer, ArrayList<String>>. I used put(...) to add data in the map if it does not exist yet. I used containsKey() to check if the entry is Key is already in the map. If it exists, I add the String to the key by doing this:

x.get(i).add(str)

To get the data, I just did a for loop with the keySet() of the hash.

If I have this hash:

int -> array of strings
1 -> "a", "b", "c"
2 -> "aa", "bb", "cc"
3 -> "aaa", "bbb", "ccc"

My problem is, the order of the data being read is not consistent. In 1 PC, it might read the keys in this order: 2, 1, 3. In another PC, it might read it in this order: 1, 2, 3.

I would prefer that the order in which they are read are the same across all PCs. The order into which they are entered into the hashmap is also the same, but Why is the hashmap reading the Keysets in different order?

0

3 Answers 3

3

Did you read the Javadoc of HashMap? It makes no guarantees about ordering.

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.

If you want consistent ordering, use LinkedHashMap or TreeMap. HashMap's lack of guarantees mean that, if the Java developers choose, it can iterate backwards on Tuesdays.

Sign up to request clarification or add additional context in comments.

3 Comments

To extend the answer, the LinkedHashSet contains a LinkedList and a HashSet in order to remember the ordering this of course comes with an increased overhead (two additional pointers per entry). By default the concept of a HashSet does not allow holding any order, see Wikipedia for an explanation on how it works.
Er, not exactly. There's no LinkedList object, the entries of the map are just modified to also form a linked list.
Yes, that is what I meant. Maybe a bit missleading with the highlighting, sorry for that. It is just that the Entry<K, V> also have a previous and a next pointer, forming a linked list, as you said.
0

Actually there is no way to control the input order of the elements in Hash Map, but it is possible to sort them before porcess them.

public class HashMapSample {
    public static void main(String[] args) {
        Map<Integer,List<String>> myHash = new HashMap<>();
        myHash.put(1, Arrays.asList("a","aa","aaa"));
        myHash.put(3, Arrays.asList("c","ccc","ccc"));
        myHash.put(2, Arrays.asList("b","bb","bbb"));
        System.out.println(myHash);
        myHash.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).forEach(System.out::println);

    }
}

2 Comments

Also not a bad idea but it probably would be better to just use a set implementation that maintains order as sorting adds a huge performance overhead.
I'm very agree with you. It will depends on the problem context and what kind of requirement has to solve. Regards.
0

You need to use TreeMap instead of HashMap

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.