Skip to content

io microsphere collection MapUtils

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

MapUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.collection | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/collection/MapUtils.java

Overview

The utilities class for Java Map

Declaration

public abstract class MapUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Method Examples

isMap

System.out.println(MapUtils.isMap(new HashMap<>())); // true
System.out.println(MapUtils.isMap(new ArrayList<>())); // false
System.out.println(MapUtils.isMap(null)); // false

isMap

System.out.println(MapUtils.isMap(HashMap.class)); // true
System.out.println(MapUtils.isMap(List.class));    // false
System.out.println(MapUtils.isMap(null));         // false

isEmpty

Map<String, Integer> map1 = new HashMap<>();
System.out.println(isEmpty(map1)); // true

map1.put("one", 1);
System.out.println(isEmpty(map1)); // false

Map<String, Integer> map2 = null;
System.out.println(isEmpty(map2)); // true

isNotEmpty

Map<String, Integer> map1 = new HashMap<>();
System.out.println(isNotEmpty(map1)); // false

map1.put("one", 1);
System.out.println(isNotEmpty(map1)); // true

Map<String, Integer> map2 = null;
System.out.println(isNotEmpty(map2)); // false

size

Map<String, Integer> map1 = new HashMap<>();
map1.put("one", 1);
map1.put("two", 2);
System.out.println(size(map1)); // Output: 2

Map<String, Integer> map2 = null;
System.out.println(size(map2)); // Output: 0

Map<String, Integer> map3 = new HashMap<>();
System.out.println(size(map3)); // Output: 0

of

Map<String, Integer> map = MapUtils.of("one", 1);
System.out.println(map.get("one")); // Output: 1
Map<String, Integer> map = MapUtils.of("one", 1, "two", 2);
System.out.println(map.get("one")); // Output: 1
System.out.println(map.get("two")); // Output: 2
Map<String, Integer> map = MapUtils.of("one", 1, "two", 2, "three", 3);
System.out.println(map.get("one"));   // Output: 1
System.out.println(map.get("two"));   // Output: 2
System.out.println(map.get("three")); // Output: 3
Map<String, Integer> map = MapUtils.of("one", 1, "two", 2, "three", 3, "four", 4);
System.out.println(map.get("one"));   // Output: 1
System.out.println(map.get("two"));   // Output: 2
System.out.println(map.get("three")); // Output: 3
System.out.println(map.get("four"));  // Output: 4
Map<String, Integer> map = MapUtils.of("one", 1, "two", 2, "three", 3, "four", 4, "five", 5);
System.out.println(map.get("one"));   // Output: 1
System.out.println(map.get("two"));   // Output: 2
System.out.println(map.get("three")); // Output: 3
System.out.println(map.get("four"));  // Output: 4
System.out.println(map.get("five"));  // Output: 5
Map<String, Integer> map = MapUtils.of("one", 1, "two", 2);
System.out.println(map.get("one")); // Output: 1
System.out.println(map.get("two")); // Output: 2

Map<String, String> emptyMap = MapUtils.of(); // returns an empty map

ofMap

Map<String, Integer> entry1 = new AbstractMap.SimpleEntry<>("one", 1);
Map<String, Integer> entry2 = new AbstractMap.SimpleEntry<>("two", 2);
Map<String, Integer> map = MapUtils.of(entry1, entry2);

System.out.println(map.get("one")); // Output: 1
System.out.println(map.get("two")); // Output: 2
Map<String, Integer> map = MapUtils.ofMap("one", 1);
System.out.println(map.get("one")); // Output: 1
Map<String, Integer> map = MapUtils.ofMap("one", 1, "two", 2);
System.out.println(map.get("one")); // Output: 1
System.out.println(map.get("two")); // Output: 2

Map<String, String> emptyMap = MapUtils.ofMap(); // returns an empty map

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.collection.MapUtils;

API Reference

Public Methods

Method Description
isMap The min load factor for HashMap or Hashtable
isMap Checks if the specified class type is an implementation of Map.
isEmpty Checks if the specified map is either null or empty.
isNotEmpty Checks if the specified map is not empty and not null.
size Returns the size of the specified map, or 0 if the map is null.
of Creates an immutable map containing a single key-value pair.
ofMap Creates an immutable map from the provided array of Map.Entry objects.

Method Details

isMap

public static boolean isMap(Object instance)

The min load factor for HashMap or Hashtable / public static final float MIN_LOAD_FACTOR = MIN_VALUE;

/** The fixed load factor for HashMap or Hashtable = 1.00 / public static final float FIXED_LOAD_FACTOR = 1.00f;

/** Checks if the specified object is an instance of Map.

This method provides a convenient way to determine whether a given object is a map. It returns true if the object is an instance of Map, otherwise false.

Example Usage

`System.out.println(MapUtils.isMap(new HashMap<>())); // true
System.out.println(MapUtils.isMap(new ArrayList<>())); // false
System.out.println(MapUtils.isMap(null)); // false
`

isMap

public static boolean isMap(Class<?> type)

Checks if the specified class type is an implementation of Map.

This method provides a convenient way to determine whether a given class implements the Map interface. It returns true if the class is assignable from Map, indicating that it is a map type.

Example Usage

`System.out.println(MapUtils.isMap(HashMap.class)); // true
System.out.println(MapUtils.isMap(List.class));    // false
System.out.println(MapUtils.isMap(null));         // false
`

isEmpty

public static boolean isEmpty(Map<?, ?> map)

Checks if the specified map is either null or empty.

A map is considered empty if it contains no key-value mappings. This method provides a null-safe way to check for emptiness.

Example Usage

`Map map1 = new HashMap<>();
System.out.println(isEmpty(map1)); // true

map1.put("one", 1);
System.out.println(isEmpty(map1)); // false

Map map2 = null;
System.out.println(isEmpty(map2)); // true
`

isNotEmpty

public static boolean isNotEmpty(Map<?, ?> map)

Checks if the specified map is not empty and not null.

This method provides a null-safe way to determine if a map contains one or more key-value mappings.

Example Usage

`Map map1 = new HashMap<>();
System.out.println(isNotEmpty(map1)); // false

map1.put("one", 1);
System.out.println(isNotEmpty(map1)); // true

Map map2 = null;
System.out.println(isNotEmpty(map2)); // false
`

size

public static int size(Map<?, ?> map)

Returns the size of the specified map, or 0 if the map is null.

This method provides a null-safe way to obtain the size of a map. It avoids NullPointerException by returning zero when the input map is null.

Example Usage

`Map map1 = new HashMap<>();
map1.put("one", 1);
map1.put("two", 2);
System.out.println(size(map1)); // Output: 2

Map map2 = null;
System.out.println(size(map2)); // Output: 0

Map map3 = new HashMap<>();
System.out.println(size(map3)); // Output: 0
`

of

public static Map of(Object... values)

Creates an immutable map containing a single key-value pair.

This method provides a convenient way to create a small, read-only map with one entry. The resulting map is thread-safe and cannot be modified after creation.

Example Usage

`Map map = MapUtils.of("one", 1);
System.out.println(map.get("one")); // Output: 1
`

ofMap

public static Map ofMap(Object... keyValuePairs)

Creates an immutable map from the provided array of Map.Entry objects.

This method offers a convenient way to construct a small, read-only map using pre-defined entries. The resulting map is thread-safe and cannot be modified after creation.

Example Usage

`Map entry1 = new AbstractMap.SimpleEntry<>("one", 1);
Map entry2 = new AbstractMap.SimpleEntry<>("two", 2);
Map map = MapUtils.of(entry1, entry2);

System.out.println(map.get("one")); // Output: 1
System.out.println(map.get("two")); // Output: 2
`

See Also

  • Map

This documentation was auto-generated from the source code of microsphere-java.

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally