TreeMap toString() in Java

toString(): This method is available in java.util.TreeMap class of Java.

Syntax:

String java.util.AbstractMap.toString()

This method returns a string representation of this map. The string representation consists of a list of key-value mappings in the order returned by the map's entrySet view's iterator, enclosed in braces( "{}"}).

Parameters: NA

Returns: a string representation of this map.

Exceptions: NA

Approach

Java

import java.util.TreeMap;

public class TreeMaptoString {
    public static void main(String[] args) {

        TreeMap<Integer, String> treeMap =
new TreeMap<Integer, String>();

        treeMap.put(2, "Hello");
        treeMap.put(11, "Java");
        treeMap.put(23, "Program");
        treeMap.put(6, "C++");
        treeMap.put(25, "Program");

        System.out.println(treeMap.toString());

    }
}

Output:

{2=Hello, 6=C++, 11=Java, 23=Program, 25=Program}


No comments:

Post a Comment