August 14, 2022

How to get the environment variables in Java

In Java, the System.getenv() returns an unmodifiable string Map view of the current system environment. If a requested environment variable is not defined, the System.getenv will return a null, and with Java 8 uses Optional to handle the null. The System.getenv() returns an unmodifiable string Map, modify the Map will cause UnsupportedOperationException.

How to get the environment variables in Java Read More

How to sort a Map in Java

Few Java examples to sort a Map by its keys or values: (1) Uses java.util.TreeMap, it will sort the Map by keys automatically, (2) Yet another java.util.TreeMap example, provide a custom Comparator to sort the key in descending order, (3) Sort by Value – Converts the Map into a List, sorts the List with a custom Comparator and put it into a new insertion order map – LinkedHashMap

How to sort a Map in Java Read More