Friday, September 5, 2025
HomeLanguagesJavaAbstractMap.SimpleEntry toString() Method in Java with Examples

AbstractMap.SimpleEntry toString() Method in Java with Examples

AbstractMap.SimpleEntry<K, V> is used to maintain a key and a value entry. The value can be changed using the setValue method. This class facilitates the process of building custom map implementations.

toString() method of AbstractMap.SimpleEntry<K, V> returns a String representation of this SimpleEntry map entry. This method returns the string representation of this map entry’s key followed by the equals character (“=”) followed by the string representation of this entry’s value.

Syntax:

public String toString()

Parameters: This method accepts nothing.

Return value: This method returns a String representation of this map entry.

Below programs illustrate toString() method:
Program 1:




// Java program to demonstrate
// AbstractMap.SimpleEntry.toString() method
  
import java.util.*;
import java.util.AbstractMap.SimpleEntry;
  
public class GFG {
  
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args)
    {
  
        // create a ArrayList of Map
        ArrayList<AbstractMap
                      .SimpleEntry<Integer, Integer> >
            arrayList
            = new ArrayList<AbstractMap
                                .SimpleEntry<Integer, Integer> >();
  
        // add values
        arrayList.add(new AbstractMap.SimpleEntry(0, 123));
        arrayList.add(new AbstractMap.SimpleEntry(1, 130));
        arrayList.add(new AbstractMap.SimpleEntry(2, 994));
  
        // print keys
        for (int i = 0; i < arrayList.size(); i++) {
  
            // get map from list
            AbstractMap.SimpleEntry<Integer, Integer>
                map
                = arrayList.get(i);
  
            // get representation of map using toString()
            String value = map.toString();
  
            System.out.println(value);
        }
    }
}


Output:

0=123
1=130
2=994

Program 2:




// Java program to demonstrate
// AbstractMap.SimpleEntry.toString() method
  
import java.util.*;
  
public class GFG {
  
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args)
    {
  
        // create a ArrayList of Map
        ArrayList<AbstractMap
                      .SimpleEntry<String, String> >
            arrayList
            = new ArrayList<AbstractMap
                                .SimpleEntry<String, String> >();
  
        // add values
        arrayList.add(new AbstractMap
                          .SimpleEntry(" 001AB ", " Emp 1"));
        arrayList.add(new AbstractMap
                          .SimpleEntry(" 011AC ", " Emp 2"));
        arrayList.add(new AbstractMap
                          .SimpleEntry(" 111AD ", " Emp 3"));
        arrayList.add(new AbstractMap
                          .SimpleEntry(" 101BE ", " Emp 4"));
        arrayList.add(new AbstractMap
                          .SimpleEntry(" 110CE ", " Emp 5"));
  
        // print keys
        for (int i = 0; i < arrayList.size(); i++) {
  
            // get map from list
            AbstractMap.SimpleEntry<String, String>
                map
                = arrayList.get(i);
  
            // get representation of map using toString()
            String value = map.toString();
  
            System.out.println(value);
        }
    }
}


Output:

001AB = Emp 1
011AC = Emp 2
111AD = Emp 3
101BE = Emp 4
110CE = Emp 5

References: https://docs.oracle.com/javase/10/docs/api/java/util/AbstractMap.SimpleEntry.html#toString()

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11868 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7028 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS