How to convert a String into a Map in Java

Hey there!

Welcome to ClearUrDoubt.com.

In this post, we will look at the program to convert a String into a Map in Java.

Consider if a String is having KeyValuePairs delimited by another character, we can split the string and load them into a Map instance.

Eg.

                 input = “1=ONE,2=TWO,3=THREE,4=FOUR”

we would like to load it into a Map instance as below

                {{1 -> “ONE”}, {2 -> “TWO”}, {3 -> “THREE”}, {4 -> “FOUR”}}

 

Let’s look at the program:

Output:

Please let me know in case of any queries.

Happy Learning :).

Leave a Reply

Your email address will not be published.