Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

1. Overview

In this tutorial, we'll discuss the new default method, computeIfAbsent, of the Map interface introduced in Java 8.

Specifically, we'll look at its signature, usage, and how it handles different cases.

2. Map.computeIfAbsent Method

Let's start by looking at the signature of computeIfAbsent:

default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)

The computeIfAbsent method takes two parameters. The first parameter is the key, and the second parameter is the mappingFunction. It's important to know that the mapping function is only called if the mapping isn't present.

First, it checks if the key is present in the map. If the key is present, and a non-null value is related to the key, then it returns that value:

Map<String, Integer> stringLength = new HashMap<>();
stringLength.put("John", 5);
assertEquals((long)stringLength.computeIfAbsent("John", s -> s.length()), 5);

As we can see, the key “John” has a non-null mapping present, and it returns the value 5. If our mapping function was used, we'd expect the function to return the length of 4.

2.2. Using the Mapping Function to Compute the Value

Furthermore, if the key isn't present in the map, or the null value is related to the key, then it attempts to compute the value using the given mappingFunction. It also enters the calculated value into the map unless the calculated value is null.

Let's take a look at the use of the mappingFunction in the computeIfAbsent method:

Map<String, Integer> stringLength = new HashMap<>();
assertEquals((long)stringLength.computeIfAbsent("John", s -> s.length()), 4);
assertEquals((long)stringLength.get("John"), 4);

Since the key “John” isn't present, it computes the value by passing the key as a parameter to the mappingFunction.

2.3. Mapping Function Returns null

Also, if the mappingFunction returns null, the map records no mapping:

Map<String, Integer> stringLength = new HashMap<>();
assertEquals(stringLength.computeIfAbsent("John", s -> null), null);
assertNull(stringLength.get("John"));

2.4. Mapping Function Throws an Exception

Finally, if the mappingFunction throws an unchecked exception, then the exception is re-thrown, and the map records no mapping:

@Test(expected = RuntimeException.class)
public void whenMappingFunctionThrowsException_thenExceptionIsRethrown() {
    Map<String, Integer> stringLength = new HashMap<>();
    stringLength.computeIfAbsent("John", s -> { throw new RuntimeException(); });
}

We can see that the mappingFunction throws a RuntimeException, which propagates back to the computeIfAbsent method.

3. Conclusion

In this brief article, we focused on the computeIfAbsent method, its signature, and its usage. Finally, we learned how it handles different cases.

As always, all of these code samples are available over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

Generic footer banner
Comments are closed on this article!