Java Map with Insertion Order

In Java, we can use LinkedHashMap to keep the insertion order.

P.S HashMap does not guarantee insertion order.

1. HashMap

Generate a HashMap, UUID as key, index 0, 1, 2, 3, 4… as value.

JavaHashMap.java

package com.mkyong.samples;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.stream.IntStream;

public class JavaHashMap {

    public static void main(String[] args) {

        Map<String, Integer> collect =
                IntStream.range(0, 10).collect(
                        HashMap<String, Integer>::new,
                        // m.size() = 0, 1, 2, 3, 4...
                        (m, v) -> m.put(UUID.randomUUID().toString(), m.size()),
                        (m, m2) -> {
                        }
                );

        collect.forEach((k, v) -> System.out.println(k + ":" + v));

    }


}

Output – Each time, the HashMap will generate a different order.

Run #1


b693f2f9-59e7-4f4b-bdfe-825707935c7f:3
69e509c0-fb67-476b-8433-53b42fb06743:9
77cac4ac-72d4-4393-9e46-947c7036e11f:0
7baee74e-549a-44f2-95fc-64a4dfb3a034:1
be43ac08-b6ff-416f-a38f-7f738ff7a7ae:2
2dad919e-217f-4ca3-a6ba-4916e0af1b38:7
a753be73-49ab-46a3-83bb-f24c98013f39:6
5f5549fa-903b-4d6c-8082-bb46df017ac0:5
4b18ee81-cb22-4114-b846-1d906ddf2681:4
a8eb415a-aab6-4ac6-bffd-8ad7bf247d42:8

Run #2


2e75035c-85c6-498f-a835-3fc1f02641c7:2
6f836297-bfab-46e5-808c-3d446730aaaf:0
f97f3c42-ef63-4b4b-a303-7b77fe1a0f16:1
c757b96d-8b5b-4e82-a9b8-a824fd6df097:3
542e7f3a-766b-4e50-82fc-2b952a98d03b:8
beb4fee5-5299-4683-b3ca-657dd8923da7:4
99bf70a2-2379-4bae-99a3-5c02a93d81a0:6
cea42ee2-49f4-4b65-9caa-53619bd066c2:5
24266158-3c8b-4188-849d-02fd2b40f4c6:7
a23a6f2f-1e1b-41b9-8418-3e70464303f8:9

Note Don’t test the HashMap with simple 1, 2, 3 as key, the internal hash(key) will display items in ascending order always, and make you think this HashMap keeps the insertion order, but it is NOT.

2. LinkedHashMap

JavaLinkedHashMap.java

package com.mkyong.samples;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import java.util.stream.IntStream;

public class JavaLinkedHashMap {

    public static void main(String[] args) {

        Map<String, Integer> collect =
                IntStream.range(0, 10).collect(
                        LinkedHashMap<String, Integer>::new,
                        (m, v) -> m.put(UUID.randomUUID().toString(), m.size()),
                        (m, m2) -> {
                        }
                );

        collect.forEach((k, v) -> System.out.println(k + ":" + v));

    }


}

Output – The above program will always keep the insertion order.

Run #1


151b60fa-9868-4893-acbc-c83892322da6:0
35a2e359-0186-4655-9c41-cb5d8a3c5047:1
3abce08a-a952-4d19-a4a3-22978844d63d:2
d1219dba-ed9b-47ff-bc6e-8568d8154f59:3
a7bb8713-1c8b-46cb-a65c-c0e192322dde:4
41851789-edb5-4de6-81ba-df0c316267bc:5
2e638207-07a1-4d54-9af1-3a8a4a7aad5c:6
45dc685a-7020-4354-94aa-a607d963a714:7
67169a03-18ac-403d-adcc-8dba822f2b8a:8
5db3efd9-4195-47fe-ab0b-660dd2eb87f7:9

Run #2


701f2306-b006-46e0-a345-1c4fa54c31cb:0
cfe0462e-4b31-43a2-aed9-4efdc41caa63:1
4df0117f-7cf7-4c62-8f35-901c76db53bb:2
41d9d633-689f-441c-a70b-ca0505c5a185:3
67096249-905e-4dc2-9b13-c97798fd43a1:4
f0f899da-760f-4459-8ef6-7041afd37c01:5
23020899-5ef6-41ad-95fc-335eab92db7b:6
12c023a2-79e3-4d62-a216-7d2a619aafd1:7
3e6cf3e2-43ae-46af-8752-e1b5642cc159:8
858a5ce4-b760-4695-bf4c-4fe406871c4b:9

References

author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar