Skip to content

j-util/optional-map

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MvnRepository

optional-map

Small Java 8+ utility for null-safe Optional access to Map values.

Replace:

Optional<String> name = Optional.ofNullable(map.get("user-1"));

with:

Optional<String> name = OptionalMaps.getOptional(map, "user-1");

Installation

Maven:

<dependency>
  <groupId>io.github.j-util</groupId>
  <artifactId>optional-map</artifactId>
  <version>1.0.0</version>
</dependency>

Maven Central

Usage

Use OptionalMaps.getOptional(map, key) when you only need one lookup:

Map<String, String> names = new HashMap<String, String>();
names.put("user-1", "Ada");

Optional<String> name = OptionalMaps.getOptional(names, "user-1");

Use OptionalMaps.wrap(map) when you want a Map view with an extra getOptional method:

Map<String, String> names = new HashMap<String, String>();
OptionalMap<String, String> optionalNames = OptionalMaps.wrap(names);

optionalNames.put("user-1", "Ada");
Optional<String> name = optionalNames.getOptional("user-1");

Null Values

getOptional returns Optional.empty() both when the key is absent and when the key is present with a null value. You still need to use containsKey to distinguish those cases.

Build

./mvnw test

About

Optional-based read access for Java Map

Resources

License

Security policy

Stars

2 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages