Get Null-safe Map values in Java

Purpose of this article

It is a Null Pointer Exception that comes with Java, but I wanted to get the value as safely as possible, so this time I tried the method of getting the value of Map.

Map to be verified

Map<String, Boolean> map = new HashMap<String, Boolean>() {
  {
    put("true_label", true);
    put("false_label", false);
    put("null_label", null);
  }
};

Map # get normally

assertTrue(map.get("true_label"));
assertFalse(map.get("false_label"));

assertNull(map.get("null_label"));
assertNull(map.get("invalid_label")); //Keys that do not exist in Map

Java 8 Map # getOrDefault

You can get an alternative value if you don't have the key.

assertTrue(map.getOrDefault("true_label", false));
assertFalse(map.getOrDefault("false_label", false));
assertFalse(map.getOrDefault("invalid_label", false)); //You can get an alternative value if you don't have the key.

assertNull(map.getOrDefault("null_label", false)); //Unfortunately it doesn't become false!!

Java 8 Optional # ofNullable

assertTrue(Optional.ofNullable(map.get("true_label")).orElse(false));
assertFalse(Optional.ofNullable(map.get("false_label")).orElse(false));
assertFalse(Optional.ofNullable(map.get("null_label")).orElse(false)); //Alternate value false
assertFalse(Optional.ofNullable(map.get("invalid_label")).orElse(false)); //Alternate value false

ObjectUtils # defaultIfNull in Apache.commons.lang3

Optional may be fine, but just in case

assertTrue(ObjectUtils.defaultIfNull(map.get("true_label"), false));
assertFalse(ObjectUtils.defaultIfNull(map.get("false_label"), false));
assertFalse(ObjectUtils.defaultIfNull(map.get("null_label"), false)); //Alternate value false
assertFalse(ObjectUtils.defaultIfNull(map.get("invalid_label"), false)); //Alternate value false

Recommended Posts

Get Null-safe Map values in Java
Get EXIF information in Java
Null-safe program in Java (Eclipse)
[Java] Get KClass in Java [Kotlin]
Sort Map values in ascending key order in Java TreeMap
Use composite keys in Java Map.
JAVA (Map)
Get attributes and values from an XML file in Java
Get stuck in a Java primer
[Java] Get List / Map elements with Iterator
Duplicate Map sorted by key in Java
Map without using an array in java
Reverse Key from Value in Java Map
How to get the date in java
Get history from Zabbix server in Java
Sample code to get key JDBC type values in Java + H2 Database
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
[Java] Map comparison
Pi in Java
FizzBuzz in Java
How to get Class from Element in Java
Map GET requests to complex objects in Spring.
Get unixtime (seconds) from ZonedDateTime in Scala / Java
Library "OSHI" to get system information in Java
[Java] Get multiple values from one return value
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
Get along with Java containers in Cloud Run
Sample code to get the values of major SQL types in Java + MySQL 8.0
Get the URL of the HTTP redirect destination in Java
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
NVL-ish guy in Java
Combine arrays in Java
[Java] Get miscellaneous dates
"Hello World" in Java
Callable Interface in Java
Comments in Java source
Azure functions in java
Format XML in Java
Source used to get the redirect source URL in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
Get Locale objects for all locales available in Java
Use OpenCV in Java
Enum reverse map Java
webApi memorandum in java
[Java] Get the file in the jar regardless of the environment
Type determination in Java
Ping commands in Java
[Java] Initialize, add, get
Various threads in java
[Java] Get the file path in the folder with List
Heapsort implementation (in java)
Zabbix API in Java
Java bidirectional map library