[Java] A story about IntelliJ IDEA teaching Map's putIfAbsent method

background

In the process of improving the existing legacy code, I tried to reduce the warnings from IntelliJ.

At that time, there was a process of "set the value when the key value is null for Map", but since I learned the point from IntelliJ, I wrote this article.

Specific example (before)

For example, suppose there is a map with the following prefecture name and prefectural office location.

Map<String, String> prefectures = new HashMap<>();
prefectures.put("Ibaraki Prefecture", "Mito");
prefectures.put("Tochigi Prefecture", "Utsunomiya");
prefectures.put("Gunma Prefecture", null);

Currently, the value of key Gunma prefecture is null, and we want to set the value here. At this time, in the existing implementation, the value was set by the following method.


if (prefectures.get("Gunma Prefecture") == null) {
    prefectures.put("Gunma Prefecture", "Maebashi");
}

IntelliJ suggested that you could write it simpler with the putIfAbsent method.

Specific example (After)

The putIfAbsent method is a method that sets the value when the key is null as above, and if you use this method, the if statement like the previous example disappears and


prefectures.putIfAbsent("Gunma Prefecture", "Maebashi");

You can write clearly in one line.

I didn't know the existence of this method, so it's easy, but I wrote this article this time.

Summary

When performing the process of "setting the value when the key value is null for Map", you can write it clearly by using the putIfAbsent method without using the if statement.

I thought again that IntelliJ, which even makes such a proposal, is amazing, but I also wondered how to know such a convenient method.

I would appreciate it if you could tell me if there is a better way than "Look at JavaDoc and take a look" and "Read the JDK release notes".

I hope this article helps someone. Until the end Thank you for reading.

References

JavaDoc for putIfAbsent method

Recommended Posts

[Java] A story about IntelliJ IDEA teaching Map's putIfAbsent method
A story about Java 11 support for Web services
A story about the JDK in the Java 11 era
A story about trying to operate JAVA File
About Java method binding
About method splitting (Java)
A story about developing ROS called rosjava with java
A note about Java GC
Create a java method [Memo] [java11]
A story about misunderstanding how to use java scanner (memo)
[Note] A story about changing Java build tools with VS Code
A story about hitting the League Of Legends API with JAVA
A story about having a hard time aligning a testing framework with Java 6
A really scary (Java anti-pattern) story
Java + OpenCV 3.X in IntelliJ IDEA
[IntelliJ IDEA] How to automatically add final when saving a Java file
Creating a project (and GitHub repository) using Java and Gradle in IntelliJ IDEA
A story about a Spring Boot project written in Java that supports Kotlin
A story about writing a binary search method at an in-house study session
[Beginner] A story about starting studying Java for job hunting ~ 2nd month ~
[Beginner] A story about starting studying Java for job hunting ~ 3rd month ~
[Beginner] A story about starting studying Java for job hunting ~ 1st month ~
[Beginner] A story about starting studying Java for job hunting ~ 5th month ~
[Beginner] A story about starting studying Java for job hunting ~ 4th month ~
About returning a reference in a Java Getter
[Creating] A memorandum about coding in Java
Optimize Java import declarations in IntelliJ IDEA
I made a plugin for IntelliJ IDEA
About Stream Debugger in IntelliJ IDEA plugin