In this short article, we will discuss how to check if the Map is empty or null in Java. Let's create a standard utility method to check if the Map is empty or null in Java.
Learn Java collections framework in-depth at Java Collections Framework in Depth
Check if Map is Empty or Null in Java - Utility Methods
- isNullOrEmptyMap(Map, ?> map) - Return true if the supplied Map is null or empty. Otherwise, return false.
- isNotNullOrEmptyMap(Map, ?> map) - Return true if the supplied Map is not null or not empty. Otherwise, return false.
1. isNullOrEmptyMap() Utility Method
Return true if the supplied Map is null or empty. Otherwise, return false.
public static boolean isNullOrEmptyMap(Map <? , ?> map) {
return (map == null || map.isEmpty());
}
2. isNotNullOrEmptyMap() Utility Method
Return true if the supplied Map is not null or not empty. Otherwise, return false.
public static boolean isNotNullOrEmptyMap(Map <? , ?> map) {
return !isNullOrEmptyMap(map); // this method defined below
}
public static boolean isNullOrEmptyMap(Map <? , ?> map) {
return (map == null || map.isEmpty());
}
Complete Example
Here is a complete example to test the above utility methods:
package net.javaguides.lang;
import java.util.HashMap;
import java.util.Map;
public class MapNullOrEmptyExample {
public static void main(String[] args) {
// return true
System.out.println(isNullOrEmptyMap(null));
System.out.println(isNullOrEmptyMap(new HashMap < > ()));
Map < String, String > map = new HashMap < > ();
map.put("key1", "value1");
map.put("key2", "value2");
// return true
System.out.println(isNotNullOrEmptyMap(map));
// return false
System.out.println(isNullOrEmptyMap(map));
}
public static boolean isNullOrEmptyMap(Map << ? , ? > map) {
return (map == null || map.isEmpty());
}
public static boolean isNotNullOrEmptyMap(Map << ? , ? > map) {
return !isNullOrEmptyMap(map);
}
}
Output:
true
true
true
false
Related Java Utility Methods or Classes
- 18 Useful Collections Utility Methods
- Java Custom CollectionUtils Class
- 27 Useful String Utility Methods
- Executors Utility Class in Java
- java.util.Arrays Class
- Java String Utility Class
- java.util.Collections Class
- Top Reflection Utility Methods
- Java Reflection Utility Class
- Java File Utility Class
- Java Zip Utility Class
- Java 8 Date Utility Class
- Top File Utility Methods
Collections Examples
- Java LinkedHashMap Example
- Java HashSet Example
- Java LinkedList Example
- Java ArrayList Example
- Java Comparator Interface Example
- Java Comparable Interface Example
- Java IdentityHashMap Example
- Java WeakHashMap Example
- Java EnumMap Example
- Java CopyOnWriteArraySet Example
- Java EnumSet Class Example
- Guide to Java 8 forEach Method
- Different Ways to Iterate over a List in Java [Snippet]
- Different Ways to Iterate over a Set in Java [Snippet]
- Different Ways to Iterate over a Map in Java [Snippet]
- Iterate over TreeSet in Java Example
- Iterate over LinkedHashSet in Java Example
- Remove First and Last Elements of LinkedList in Java
- Iterate over LinkedList using an Iterator in Java
- Search an Element in an ArrayList in Java
- Iterate over ArrayList using Iterator in Java
- Remove Element from HashSet in Java
- Iterating over a HashSet using Iterator
- How To Remove Duplicate Elements From ArrayList In Java?
- Different Ways to Iterate over List, Set, and Map in Java
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course
Comments
Post a Comment