Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java Util Map getOrDefault

farenda 2016-06-15 0

java.util.Map.getOrDefault(key, defaultValue)

The method Map.getOrDefault(key, defaultValue) returns mapped value for the given key or defaultValue if there is no such mapping.

Method signature

The signature of the java.util.Map.getOrDefault(key, defaultValue) method is as follows:

default V getOrDefault(Object key,
                       V defaultValue)

The method is available since Java 8.

Parameters

  • Object key: the key whose associated value is to be returned
  • V defaultValue: the default mapping of the key

Return value

Returns the value for given key or defaultValue if there is no mapping for the given key.

Exceptions

  • ClassCastException: if the key is of an inappropriate type for this map (optional)
  • NullPointerException: if the specified key is null and this map does not permit null keys (optional)

Example usage

In the following code we use java.util.Map.getOrDefault(key, defaultValue) to simplify getting values from a map:

package com.farenda.java.util;

import java.util.HashMap;
import java.util.Map;

public class MapExamples {

    public static void main(String[] args) {
        Map<Integer,String> vilians = new HashMap<>();
        vilians.put(1, "Kajko");
        System.out.println("Original map: " + vilians);

        // pre Java 8 version:
        String name;
        if (vilians.containsKey(2)) {
            name = vilians.get(2);
        } else {
            name = "Kokosz";
        }
        System.out.println("Name: " + name);

        // Java 8 version:
        name = vilians.getOrDefault(2, "Kokosz");
        System.out.println("Name: " + name);
    }
}

The above code produces the following output:

Original map: {1=Kajko}
Name: Kokosz
Name: Kokosz

References:

  • Java 8 Map API
Share with the World!
Categories Java Tags java, java-collections, java-util, java8
Previous: Java Util Map forEach(BiConsumer)
Next: Java Util Map merge(key, value, BiFunction)

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored