Saturday, October 25, 2025
HomeLanguagesJavaReplace null values with default value in Java Map

Replace null values with default value in Java Map

Given a Map with null values in it, the task is to replace all the null values with a default value.

Examples:

Input: map = {1=1, 2=2, 3=null, 4=4, 5=null, 6=null}, defaultValue = 0
Output: {1=1, 2=2, 3=0, 4=4, 5=0, 6=0}

Input: map = {1=A, 2=B, 3=null, 4=D, 5=null, 6=null}, defaultValue = ‘Z’
Output: {1=A, 2=B, 3=Z, 4=D, 5=Z, 6=Z}

Approach:

  1. Get the map with null values and the default value to be replaced with.
  2. Get the set view of the Map using Map.entrySet() method.
  3. Convert the obtained set view into stream using stream() method.
  4. Now map the null values to default value with the help of map() method.
  5. Collect the modified stream into Map using collect() method.
  6. The null values has been successfully replaced with default value.

Below is the implementation of the above approach:

Example 1: With integers.




// Java program to replace null values
// of a map with a default value
  
import java.util.*;
import java.util.stream.*;
  
class GFG {
  
    // Function to replace the null values
    public static <T, K> Map<K, T>
    replaceNullValues(Map<K, T> map, T defaultValue)
    {
  
        // Replace the null value
        map = map.entrySet()
                  .stream()
                  .map(entry -> {
                      if (entry.getValue() == null)
                          entry.setValue(defaultValue);
                      return entry;
                  })
                  .collect(Collectors.toMap(Map.Entry::getKey,
                                            Map.Entry::getValue));
  
        return map;
    }
  
    public static void main(String[] args)
    {
  
        // Get the map
        Map<Integer, Integer> map = new HashMap<>();
        map.put(1, 1);
        map.put(2, 2);
        map.put(3, null);
        map.put(4, 4);
        map.put(5, null);
        map.put(6, null);
  
        // Get the default value
        int defaultValue = 0;
  
        // Print the original map
        System.out.println("Map with null values: "
                           + map);
  
        // Replace the null values with the defaultValue
        map = replaceNullValues(map, defaultValue);
  
        // Print the modified map
        System.out.println("Map with null value replaced: "
                           + map);
    }
}


Output:

Map with null values: {1=1, 2=2, 3=null, 4=4, 5=null, 6=null}
Map with null value replaced: {1=1, 2=2, 3=0, 4=4, 5=0, 6=0}

Example 2: With characters.




// Java program to replace null values
// of a map with a default value
  
import java.util.*;
import java.util.stream.*;
  
class GFG {
  
    // Function to replace the null values
    public static <T, K> Map<K, T>
    replaceNullValues(Map<K, T> map, T defaultValue)
    {
  
        // Replace the null value
        map = map.entrySet()
                  .stream()
                  .map(entry -> {
                      if (entry.getValue() == null)
                          entry.setValue(defaultValue);
                      return entry;
                  })
                  .collect(Collectors.toMap(Map.Entry::getKey,
                                            Map.Entry::getValue));
  
        return map;
    }
  
    public static void main(String[] args)
    {
  
        // Get the map
        Map<Integer, Character> map = new HashMap<>();
        map.put(1, 'A');
        map.put(2, 'B');
        map.put(3, null);
        map.put(4, 'D');
        map.put(5, null);
        map.put(6, null);
  
        // Get the default value
        char defaultValue = 'Z';
  
        // Print the original map
        System.out.println("Map with null values: "
                           + map);
  
        // Replace the null values with the defaultValue
        map = replaceNullValues(map, defaultValue);
  
        // Print the modified map
        System.out.println("Map with null value replaced: "
                           + map);
    }
}


Output:

Map with null values: {1=A, 2=B, 3=null, 4=D, 5=null, 6=null}
Map with null value replaced: {1=A, 2=B, 3=Z, 4=D, 5=Z, 6=Z}
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS