
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Replace null values with default value in Java Map
To replace null values with default value in Java Map, the code is as follows −
Example
import java.util.*; import java.util.stream.*; public class Demo{ public static <T, K> Map<K, T> null_vals(Map<K, T> my_map, T def_val){ my_map = my_map.entrySet().stream().map(entry -> { if (entry.getValue() == null) entry.setValue(def_val); return entry; }) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); return my_map; } public static void main(String[] args){ Map<Integer, Integer> my_map = new HashMap<>(); my_map.put(1, null); my_map.put(2, 56); my_map.put(3, null); my_map.put(4, 99); int defaultValue = 0; System.out.println("The map with null values is : "+ my_map); my_map = null_vals(my_map, defaultValue); System.out.println("The map with null values replaced is : " + my_map); } }
Output
The map with null values is : {1=null, 2=56, 3=null, 4=99} The map with null values replaced is : {1=0, 2=56, 3=0, 4=99}
A class named Demo contains a function named ‘null_vals’ that checks for null values in an array and replaces them with a default value that is previously defined. In the main function, a Map instance is created and elements are pushed into it using the ‘put’ function. The ‘null_vals’ function is called on this map and the null values are replaced with a default value.
- Related Articles
- Replace 0 with null in MySQL?
- Insert default into not null column if value is null in MySQL?
- Comparing Strings with (possible) null values in java?
- How to set default value to NULL in MySQL?
- Fetch maximum value from multiple columns with null and non-null values?
- Replace a value if null or undefined in JavaScript?
- Java Program to replace key and value in HashMap with identical key and different values
- Default array values in Java
- How to Replace null with “-” JavaScript
- MySQL: How can I find a value with special character and replace with NULL?
- MySQL query to replace null value with empty string in several columns while fetching data
- Python - Replace negative values with latest preceding positive value in Pandas DataFrame
- How to check whether column value is NULL or having DEFAULT value in MySQL?
- MySQL query to replace only the NULL values from the table?
- Adding a column whose value is not null by default in MySQL?

Advertisements