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

  • Home
  • SEARCH
  • 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 8657857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:37:31+00:00 2026-06-12T15:37:31+00:00

I’m stuck on how to transfer key-value pairs from map1 into map2 only if

  • 0

I’m stuck on how to transfer key-value pairs from map1 into map2 only if each key has a unique value in map1.

Let’s say I have the following maps:

  • map1: [1,2] [2,4] [4,4]
  • map2: [1,2] [2,4]

I suppose the algorithm would be:

  1. Loop through entries in the first map.
  2. Add a key to map2.
  3. Add a value to a set which checks against the values of map2
  4. If the values are duplicate the value doesn’t get added to the set and disregard adding its corresponding key to map2.

Code snippet:

public static <K,V> Map<K,V> unique (Map<K,V> m) {
  Map<K,V> newMap = new ArrayMap<K,V>();

  //Remember all values in the newMap.
  Set<V> holding = new ArraySet<V>(newMap.values());

  for (Map.Entry<K, V> graphEntry : m.entries()) {
     //not sure.
  }

  return newMap;  
}

Is my idea of how its supposed to be done on the right track? Quite lost here.

  • 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-12T15:37:32+00:00Added an answer on June 12, 2026 at 3:37 pm

    From a Map<K, V> create a Map<V, K> that will add the item if and only if the key is not in the map. Using this Map<V, K>, recreate your Map<K, V>.

    public static <K, V> Map<K, V> createMap(Map<K, V> m) {
        Map<K, V> map = new HashMap<K, V>();
        Map<V, K> tmpMap = new HashMap<V, K>();
        for(Map.Entry<K, V> entry : m.entrySet()) {
            if (!tmpMap.containsKey(entry.getValue())) {
                tmpMap.put(entry.getValue(), entry.getKey());
            }
        }
        for(Map.Entry<V, K> entry : tmpMap.entrySet()) {
            map.put(entry.getValue(), entry.getKey());
        }
        return map;
    }
    

    If you need to keep the preserver order of the data, use LinkedHashMap instead of HashMap.

    • 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 parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
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've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.