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 8637043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:22:50+00:00 2026-06-12T10:22:50+00:00

I’m trying to copy the keys and values from one map, map1, into another

  • 0

I’m trying to copy the keys and values from one map, map1, into another map, map2. The values in map 1 are stored in a set and map 2 should store map1’s values in a list. The keys in each should map the same in both maps.

I could loop through the keys in map1 then add those keys to map 2. And have another inner for loop to add each set’s elements to the list in map2 but I’m not sure if this is a right way to go about it, or even correct.

public static <K, V> void changeSetToList (Map<K, Set<V>> map1, Map<K, List<V>> map2) {
for (Map.entry<K, Set<V>> entry : m1.keys()) 
  for (List<V> l : m1.values()) 
      m2.put(entry.getKey(), l.getValue());

}

I haven’t compiled or tested it yet though. No access to computer.

  • 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-12T10:22:52+00:00Added an answer on June 12, 2026 at 10:22 am

    You can iterate through the Map and use new ArrayList(Collections) constructor to create a List out of the Set stored in original Map.. And put it into new Map..

        Map<String, Set<String>> givenMap = new HashMap<String, Set<String>>();
        Map<String, List<String>> newMap = new HashMap<String, List<String>>();
    
        Set<String> newSet = new HashSet<String>();
        newSet.add("rohit");
    
        givenMap.put("a", newSet);
        givenMap.put("b", newSet);
        givenMap.put("c", newSet);
    
        for (String str: givenMap.keySet()) {
            newMap.put(str, new ArrayList<String>(givenMap.get(str)));
        }
    
        for(String str:newMap.keySet()) {
            System.out.println(newMap.get(str));
        }
    

    And if you want to use a Generic method.. You need to change your method to this: –

    public static <K, V> void changeSetToList (Map<K, Set<V>> givenMap, 
            Map<K, List<V>> newMap) {
    
        for (K str: givenMap.keySet()) {
            newMap.put(str, new ArrayList<V>(givenMap.get(str)));
        }
    
        for(K str:newMap.keySet()) {
            System.out.println(newMap.get(str));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

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.