Thursday, October 13, 2022

Java Map getOrDefault() Method With Examples

The getOrDefault() method in java.util.Map is added in Java 8, this method gives you an option to return a default value if the specified key is not present in the Map. Value associated with the key is returned if the specified key is present in the Map.

Syntax of getOrDefault() method is as given below.

V getOrDefault(Object key, V defaultValue)

Parameters of the method are-

key- The key whose associated value is to be returned

defaultValue- The default value which has to be returned if the key is not present in the Map.

Method returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.

getOrDefault() method Java example

1. In this example we'll use getOrDefault() method to find value for the passed key, also pass a default value as a second argument which should be returned as value if the key is not found.

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

public class GetOrDefaultDemo {

  public static void main(String[] args) {
    // creating HashMap
      Map<String, String> langMap = new HashMap<String, String>();

      langMap.put("ENG", "English");
      langMap.put("NLD", "Dutch");
      langMap.put("BEN", "Bengali");
      langMap.put("ZHO", "Chinese");
      langMap.put("ZUL", "Zulu");
      
      String value = langMap.getOrDefault("HND", "Language not present");    
      System.out.println("Value for Key-HND: " + value);
      
      value = langMap.getOrDefault("ZUL", "Language not present");    
      System.out.println("Value for Key-ZUL: " + value);

  }
}

Output

Value for Key-HND: Language not present
Value for Key-ZUL: Zulu

Since key "HND" is not present in the HashMap so the default value is returned. Key "ZUL" is present in the HashMap so the value mapped to the key is returned.

2. getOrDefault() method also gives an option to write the program to Count Number of Times Each Character Appears in a String
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class CountCharacters {

  public static void main(String[] args) {
    CountCharacters cc = new CountCharacters();
    cc.countChars("I am a proud Indian");

  }
  // method used to count characters in a String
  public void countChars(String message){
    Map<Character, Integer> numCharMap = new HashMap<Character, Integer>();
    for(int i = 0; i < message.length(); i++){
      // Take one character 
      char c = message.charAt(i);
      // We don't need to count spaces
      if(c == ' ')
        continue;
      // If that character is already there in the map
      // then increase the value by 1, otherwise pass 0 //as default value
      numCharMap.put(c, numCharMap.getOrDefault(c, 0) + 1);
    }
    
    // Displaying the map values
    Set<Map.Entry<Character, Integer>> numSet = numCharMap.entrySet();
    for(Map.Entry<Character, Integer> m : numSet){
      System.out.println("Char- " + m.getKey() + " Count " + m.getValue());
    }
  }
}

Output

Char- p Count 1
Char- a Count 3
Char- r Count 1
Char- d Count 2
Char- u Count 1
Char- I Count 2
Char- i Count 1
Char- m Count 1
Char- n Count 2
Char- o Count 1

That's all for this topic Java Map getOrDefault() Method With Examples. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Java Map computeIfAbsent() With Examples
  2. Java Map merge() With Examples
  3. Java Map containsKey() - Check if Key Exists in Map
  4. How HashSet Works Internally in Java
  5. Java Collections Interview Questions And Answers

You may also like-

  1. How and Why to Synchronize ArrayList in Java
  2. Java Phaser With Examples
  3. static Keyword in Java With Examples
  4. Read or List All Files in a Folder in Java
  5. Binary Tree Traversal Using Depth First Search Java Program
  6. Check if String Present in Another String in Python
  7. Injector Hierarchy and Service Instances in Angular
  8. registerShutdownHook() Method in Spring Framework

No comments:

Post a Comment