Skip to Content
Java Coding Problems
book

Java Coding Problems

by Anghel Leonard
September 2019
Intermediate to advanced
816 pages
18h 47m
English
Packt Publishing
Content preview from Java Coding Problems

Example 3 (compute())

Let's suppose that we have the following Map:

Map<String, String> map = new HashMap<>();map.put("postgresql", "127.0.0.1");map.put("mysql", "192.168.0.50");

We use this map to build JDBC URLs for different database types.

Let's assume that we want to build the JDBC URLs for MySQL and Derby DB. In this case, irrespective of whether the key (mysql or derby) is present in the map, the JDBC URL should be computed based on the corresponding key and value (which can be null). In addition, if the key is present in the map and the result of our computation is null (the JDBC URL cannot be computed), then we want to remove this entry from the map. Basically, this is a combination of computeIfPresent() and computeIfAbsent().

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Coding Problems - Second Edition

Java Coding Problems - Second Edition

Anghel Leonard
Java in a Nutshell, 7th Edition

Java in a Nutshell, 7th Edition

Benjamin J. Evans, David Flanagan
The Well-Grounded Java Developer, Second Edition

The Well-Grounded Java Developer, Second Edition

Benjamin Evans, Martijn Verburg, Jason Clark

Publisher Resources

ISBN: 9781789801415Supplemental Content