Results 1 to 6 of 6
  1. #1
    coyne20 is offline Member
    Join Date
    Feb 2009
    Posts
    20
    Rep Power
    0

    Default Is there a way to combine or merge 2 sorted maps into 1 based on a common key?

    I have a small problem to solve by which I would like to merge 2 sorted maps into 1.

    Map A
    -------
    Keys, Values
    1, A
    2, B
    3, C
    4, D
    5, E

    Map B
    -------
    Keys, Values
    1, 10
    2, 20
    3, 30
    4, 40
    5, 50

    Final Map should look like:
    Keys, Values
    A, 10
    B, 20
    C, 30
    D, 40
    E, 50

    The final map would have all the values from Map A as a key and the values from Map B as values in the Final Map.

    Is there a way to do this using Java?

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,012
    Rep Power
    6

    Default Re: Is there a way to combine or merge 2 sorted maps into 1 based on a common key?

    Of course it is possible using Java; the Map itself has methods to put stuff in them, take stuff out of them and loop over both the available keys and the available values; you don't need any more to write the code to construct the third map.

    The question is: can YOU do it? Perhaps you need to study the Map interface and the HashMap class a little more.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Is there a way to combine or merge 2 sorted maps into 1 based on a common key?

    I would iterate over the keys of map A, checking if the same key exists in map B, and adding the values to your final map as you go. You'll need to decide what happens if a key exists in one map but not the other.
    Get in the habit of using standard Java naming conventions!

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,606
    Rep Power
    5

    Default Re: Is there a way to combine or merge 2 sorted maps into 1 based on a common key?

    Hmm, of course you can do it. But it appears you can do this regardless of whether the initial maps are sorted or not as long as they have the same keys (baring the special situation which kjkrum mentioned). But since you need a sorted map for the final combination, you may not need to sort the first maps unless its required for some other purpose. This has nothing to do with your problem but I thought I would mention it as it seems like it would be a tad more efficient.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Join Date
    Apr 2014
    Posts
    1
    Rep Power
    0

    Lightbulb Re: Is there a way to combine or merge 2 sorted maps into 1 based on a common key?

    package org.gpk.javabrains;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.Set;

    public class twomap {

    public static void main(String[] args) {

    Scanner scan =new Scanner(System.in);

    System.out.println("Enter the map1 size");
    int map1size = scan.nextInt();
    Map map1 = new HashMap();
    for(int i=0; i<map1size; i++){
    System.out.println("Enter the map1 Key");
    int map1key = scan.nextInt();
    System.out.println("Enter the map1 Value");
    String map1val = scan.next();
    map1.put(map1key, map1val);
    }
    System.out.println("Enter the map2 size");
    int map2size = scan.nextInt();
    Map map2 = new HashMap();
    for(int i=0; i<map2size; i++){
    System.out.println("Enter the map2 Key");
    int map2key = scan.nextInt();
    System.out.println("Enter the map2 Value");
    String map2val = scan.next();
    map2.put(map2key, map2val);
    }
    System.out.println(map1);
    System.out.println(map2);
    StringBuilder s2b = new StringBuilder();
    Map map3 = new HashMap();
    Set set = new HashSet();
    Iterator it1 = map1.keySet().iterator();
    while(it1.hasNext()){
    set.add(it1.next());

    }

    Iterator it2 = map2.keySet().iterator();
    while(it2.hasNext()){

    set.add(it2.next());
    }
    Iterator it3 = set.iterator();

    while(it3.hasNext()){
    Object obj = it3.next();
    if(map1.containsKey(obj)&&map2.containsKey(obj)){

    map3.put(map1.get(obj), map2.get(obj));
    }

    }
    System.out.println(map3);
    }

    }
    Last edited by karthickgpece@gmail.com; 04-04-2014 at 06:30 PM.

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,606
    Rep Power
    5

    Default Re: Is there a way to combine or merge 2 sorted maps into 1 based on a common key?

    What's all of this??

    Please read the forum rules before posting. This thread is about 9 months old so the OP probably found a solution. You should never provide a solution to a requested problem as that is spoon feeding and frowned upon (besides you solution has unnecessary code). Please put your code between code tags to properly format any examples of code (but again, no specific solutions).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Combine Error
    By cloud in forum Eclipse
    Replies: 1
    Last Post: 12-07-2011, 09:26 AM
  2. Replies: 1
    Last Post: 08-11-2011, 07:56 PM
  3. Combine two S.O.P in one
    By javauserjava in forum New To Java
    Replies: 5
    Last Post: 04-05-2011, 06:27 AM
  4. Merge 2 XMLs Based on Schema
    By wowstartup in forum XML
    Replies: 0
    Last Post: 02-12-2009, 05:46 PM
  5. How can I do this? Combine variable.
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 01-14-2009, 05:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •