Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8535407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:25:25+00:00 2026-06-11T10:25:25+00:00

I have a Map where key is a String and value is comma seperated

  • 0

I have a Map where key is a String and value is comma seperated list of values.

How can I reverse the ordering of the key/value pairs and assign to new Map so that keys become values and the values become keys

So :

key   value
key1  value1,value2,value3
key2  value1,value4,value5        
key3  value4,value2,value1

becomes :

key       value
value1    key1,key2,key3
value2    key1,key3
value3    key1
value4    key2,key3
value5    key2

A possible solution is to iterate over each value and then iterate of every key
searching for same corresponding value. If found add this new key/value pair to a new Map.
This seems inefficient ?

Solution (implemented using accepted answer) :

import java.util.HashMap;
import java.util.Map;

public class MapTransformer {

    public static void main(String args[]) {

        Map<String, String> m1 = new HashMap<String, String>();

        m1.put("key1", "value1,value2");
        m1.put("key2", "value5");
        m1.put("key3", "value4,value2,value1");

        Map<String, String> inverseMap = new HashMap<String, String>();
        for (Map.Entry<String, String> entry : m1.entrySet()) {
            for (String value : entry.getValue().split(",")) {
                if (inverseMap.containsKey(value))
                    inverseMap.put(value, inverseMap.get(value) + "," + entry.getKey());
                else
                    inverseMap.put(value, entry.getKey());
            }
        }

        for (Map.Entry<String, String> entry : inverseMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+" "+value);
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:25:26+00:00Added an answer on June 11, 2026 at 10:25 am
    Map<String,String> initialMap = ...
    Map<String,String> inverseMap = new HashMap<String,String>();
    for (Map.Entry<String,String> entry: initialMap.entrySet()) {
        for (String v : entry.getValue().split(",")) {
            if (inverseMap.containsKey(v)) 
              inverseMap.put(v,inverseMap.get(v)+","+entry.getKey());
            else
              inverseMap.put(v, entry.getKey());
        }
    }
    

    Note that this does not sort the strings in the inverse map. To do that you then do a second iteration through the inverse map splitting and rejoining the strings.

    A better solution is to model the data as a SortedMap<String,SortedSet<String>>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code private Map<KEY, Object> values = new HashMap<KEY, Object>(); public
We have a number of Scala classes returning Map[String,String] (key,value) results for storage in
I have a key/value pairs mapped by hash (ID) in Dictionary<string, Dictionary<string, string>> called
I have map with key and value and my goal is to get list
I have an std::map<std::string, float> so I can do quick lookups for float values
I have a map declaration: <!-- SOME MAP --> <util:map id=someMap map-class=java.util.HashMap key-type=java.lang.String value-type=java.lang.String
I have map where each key is a String. To access a value I
I have something like this: Map<String, String> myMap = ...; for(String key : myMap.keySet())
In a controller, I have populated a map that has a string as key
I have a java map with string in its key and integer in its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.