There are various ways to achieve this. Here are three.

    Map<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    System.out.println("using entrySet and toString");
    for (Entry<String, String> entry : map.entrySet()) {
        System.out.println(entry);
    }
    System.out.println();

    System.out.println("using entrySet and manual string creation");
    for (Entry<String, String> entry : map.entrySet()) {
        System.out.println(entry.getKey() + "=" + entry.getValue());
    }
    System.out.println();

    System.out.println("using keySet");
    for (String key : map.keySet()) {
        System.out.println(key + "=" + map.get(key));
    }
    System.out.println();

Output

using entrySet and toString
key1=value1
key2=value2
key3=value3

using entrySet and manual string creation
key1=value1
key2=value2
key3=value3

using keySet
key1=value1
key2=value2
key3=value3
Answer from Stefan Dollase on Stack Overflow
🌐
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()) ...
🌐
Vogella
vogella.com › tutorials › JavaMap › article.html
Using the map datastructures like HashMap and ConcurrentHashMap in Java
September 25, 2024 - This factory method supports a maximum of 10 key-value pairs and requires at least Java 9. Map<String, String> map = Map.of("key1", "value1", "key2", "value2");
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java
Converting String or String Array to Map in Java - Java Code Geeks
October 10, 2023 - We can also use the String.split() method to convert a string to a map in Java. This involves splitting the input string into tokens based on a delimiter, and then parsing these tokens into key-value pairs. Here is an example:
🌐
KapreSoft
kapresoft.com › java › 2023 › 05 › 03 › java-string-to-map.html
Java • Converting Strings To Map | KapreSoft
May 3, 2023 - In this example, we first split the input string into an array of key-value pairs using the semi-colon delimiter. Then, we use the map() method to split each pair into an array of key and value using the equals sign delimiter.
🌐
Baeldung
baeldung.com › home › java › java string › converting string or string array to map in java
Converting String or String Array to Map in Java | Baeldung
January 8, 2024 - In this article, we have explored different techniques for converting Strings and String arrays into Maps in Java. We covered basic conversions, handling duplicate keys, and provided JUnit test examples to ensure the correctness of the implementations.
Find elsewhere
🌐
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 - In this tutorial, we’ll focus on conversion from a Map to a String and the other way around. First, we’ll see how to achieve these using core Java methods, and afterward, we’ll use some third-party libraries. In all examples, we’re going to use the same Map implementation:
🌐
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 - The string representation of a ... single space and each key-value pair is separated by the equals sign (=). i.e., {K1=V1, K2=V2, ..., Kn=Vn}. Following is a simple example demonstrating the string representation of the ...
🌐
W3Schools
w3schools.com › java › java_hashmap.asp
Java HashMap
Keys and values in a HashMap are actually objects. In the examples above, we used objects of type "String". Remember that a String in Java is an object (not a primitive type). To use other types, such as int, you must specify an equivalent wrapper class: Integer.
🌐
GeeksforGeeks
geeksforgeeks.org › java › convert-string-or-string-array-to-hashmap-in-java
Convert String or String Array to HashMap In Java - GeeksforGeeks
July 23, 2025 - // Importing java generic libraries ... name String stuName[] = { "Aashish", "Bina", "Chintu" }; // Integer array that contains roll number of the // students Integer stuRollNo[] = { 1, 2, 3 }; // New HashMap obj Map<Integer, String> hashMap = new HashMap<Integer, String>(); // Iterating ...
🌐
Jenkov
jenkov.com › tutorials › java-collections › map.html
Java Map
By default you can put any Object ... can use for both keys and values in a Map. Here is an example: Map<String, MyObject> map = new HashMap<String, MyObject>();...
🌐
CalliCoder
callicoder.com › home › topics › java › java hashmap tutorial with examples
Java HashMap Tutorial with Examples | CalliCoder
April 21, 2018 - Written against Java 17. Map<String, Integer> ages = new HashMap<>(); Map<String, Integer> sized = new HashMap<>(64); // initial capacity Map<String, Integer> copy = new HashMap<>(existingMap); Map<String, Integer> fixed = Map.of("ann", 34, "bob", 41); // immutable, no nulls
🌐
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 - ... List<Integer> k_list = new ArrayList(mp.keySet()); List<String> v_List = new ArrayList(mp.values()); ... import java.util.*; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); String m; int n; ...
🌐
Medium
parathan.medium.com › split-map-a-string-in-java-bdb006ef45f9
Split & Map a String in Java
September 27, 2018 - I imported all java.util package modules to use “Map, Hash Map, put” keyword which are belong to the util package. Then in the App class I declared a map of datatype Map. For loop is used to loop until the string fully read by the program.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Split String To Map In Java - Java Code Geeks
April 3, 2024 - In this example, the input string name:John age:30 city:New York is split into key-value pairs using the : delimiter, and then stored in a HashMap. The output displays the resulting key-value map.