Last Updated : 12 Jul, 2025
In Java, the toString() method of the AbstractMap.SimpleEntry class is used to return a string representation of the key-value pair in the format key=value. If the key or value is null, it is represented as the string null. This method is used to provide a straightforward way to display the contents of the SimpleEntry object.
Example 1: Here, the toString() method of AbstraceMap.SimpleEntry class is used to print key-value pairs stored in a List.
Java
// Java program to demonstrate
// AbstractMap.SimpleEntry.toString() method
import java.util.*;
import java.util.AbstractMap.SimpleEntry;
public class Geeks {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args)
{
// create a ArrayList of Map
ArrayList<
AbstractMap.SimpleEntry<Integer, Integer> >
al
= new ArrayList<AbstractMap.SimpleEntry<
Integer, Integer> >();
// add values
al.add(new AbstractMap.SimpleEntry(0, 100));
al.add(new AbstractMap.SimpleEntry(1, 200));
al.add(new AbstractMap.SimpleEntry(2, 300));
for (int i = 0; i < al.size(); i++) {
// get map from list
AbstractMap.SimpleEntry<Integer, Integer> m
= al.get(i);
// get representation of map using toString()
String value = m.toString();
System.out.println(value);
}
}
}
Explanation: This example stores key-value pair in an ArrayList using AbstractMap.simpleEntry and print them using toString() method, which display the pais in key=value format.
Syntax of toString() Methodpublic String toString()
Return Type: This method returns a string that represents the key-value pair in the SimpleEntry object.
Example 2: Here, the AbstractMap.SimpleEntry includes null as a key in its string representation when no key is provided.
Java
// Java program to demonstrates that AbstractMap.SimpleEntry
// allows null as a key and prints it as null=Value
import java.util.AbstractMap.SimpleEntry;
public class Geeks {
public static void main(String[] args)
{
// Create a SimpleEntry with a null key
SimpleEntry<String, String> e
= new SimpleEntry<>(null, "Value");
// Print the entry
System.out.println(e);
}
}
Explanation: In this example, we create a SimpleEntry object with a null key and a value "Value". The output is null=Value demonstrating that SimpleEntry includes null in its string representation.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4