Converting Integer-String HashMap to String-Array-Integer HashMap in JavaLast Updated : 6 Jan 2026 In Java, we generally use HashMap to store key-value pairs. Sometimes we need to change the structure of the Map as per requirement. One of the common tasks is to convert a HashMap that has Integer as keys and String as values into another HashMap that uses a String as the key and an array of string as the value. Suppose, we have the following Map: In the above example both key 1 and key 3 have the same value "Pen". Now, we want to reverse the map so that "Pen" becomes the key and the values of this are the original keys stored as strings in an array. So, the result should look like: To perform this, we can use a loop to go through the original Map and collect the key for each value and then convert those keys into a string array. Algorithm
Example: Converting Integer String HashMap to String Array Integer HashMapExampleCompile and RunOutput: Converted HashMap<List<String>, Integer>: [pen, pencil] => 101 [eraser, sharpener] => 102 [scale] => 103 Explanation The above Java program converts a HashMap with Integer keys and String values into a new map where the keys are List<String> and the values are integers. It starts by mapping item IDs and their names (as strings). Then, it loops through each entry, splits the string value into individual words and stores them as a list. This list becomes the key in the new map and the original ID becomes the value. At the end of the program, it prints the new map and shows how each list of words is now linked to its corresponding ID. This helps in organizing data based on word groups instead of plain strings. Advantages
Disadvantages
ConclusionIn reference to the above concept changing an integer-string HashMap into a String-Array-Integer HashMap are very helpful when we need to group all the keys that have the same value. When the same value appears many times it helps in reversing lookups and makes the data more organized. Also, this conversion brings some problems such as uses of extra memory, slower performance and more complexity. The original HashMap is the better choice if we need a one-to-one mapping. But it is important when we group the values the converted structure works well and makes the data easy to manage and analyze. Next TopicElectricity Bill Program in Java |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India