• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap - Return multiple keys -OR- values  RSS feed

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone;

I need help with the following;

Here is my HashMap and a method for listing all the keys in it

HashMap<String, String> exampleOne = new HashMap<String, String>();


public void allKeys()
{
int i;
i =0;
for (String name: exampleOne.keySet())
{

String key =name.toString();
String value = exampleOne.get(name).toString();
i++;
System.out.println("Key "+ i +": "+ key );

}
}

Now I want to return all values that associated with one key. How do I do this?
Or is it possible to other way round? I mean return All keys associated with a value? <<<< This is the main part that im interested in.

Thank you.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keys are unique (a HashMap cannot contain the same key more than once), and each key is associated with exactly one value.

So, if you are talking about "all values associated with one key" - there is only one value associated with each key.

Values are not unique, so the same value can be stored under different keys. Suppose that you have some value and you want to find out under what keys it is in the map, then the only way to do that is to iterate over the whole map and for each key-value pair check if the value is what you are looking for.

Libraries like Google Guava have special map interfaces and classes, for example Multimap which allows you to store multiple values under one key, and BiMap ("bidirectional map") which allows you to find not only values by key, but also keys by value efficiently.
 
Hans Baba
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks a bit challenging for me can we say that a multimap is kind of HashMap? Do I need to use it instead of hashmap above to return multiple keys or do I need to create new one in the method itself? im a bit confused.

 
Hans Baba
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, I managed to do it as follow;


and it return keys in the following format;

"[key1,key2,key3]"

however, I want it to return it like;

"key1:key2:key3"

:S i need some clue!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of comments about that code:

- Never use == to compare Strings. It might have worked this time, but it won't be consistent. Use the equals() method.

- There's no need to check whether the number is in the value set first. If it isn't you'll still get an empty list. As it stands your code is searching through the map twice (because containsValue will have to do that - at least until it finds the first one).

If you want to convert it to a String with a particular format you'll have to do that yourself. If you're using Java 8 there's a convenient method in the String class that will help you (join), otherwise you need to iterate through the list and build up the String yourself.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hans Baba wrote:okay, I managed to do it as follow

I hate to say, but what you've done is quite tortuous.

Let me describe what you're doing in English:
  • You check to see if the Map contains the value you're looking for (OK, but unnecessary; containsValue() is quite a "heavy" operation).
  • If it does, you:
  • Go through ALL the keys in the Map.
  • Check if any of their values equals the number you supplied - and that check is WRONG - have a look at the AvoidTheEqualityOperator (←click) page to find out why.

  • It may work (once you get that check corrected) but there's a better way to do it using the Map's entrySet(). See if you can work out what it is.

    Furthermore:
  • You return them as a String.
  • That String is not in the format you want.

  • My advice: Concentrate on one thing at a time.

    If you want to return all "matching" keys, just return them - maybe as a List<String> - and let some other method deal with formatting them into a String.

    Alternatively, forget about converting them altogether. Normally, StringsAreBad, so you really only want to use them when you know that you're going to "display" something.

    Winston

    [Edit] Too late. Got sidetracked
     
    Hans Baba
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just too tired and fed up. can someone PM me a solution or show me any example that I can check?
     
    Marshal
    Posts: 4051
    239
    Clojure IntelliJ IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hans Baba wrote:Just too tired and fed up. can someone PM me a solution or show me any example that I can check?

    I'm going to try that with my manager next week. "Sorry boss, don't fancy doing any hard work today, you do it". We can all guess how well that will go....

    If you're tired, have a break and come back to your problem fresh. You'd be surprised how effective that can be.

    Unfortunately though, nobody here is going to provide you with a ready made solution. We love to help, and more specifically we love to help you learn for yourself. If we just handed out the solution then you would not have learned anything.
     
    Hans Baba
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "- There's no need to check whether the number is in the value set first. If it isn't you'll still get an empty list."
    Yes, and this is the requirement in the exercise itself


    Normally, StringsAreBad, so you really only want to use them when you know that you're going to "display" something.
    And this.


    I need to convert arraylist to colon separated string, but I cant use google guava or apache commons for some reasons (I use BlueJ IDE) There should be another way to do this. Any guidance?
     
    Matthew Brown
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hans Baba wrote:
    I need to convert arraylist to colon separated string, but I cant use google guava or apache commons for some reasons (I use BlueJ IDE) There should be another way to do this. Any guidance?


    Which version of Java? If it's Java 8, as I said, there's a static method in the String class that will do it. Otherwise, iterate over the list and build up a String by adding the value and a colon.
     
    Hans Baba
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    OKAY, I made the following changes on the code and it returns the content in the format I want.






    HOWEVER, it returns some of the content multiple times.

    Like, i.e "key1:key1:key1:key2:key3"

    what is wrong this time? :/
     
    Hans Baba
    Ranch Hand
    Posts: 36
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    NVM, just moved down the for-each loop and its solved
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!