Use Object#toString().

String string = map.toString();

That's after all also what System.out.println(object) does under the hoods. The format for maps is described in AbstractMap#toString().

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 ("{}"). Adjacent mappings are separated by the characters ", " (comma and space). Each key-value mapping is rendered as the key followed by an equals sign ("=") followed by the associated value. Keys and values are converted to strings as by String.valueOf(Object).

Answer from BalusC on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java collections › java map › map to string conversion in java
Map to String Conversion in Java | Baeldung
January 8, 2026 - public String convertWithApache(Map map) { return StringUtils.join(map); } One special mention goes to the debugPrint method available in Apache Commons. It is very useful for debugging purposes. ... Map as String = { 1 = one java.lang.String 2 = two java.lang.String 3 = three java.lang.String 4 = four java.lang.String } java.util.HashMap
Discussions

java - Using lambda to format Map into String - Stack Overflow
I have a map with Integer keys and values. I need to transform it into a String with this specific format: key1 - val1, key2 - val2, key3 - val3. Now, I'm using forEach to format each element, coll... More on stackoverflow.com
🌐 stackoverflow.com
java - How I can convert Map to String? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Ask questions, find answers and collaborate at work with Stack Overflow for Teams More on stackoverflow.com
🌐 stackoverflow.com
System.out.printf("%s", hashTagCounts); How Map is converted into String?
Viktor Kuryshev is having issues with: It is not clear for me, why we did not need a loop to get every value from Map to print it, how printf converts Map Array into string. I assume ... More on teamtreehouse.com
🌐 teamtreehouse.com
1
October 9, 2016
lambda - Java 8: How to convert String to Map<String,String>? - Stack Overflow
String utilMapString = utilMap .entrySet() .stream() .map(e -> e.toString()).collect(Collectors.joining(",")); Out put: 1=1,2=2,3=3,4=4,5=5 · How to convert utilMapString to Map in Java8? More on stackoverflow.com
🌐 stackoverflow.com
🌐
Techie Delight
techiedelight.com › home › java › convert a map to a string in java
Convert a Map to a String in Java | Techie Delight
July 7, 2026 - With Java 8 and above, you can get the string representation of a map using a Stream API, easily customized according to your needs. ... That’s all about converting a Map to a String in Java.
🌐
Baeldung
baeldung.com › home › java › java collections › java map › converting map to map in java
Converting Map to Map in Java | Baeldung
January 8, 2024 - We can build a checkAndTransform() method to perform a safe conversion: Map<String, String> checkAndTransform(Map<String, Object> inputMap) { Map<String, String> result = new HashMap<>(); for (Map.Entry<String, Object> entry : inputMap.entrySet()) ...
🌐
LinuxHaxor
linuxhaxor.net › code › how-convert-map-string-java.html
How to Convert a Map to a String in Java - A Complete Guide for Developers - LinuxHaxor
September 15, 2024 - Let‘s benchmark performance by ... Next let‘s see more customizable techniques. Introduced in Java 8, StringJoiner provides a flexible way to combine string representations with delimiters....
🌐
javathinking
javathinking.com › blog › convert-map-to-string-java
Convert Map to String in Java — javathinking.com
Performance: Using string concatenation (+) in a loop can be inefficient, especially for large Maps. It is better to use StringBuilder or other optimized methods. Handle Null Values: Check for null values before converting them to strings. You can use Objects.toString method to handle null values gracefully. Choose the Right Method: Depending on the Java version and the complexity of the requirements, choose the most appropriate method. For Java 8 and later, the Stream API can provide a concise and readable solution.
Find elsewhere
🌐
CodeSpeedy
codespeedy.com › home › how to convert a map to a string in java
How to convert a map to a string in Java - CodeSpeedy
March 30, 2022 - We can also convert a map to a string in java by using streams by including the package import java.util.stream.Collectors consisting of stream() method, collect() method, etc for the conversion. We will provide the values in the maps and the associated keys are generated.
🌐
DZone
dzone.com › data engineering › data › two ways to convert java map to string
Two Ways to Convert Java Map to String - DZone
May 8, 2010 - Approach 1: simple, lightweight – produces query string like output, but restrictive. Approach 2: uses Java XML bean serialization, more robust but produces overly verbose output. Approach 1 converts a map to a query-string like output.
🌐
Reddit
reddit.com › r/learnprogramming › java streams - how to convert map > to map ?
r/learnprogramming on Reddit: Java Streams - how to convert Map <String, Optional<Object>> to Map <String, Object>?
August 25, 2022 -

Hi, the task is to assign every most populated city of each continent to its continent.

"countries" is the map type <String, Country> where String is the country code, and value is just the Country object assigned to that code.

same with "cities", but it's map type <Integer, City> where Integer is ID of the assigned city

The implementation I've made almost provides the solution but it's a map of <String, Optional<City>> instead of <String, City> (where String is the continent, and City is the most populated city)

How to convert the map values of type <Optional<City>> to type <City>?

Map<String, Optional<City>> ex = cities.values().stream()
         .collect(Collectors.
          groupingBy(p -> countries.get(p.getCountryCode()).getContinent(),       
                 Collectors.maxBy(Comparator.comparing(City::getPopulation))));

Also the github with exercises and provided data: https://github.com/deepcloudlabs/stream-api-exercises-part1

City class: https://github.com/deepcloudlabs/stream-api-exercises-part1/blob/master/src/com/example/domain/City.java

Country class: https://github.com/deepcloudlabs/stream-api-exercises-part1/blob/master/src/com/example/domain/Country.java

Top answer
1 of 3
12

Well you can't safely cast it to a Map<String, String> because even though you know you've only got strings as the values, the compiler doesn't. That's like expecting:

Object x = "foo";
String y = x;

to work - it doesn't; you need to explicitly cast.

Likewise you can explicitly cast here, too, if you go via Object:

Map<String, Object> x = ...;
Map<String, String> y = (Map<String, String>) (Object) x;

Now you'll get a warning saying that it's an unchecked cast, because unlike the earlier "object to string" cast, there's no execution-time check that it's really valid. Type erasure means that a map doesn't really know its key/value types. So you end up with checking only being done when elements are fetched:

import java.util.*;

class Test {
    public static void main(String[] args) {
        Map<String, Object> x = new HashMap<>();
        x.put("foo", "bar");
        x.put("number", 0);
        Map<String, String> y = (Map<String, String>) (Object) x;
        // This is fine
        System.out.println(y.get("foo"));

        // This goes bang! It's trying to cast an Integer to a String
        System.out.println(y.get("number"));
    }
}

So if you really want to avoid creating a new map, this "cast via Object" will work - but it's far from ideal.

Your approach is safer, although you can make it slightly more efficient by avoiding the lookup:

public static Map<String, String> copyToStringValueMap(
        Map<String, Object> input) {
    Map<String, String> ret = new HashMap<>();
    for (Map.Entry<String, Object> entry : input.entrySet()) {
        ret.put(entry.getKey(), (String) entry.getValue());
    }
    return ret;
}
2 of 3
8

A Java 8 solution:

private Map<String, String> stringifyValues(Map<String, Object> variables) {
    return variables.entrySet().stream()
            .collect(Collectors.toMap(Map.Entry::getKey, e -> (String) e.getValue()));
}
🌐
Baeldung
baeldung.com › home › java › java collections › java map › convert hashmap.tostring() to hashmap in java
Convert HashMap.toString() to HashMap in Java | Baeldung
April 7, 2025 - In this tutorial, we’ll delve into the process of converting a HashMap‘s string representation, obtained through the toString() method, back to a HashMap object in Java. Before we dive into the conversion process, let’s gain a solid ...
🌐
Mkyong
mkyong.com › home › java › convert json string to map using jackson
Convert JSON string to Map using Jackson | mkyong.com
April 23, 2024 - package com.mkyong.json.jackson; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.HashMap; import java.util.Map; public class MapToJsonExample { public static void main(String[] args) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); Map<String, Object> map = new HashMap<>(); map.put("name", "mkyong"); map.put("age", 20); // convert map to JSON string String json = mapper.writeValueAsString(map); System.out.println(json); // compact-print // enable pretty-print json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map); System.out.println(json); // pretty-print } }