Return a default value from a Map in Java 8

Consider the following Employee class:

public class Employee
    private UUID id;
    private String name;
    private int age;

    public Employee(UUID id, String name, int age)
    { = id; = name;
        this.age = age;
    public UUID getId()
        return id;

    public void setId(UUID id)
    { = id;

    public String getName()
        return name;

    public void setName(String name)
    { = name;
    public int getAge()
        return age;

    public void setAge(int age)
        this.age = age;

Let’s put some Employee objects into a hash map:

Map<Integer, Employee> employeeMap = new HashMap<>();
employeeMap.put(1, new Employee(UUID.randomUUID(), "Elvis", 50));
employeeMap.put(2, new Employee(UUID.randomUUID(), "Marylin", 18));
employeeMap.put(3, new Employee(UUID.randomUUID(), "Freddie", 25));
employeeMap.put(4, null);
employeeMap.put(5, new Employee(UUID.randomUUID(), "Mario", 43));
employeeMap.put(6, new Employee(UUID.randomUUID(), "John", 35));
employeeMap.put(7, new Employee(UUID.randomUUID(), "Julia", 55));

Note the null value for key 4. Let’s also define a default Employee object:

Employee defaultEmployee = new Employee(UUID.fromString("00000000-0000-0000-0000-000000000000"), "", -1);

Java 8 includes a new method called “getOrDefault” on the Map interface. It accepts a key, like the “get” method on the Map, but it also accepts a default object that will be returned if the key does not exist.

Can you guess what the below code will return?

Employee employee = employeeMap.getOrDefault(4, defaultEmployee);

“employee” will be null of course, as key 4 exists and its value is null. However, if you simply call the “get” method with 4 as the key input then you don’t know exactly how to interpret the null result: does 4 exist as key in the map and its value is null or does the key 4 not exist at all in the map? With getOrDefault returning 0 in this case you can be 100% sure that a null response is unambiguous.

Let’s see what the below bit of code returns:

Employee employee = employeeMap.getOrDefault(12, defaultEmployee);

This time it returns the default employee as the key 12 does not exist in the map.

View all posts related to Java here.


About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


A great site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: