This discussion is archived
10 Replies Latest reply: Jun 25, 2007 11:02 PM by 807600 RSS

map multiple keys to a single value?

807600 Newbie
Currently Being Moderated
Hi all,
I am reading "Objects first with Java"(third edition) and I am kind of stack on page 145.
I am given a little project called techsupport system which simply gets the user input from the terminal window, splits it at the spaces adds the words to a HashSet and passes it to another class which iterates trough the collection. During this each word is passed to a HashMap as a key and if a match is found it prints out the value.
My exercises are:
1. to match more than 1 word to a given response (with only one entry to the map);
2. to use 2 keys and get their mapped value only if both words present in the HashSet.

I can easily do them with conditional statements but I have no clue who to do them with a HashMap. I have been stack on this for 2 days now and would really appreciate your tips. thanks
  • 1. Re: map multiple keys to a single value?
    807600 Newbie
    Currently Being Moderated
    1) It's no problem to have multiple keys to the same value.
    Map<String, String> map = new HashMap<String, String>();
    String key1 = "foo";
    String key2 = "fah";
    String value = "bar";
    map.put(key1, value);
    map.put(key2, value);
    System.out.println(map);
    System.out.println(map.get(key1));
    System.out.println(map.get(key2));
    2) Have a look at the contains() method of java.util.Set. Use an "if" statement.

    ~
  • 2. Re: map multiple keys to a single value?
    807600 Newbie
    Currently Being Moderated
    I think I wasn't very clear on the condition so I will copy them from the book:

    Exercise 5.40: Sometimes two words (or variations of a ward) are mapped to the same response. Deal with this by mapping synonyms or related expresions to the same string, so that you don't need multiple entries in the response map for the same response.

    In other words:
    map.put(foo || fah=bar) //obviously whis won't compile;

    Exercise 5.41. Identify multiple mathing words in the user's input, and respond with a more appropriate answer in that case.

    Something like
    map.(foo && fah=bar)

    I hope that I have been more precise with the description this time.
  • 3. Re: map multiple keys to a single value?
    807600 Newbie
    Currently Being Moderated
    That book sounds really awful.

    The point of a Map is to Map. If you have two different keys that map to the same value, then do that.

    Anyway, the assignment questions are crap. "respond with a more appropriate answer in that case"? That basically means nothing.

    Anyway...maybe all it means by the first question is that you can re-use the same value in a Map. You might have two entries where the value is the exact same object.

    Do the simplest thing that works. A book that encourages nasty kluges is to be avoided.
  • 4. Re: map multiple keys to a single value?
    807600 Newbie
    Currently Being Moderated
    > I hope that I have been more precise with the description this time.

    Okay, so what have you tried?

    ~
  • 5. Re: map multiple keys to a single value?
    807600 Newbie
    Currently Being Moderated
    As I said I managed to complete the task by passing the HashSet with the words from the input to a method full of if statements.
    Something like:
    public String response(HashSet<String>words) {
    if (words.contains("hard") {
    System.out.println("Hard! It's more like imposible!");
    }
    if(words.contains("I") && words.contains("love") {
    System.out.println("you are having a laugh!");
    }..................
    But if I do it this way I don't use hashmap. What made me think that there might be a way of comleting the exercise is that it is graded as challenging by the auther (using if statements is not challenging at all).
    Also it makes sence having a method in HashMap which would allow you to map 2 keys to single value ( taking memory usage into cosideration). But then again, what type should be the key? For example, if I want to match 2 keys with ANY 2 words in a HashSet with 50 objects then there are 50*50 possible combinations...
    However, I will stop thinking here, otherwise you will get my headache. May be the best thing is the write to the authers.
  • 6. Re: map multiple keys to a single value?
    807600 Newbie
    Currently Being Moderated
    Also it makes sence having a method in HashMap which
    would allow you to map 2 keys to single value (
    taking memory usage into cosideration). But then
    again, what type should be the key?
    You could use a Set as the key in that case. Example:
    Set set = new HashSet();
    set.add("foo");
    set.add("bar");
    Map map = new HashMap();
    map.put(set, "baz");
    set = new HashSet<String>();
    set.add("bar");
    set.add("foo");
    System.out.println(map.get(set)); // baz
    ~
  • 7. Re: map multiple keys to a single value?
    807600 Newbie
    Currently Being Moderated
    yawmark, you had some generic code in there that you took out (due to the forum messing up I think). Any chance you could try to put it back in,... perhaps with extra white-space that we can delete...?

    Also a question: is it possible to have a HashMap that accepts either a String or a String set (in other words it potentially can accept both) as a key?

    Thanks
  • 8. Re: map multiple keys to a single value?
    807600 Newbie
    Currently Being Moderated
    > yawmark, you had some generic code in there that you
    took out (due to the forum messing up I think). Any
    chance you could try to put it back in,... perhaps
    with extra white-space that we can delete...?

    I see I missed removing all the "genericization". Sorry about that. Anyway, if you know about generics, I just declared the Map to take a Set<String> type as the key and a String type as the value. If you don't know about generics, then don't worry about adding it until you're more familiar with it.

    > Also a question: is it possible to have a HashMap
    that accepts either a String or a String set (in
    other words it potentially can accept both) as a
    key?

    Sure, but it's probably not going to mesh with your goal of only having one entry per value in the Map (not a very reasonable goal, but it sounds like that's what you think the authors are looking for). To accomplish that with only Sets as the key, you can implement your own map-type ADT, with calls to get(String s) iterating through the keys and calling contains() to see if the key contains the given string. If you find one that does, return the value mapped to that key. Otherwise, return null.

    ~
  • 9. Re: map multiple keys to a single value?
    807600 Newbie
    Currently Being Moderated
    Thanks.

    Oh I know about generics, but they say in programming as they say in karate, a little knowledge is dangerous.
  • 10. Re: map multiple keys to a single value?
    807600 Newbie
    Currently Being Moderated
    You could use a Set as the key in that case.
    Yes, I thought about this but didn't try it for two reasons:
    1. I have to pass a set to the map. This set will contain all the words from the user input. The set used as a key contains only 2 words. So I thought because both sets have diffirent content the value mapped to the key set will not be returned.
    2. Even if a value is returned this will be only in case both words are found in the input set. I want the have the mapped value returned if ANY of the Striings in the key set matches a String in the .input set.

    But I will give it a try. There might be something about set interface which I don't know.