Home » Java programming language

Java LinkedHashMap getOrDefault() Method with Example

LinkedHashMap Class getOrDefault() method: Here, we are going to learn about the getOrDefault() method of LinkedHashMap Class with its syntax and example.
Submitted by Preeti Jain, on March 09, 2020

LinkedHashMap Class getOrDefault() method

  • getOrDefault() method is available in java.util package.
  • getOrDefault() method is used to get the value associated with the given key element when it exists otherwise it gets the default value for the given key element when no previous value associated with the given key.
  • getOrDefault() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getOrDefault() method does not throw an exception at the time of getting the value element.


    public getOrDefault(Object key_ele, Value def_val);


  • Object key_ele – represents the key element (key_ele) to which the associated value is to be retrieved.
  • Value def_val – represents the default value (def_val) is to be retrieved when no previous value exist for the given key element.

Return value:

The return type of the method is Value, it returns the linked value for the given key element if exists otherwise it returns the default value (def_val).


// Java program to demonstrate the example 
// of getOrDefault(Object key_ele, Value def_val)
// method of LinkedHashMap 

import java.util.*;

public class GetOrDefaultOfLinkedHashMap {
    public static void main(String[] args) {
        // Instantiates a LinkedHashMap object
        Map < Integer, String > map = new LinkedHashMap < Integer, String > ();

        // By using put() method is to add
        // key-value pairs in a LinkedHashMap
        map.put(10, "C");
        map.put(20, "C++");
        map.put(50, "JAVA");
        map.put(40, "PHP");
        map.put(30, "SFDC");

        // Display LinkedHashMap
        System.out.println("LinkedHashMap: " + map);

        // By using getOrDefault() method is to
        // return the value associated for the
        // given key element if exists otherwise
        // it returns the default value

        Object val_ele = map.getOrDefault(50, "Microservices");

        //Display val_ele
        System.out.print("map.getOrDefault(50,Microservices): ");


LinkedHashMap: {10=C, 20=C++, 50=JAVA, 40=PHP, 30=SFDC}
map.getOrDefault(50,Microservices): JAVA


Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.