[JAVA] Ideal and reality that I felt when I used Optional for the first time ~ Implementation of cache using Map ~

Overview

Keep the value in Map If there is already a value in Map, it is the story when I implemented it to use that.

that time, Map # put () does not return the put value as it is, I couldn't write it as I expected, that is, it's a complaint: cry: (Specifically, refer to the source code: pencil :)

※perhaps, There may be a smart solution and Maybe it's just a misunderstanding: thinking:

Source code

Main.java


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        //Ideal (I want to write like this)
        String idealKey = "idealKey";
        Map<String, String> idealMap = new HashMap<String, String>();
        String idealValue = Optional.ofNullable(
	        idealMap.get(idealKey)
        ).orElseGet(
            //put returns null ...
            () -> idealMap.put(idealKey, "idealValue")
        );
        System.out.println("get:" + idealValue);

        //Reality (I ended up writing this)
        String realKey = "realKey";
        Map<String, String> realMap = new HashMap<String, String>();
        String realStr = Optional.ofNullable(
	        realMap.get(realKey)
        ).orElseGet(() -> {
            String value = "realValue";
	        realMap.put(realKey, value);
        	return value;
        });
        System.out.println("get:" + realStr);
    }
}

Execution result

get:null
get:realValue

Recommended Posts

Ideal and reality that I felt when I used Optional for the first time ~ Implementation of cache using Map ~
I tried using Docker for the first time
[Rails] I tried using the button_to method for the first time
The story of intentionally using try catch for the first time in my life
Impressions and doubts about using java for the first time in Android Studio
I tried touching Docker for the first time
[CircleCI] I will explain the stupid configuration file (config.yml) that I wrote for the first time.
Learn for the first time java # 3 expressions and operators
Learning memo when learning Java for the first time (personal learning memo)
A summary of what Java programmers find when reading Kotlin source for the first time
Know the convenience of Docker (-compose) now (information list that I referred to when using it)
[Introduction] Installation of Docker Desktop for Mac for the first time and setup of virtual environment construction of CentOS
I will explain the nesting of for statements that kill beginners
Think when Rails (turbolinks) doesn't load the page for the first time
The story of Collectors.groupingBy that I want to keep for posterity
I tried using the cache function of Application Container Cloud Service
How to get the contents of Map using for statement Memorandum
Summary: Implementation of a function that switches the logical value when a link is clicked [for own output]
Lock_version may be used for tables that tend to access and edit the same record at the same time
[Ruby on Rails] When logging in for the first time ・ How to split the screen in half using jQuery