Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Maps Question

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Maps Question

    I have an odd sort of question. Let's say I have two Maps (map1 and map2) where:
    map1: Map<String,Object>
    map2: Map<Object,Integer>

    map2's keys can only consist of map1's values.

    My end goal is to print out the Integer from map2, which represents the value mapped to the key from map1.

    For example, let's say map1's keys and values looked like:
    "str1", obj1
    "str5", obj2
    "str2", obj4
    and map2's keys and values looked like:
    obj2, 3
    obj1, 2
    obj4, 6
    In the end, I am wanting to print out something like:
    "str1" = 2
    "str5" = 3
    "str2" = 6
    Does anyone have any ideas how to go about doing this? I'm restricted to the Map Interface methods (as it is unknown to me which implementing class was used to make the Map).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Maps Question

    If I understand the question correctly, can you just cascade the gets?
    map2.get(map1.get("str1"));

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Maps Question

    What methods have you tried? Look at the methods available and see which ones get the values you want.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Maps Question

    I might have figured it out. I did sort of what copeg suggested. The only problem is that I don't know the keys for map1. But I managed to get those keys by calling the Map.keySet() method, and then the Set.toArray() method. I have no idea if it worked, but I will know when the rest of the code is put together and I can test it.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Maps Question

    You should be able to test it with a small testing program.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Google Maps
    By Jyothi in forum Other Programming Languages
    Replies: 1
    Last Post: May 14th, 2012, 07:46 AM
  2. Maps and HashMaps
    By moonah in forum Collections and Generics
    Replies: 2
    Last Post: January 26th, 2012, 09:22 AM
  3. JAVA and Google Maps
    By aussiemcgr in forum Java Theory & Questions
    Replies: 7
    Last Post: February 1st, 2011, 08:57 PM
  4. Design question about maps
    By KaleeyJ in forum Java Theory & Questions
    Replies: 1
    Last Post: February 2nd, 2010, 04:17 AM
  5. Convert Maps of String to Map of Maps
    By abhay8nitt in forum Collections and Generics
    Replies: 1
    Last Post: October 27th, 2009, 07:27 AM