Open In App

Converting Integer-String HashMap to String-Array-Integer HashMap in Java

Last Updated : 29 Dec, 2023
Suggest changes
Share
Like Article
Like
Report

Efficient Conversion of a HashMap<Integer, String> into another HashMap<String, Integer[]> involves organizing integer keys based on their corresponding string values. Here's a concise and efficient Java method to accomplish this transformation, focusing on clear and straightforward implementation.

Step-by-Step Implementation of Conversion

The method follows a step-by-step procedure using HashMaps and Lists.

  • First, it gathers integer keys together by looking at their matching string values in a temporary HashMap.
  • Then, it turns these grouped keys into arrays, creating a fresh HashMap where strings are linked to arrays of integers.
Java
// Java program for converting a  // HashMap<Integer, String> to HashMap<String, Integer[]>. import java.util.*; // Driver class public class HashMapConversion {  // Main Method  public static void main(String[] args){    // Create an input HashMap with integer keys and  // string values  HashMap<Integer, String> inputMap = new HashMap<>();  inputMap.put(1, "a");  inputMap.put(2, "b");  inputMap.put(3, "a");  inputMap.put(4, "k");  inputMap.put(5, "fg");  inputMap.put(7, "aa");  inputMap.put(9, "b");  inputMap.put(10, "vc");  // Create an output HashMap to store strings as keys  // and lists of associated integer keys  HashMap<String, List<Integer>> outputMap= new HashMap<>();  // Iterate through each entry in the input map  for (Map.Entry<Integer, String> entry:inputMap.entrySet()) {    // Get the integer key  Integer key= entry.getKey();    // Get the string value  String value= entry.getValue();  // Check if the output map already contains the  // string value as a key  if (outputMap.containsKey(value)) {    // If it exists, add the current integer key  // to its associated list  outputMap.get(value).add(key);  }  else {    // If it doesn't exist, create a new list  // for the string value and add the current  // integer key  List<Integer> list = new ArrayList<>();  list.add(key);  outputMap.put(value, list);  }  }  // Create a final HashMap to store strings as keys  // and arrays of associated integer keys  HashMap<String, Integer[]> finalMap= new HashMap<>();  // Iterate through each entry in the output map  // (which has string keys and list values)  for (Map.Entry<String, List<Integer> > entry:outputMap.entrySet()) {    // Get the string key  String key = entry.getKey();    // Get the list of integer values  List<Integer> values= entry.getValue();  // Convert the list of integer values to an  // array and store it in the final map  finalMap.put(key,  values.toArray(new Integer[0]));  }  // Print the final map contents  for (Map.Entry<String, Integer[]> entry:finalMap.entrySet()) {  System.out.print("<" + entry.getKey() + ",[");    // Get the array of integer values  Integer[] values = entry.getValue();   // Print each integer of array  for (int i = 0; i < values.length; i++) {  System.out.print(values[i]);  if (i < values.length - 1) {  System.out.print(",");  }  }  System.out.println("]>");  }  } } 

Output
<aa,[7]> <a,[1,3]> <fg,[5]> <b,[2,9]> <k,[4]> <vc,[10]> 

Explanation of the above code:

  • This program uses the HashMap where numbers are linked to words and rearranges it.
  • It groups numbers based on the words they're linked to, creating arrays for each word with their corresponding numbers.
  • For example, the key "a" in the final map has the value [1, 3]. This means that in the input map, the String value "a" was associated with both the Integer keys 1 and 3.

Conclusion

This method provides an efficient way to convert a HashMap with integer keys and string values to a HashMap with string keys and integer arrays. It leverages temporary HashMaps and Lists for intermediate storage while prioritizing clear and straightforward implementation. The code effectively utilizes Java's built-in data structures and iterators, making it concise and easy to understand.


Explore