• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Contact Us

All about code

  • Java
    • Spring
      • Spring Boot
      • Spring Cloud
    • Java Miscellaneous Tips
    • Java Design Patterns
  • Data Structures and Algorithms
    • Algorithms
    • Data Structures
  • Thoughts and Stories
  • AI

Java Map: How to Preserve Insertion Order

laptop infographic online business 6087062

Java provides a set of classes that implement the Map interface. The map interface defines how to map keys with values. This is the basic functionality, but there are several different ways to obtain this behavior. In fact, many different classes implement the Map interface. One of the nuances in the key/value essential feature regards how the map returns its entries:

  • Are the returned keys ordered?
  • Are they ordered in the same way they were inserted or are they sorted using a natural or specified order?

In this article, we will quickly describe the choices available for the above scenarios, and we will then focus on describing the available implementation for preserving the insertion order.

You can find the code fragments used in this post in the following GitHub project: java miscellaneous tips.

Java Map: Preserve Insertion Order with LinkedHashMap 

Regarding the order in which we can extract the keys of a Map object we have these three choices:

  • For non-ordered keys, the HashMap class is provided by Java. The HashMap class is basically implemented as a hash table data structure and does not preserve order.
  • For keys sorted in a natural order, or by using a specific Comparator object, an implementation is provided by the TreeMap class. The TreeMap class is implemented internally as a red-black tree data structure to optimize the sorting process.
  • Finally, if we want to preserve the order of the keys as they were inserted, the natural choice is the LinkedHashMap class.

The LinkedHashMap class internally uses a doubly-linked list and the keys can be extracted in the same order they were inserted. As with all the implementations of the Map interface, it does not allow duplicates. It can store a single null key but at the same time multiple null values. The following snippet shows an example of its basic usage:

   LinkedHashMap<Integer, String> preserveOrderMap = new LinkedHashMap<Integer, String>();  
   preserveOrderMap.put(1,"First");  
   preserveOrderMap.put(2,"Second");  
   preserveOrderMap.put(3,"Third");
   preserveOrderMap.put(4,"Fourth");

   for(Map.Entry mapEntry: preserveOrderMap.entrySet()){  
        System.out.println(mapEntry.getKey() + " - " + mapEntry.getValue());  
   }  

In the example above the loop on the LinkedHashMap entries prints the keys and related values in the same order in which they were put into the map.

Primary Sidebar

Social

  • Facebook
  • LinkedIn
  • Twitter

Archives

  • May 2026
  • April 2026
  • March 2026
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • September 2023
  • May 2023
  • March 2023
  • January 2023
  • November 2022
  • September 2022
  • August 2022

Recent Posts

  • Better Avoid Spring Boot Lazy-initialization @PostConstruct Trap
  • How To Handle Large Datasets in Spring Boot
  • How to Log HTTP Incoming Requests in Spring Boot
  • How to Reliably Implement Post-Commit Actions in Spring
  • Brief and Easy Explanation of HATEOAS

TAGS

Algorithms Apache POI Backtracking Date Structures Dynamic Programming engineering Graphs Greedy Horror Stories HSSF Java Java New Features Java Principles Java tips real life Sorting Spring Boot Spring Cloud Strings Trees

Footer

Privacy Policy Cookie Policy Terms and Conditions