Java map sort by value ascending

In Java 8 sorting a Map / HashMap ( Map is an interface while HashMap is a class implementation of this interface) can be done with method sorted() of Stream class and Comparator. Sorting by values can be done by method comparingByValue() of the Entry class. Newly added method since Java 8 makes sorting easier.

Sorting HashMap by value in Java 8 using streams, sorted() and LinkedHashMap:

Java map get insert order

If the insert order is important for you then you need to use LinkedHashMap. This will ensure that insert order is preserved and you can access the elements in the same way in which you inserted them. Here you can see another way of printing a map in Java: