Converting Integer-String HashMap to String-Array-Integer HashMap in Java Last Updated : 29 Dec, 2023 Comments Improve Suggest changes 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 ConversionThe 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.ConclusionThis 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. Comment More infoAdvertise with us Next Article Converting Integer-String HashMap to String-Array-Integer HashMap in Java yaduttampareek95 Follow Improve Article Tags : Java Java Programs Java-Collections Java-Strings Java-Class and Object Java-HashMap +2 More Practice Tags : JavaJava-Class and ObjectJava-CollectionsJava-Strings Similar Reads Java Program to Convert Integer List to Integer Array There are many ways to convert integer List to ArrayList where in this article we will be discussing out 2 approaches as below: Using concept od streams in Java8Using Apache Commons LangUsing Guava Library Method 1: Using concept od streams in Java8 So, first, we will get to know about how to conver 3 min read Program to Convert Set of Integer to Array of Integer in Java Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. A Stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. 2 min read Java Program to Convert String to Integer Array In Java, we cannot directly perform numeric operations on a String representing numbers. To handle numeric values, we first need to convert the string into an integer array. In this article, we will discuss different methods for converting a numeric string to an integer array in Java.Example:Below i 3 min read How to Copy One HashMap to Another HashMap in Java? HashMap is similar to the HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values. This class makes no guarantees as to the order of the map. To use this class and its methods, you need to 4 min read Converting ArrayList to HashMap in Java 8 using a Lambda Expression A lambda expression is one or more line of code which works like function or method. It takes a parameter and returns the value. Lambda expression can be used to convert ArrayList to HashMap. Syntax: (parms1, parms2) -> expressionExamples: Input : List : [1="1", 2="2", 3="3"] Output: Map : {1=1, 2 min read Convert HashSet to array in Java Java HashSet class is used to create a collection that uses a hash table for the storage of elements. It inherits AbstractSet class and implements Set Interface. The key points about HashSet are: HashSet contains unique elements only.HashSet allows null values.The insertion of elements in a HashSet 2 min read How to Implement a Custom Hash function for Keys in a HashMap in Java? In Java, HashMap is the data structure that implements the Map interface. This is used to save the data in the form of key-value pairs. In this article, we will learn how to implement a Custom Hash function for keys in a HashMap in Java. In Java, implementing the custom hash function for keys in a H 2 min read How to Convert Comma Separated String to HashSet in Java? Given a Set of String, the task is to convert the Set to a comma-separated String in Java. Examples: Input: Set<String> = ["Geeks", "ForGeeks", "GeeksForGeeks"] Output: "Geeks, For, Geeks" Input: Set<String> = ["G", "e", "e", "k", "s"] Output: "G, e, e, k, s" There are two ways in which 2 min read Program to convert List of Integer to List of String in Java The Java.util.List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and S 3 min read Program to convert List of String to List of Integer in Java The Java.util.List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector, and 3 min read Like