CKTIS / Java
AbstractMap.toString — a map's string representation
AbstractMap.toString returns text containing key–value mappings. Their order comes from the entrySet() iterator, not from automatic sorting.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
public String toString()
// Usage:
String text = map.toString();AbstractMap.toString is a no-argument instance method returning String; it overrides Object.toString(). It represents a map as {key=value, key=value}. An empty map produces {}; successive mappings are separated by , .
The entrySet() iterator determines the order: the method does not sort entries. Keys and values are normally converted as by String.valueOf(Object): null becomes the text null, and other objects use their own toString(). A map can, however, contain itself as a value. In that case, the inherited AbstractMap.toString implementation in OpenJDK 21 renders that value as (this Map) instead of recursively calling toString() on the same map. The Java SE specification does not guarantee this marker. Do not treat this representation as JSON or a reliable data-interchange format.
The example uses LinkedHashMap, whose default order follows insertion order. The variable type AbstractMap does not change the object's actual class; a subclass can override toString(). This description targets Java SE 21; the production runtime has not been attested.
Examples
import java.util.AbstractMap;
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) {
AbstractMap<String, Integer> map = new LinkedHashMap<>();
map.put("beta", 2);
map.put("alpha", 1);
map.put("missing", null);
System.out.println(map.toString());
map.clear();
System.out.println(map.toString());
}
}Result
{beta=2, alpha=1, missing=null}
{}
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.