How to initialize a hashmap with values in Java

We will use the Collections.singletonMap() method to initialize it. Since it is an immutable map, we cannot add more than one entry to it. This will result in the UnsupportedOperationException. The HashMaps are key-value-based unordered, unsorted collections. We often use HashMaps in Java and put hard-coded values in it. This way of initializing the map…