How to convert a map to a string in Java

In this tutorial, we will learn how to convert a map to a string in Java. In Java, map is a collection where every map element contains key pairs. It means every key has one value associated with it. Duplicates can’t be there as every key has its unique pair. In Java, list is also a collection for storing the references of every element sequentially as every element can be uniquely identified because of its index.

There are many ways in which we can convert a map to a string. The two ways are:

  1. Using two lists
  2. Using stream

Ways to Convert a map to a string in Java

We have explained each of the methods one by one with example.

Using two lists

We can convert a map to a string in java using two array lists. In this, we first fill the map with the keys. Then, we will use keySet() method for returning the keys in the map, and values() method for returning the value present in the map to the ArrayList constructor parameter.

Like this way:

List<Integer> k_list = new ArrayList(mp.keySet()); 
List<String> v_List = new ArrayList(mp.values());

Code:

import java.util.*;

public class Main{

    public static void main(String[] args) {
         
         Scanner in = new Scanner(System.in);
          String m;
          int n;
          System.out.println("enter the number of inputs");
          n=in.nextInt();
          System.out.println("enter the strings");
          m=in.nextLine();
          Map<Integer, String> mp = new HashMap<>();
          for(int i=1;i<=n;i++)
          {
             m=in.nextLine();
             mp.put(i,m);
          }

          List<Integer> k_List = new ArrayList(mp.keySet());
          List<String> v_List = new ArrayList(mp.values());

          System.out.println("Key List: " + k_List);
          System.out.println("Value List: " + v_List);

    }
}

Output:

Enter the number of inputs
5
Enter the strings
red
blue
green
black
orange
Key List: [1, 2, 3, 4, 5]
Value List: [red, blue, green, black, orange]

2) Using stream:

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. Here, keySet() return the keys and values() return values from the map to the List using stream() method. The collect() method collects the streams of the returned value and converts them into the list passing Collectors.toList() in the parameter.

Like this:

List<Integer> k_List = mp.keySet().stream().collect(Collectors.toList()); 
List<String> v_List = mp.values().stream().collect(Collectors.toList());

Code:

import java.util.*;
import java.util.stream.Collectors;

public class Main{

    public static void main(String[] args) {
         
         Scanner in = new Scanner(System.in);
          String m;
          int n;
          System.out.println("Enter the number of inputs");
          n=in.nextInt();
          System.out.println("Enter the strings");
          m=in.nextLine();
          Map<Integer, String> map = new HashMap<>();
          for(int i=1;i<=n;i++)
          {
               m=in.nextLine();
             mp.put(i,m);
          }

        List<Integer> k_List = mp.keySet().stream().collect(Collectors.toList());
        List<String> v_List = mp.values().stream().collect(Collectors.toList());

        System.out.println("Key List: " + k_List);
        System.out.println("Value List: " + v_List);

    }
}

Output:

Enter the number of inputs
5
Enter the strings
violet
indigo
blue
green
yellow
Key List: [1, 2, 3, 4, 5]
Value List: [violet, indigo, blue, green, yellow]

Hope this tutorial was useful.

Leave a Reply

Your email address will not be published.