• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with hashmaps - selecting keys/values

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm learning Java using BlueJ, I have made a class that has a HashMap of (String, String) that contains an the make of a car and the model.

I want a method to return a collection of all the keys that satisfy a condition, like if someone wants to find what make a certain model is. I know it requires a loop, just not too sure how to write the loop to satisfy the condition if anyone could point me in the right direction?

Thank you so much.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch.

There are 3 important methods from the Map interface you will use often: 1) keySet(), 2) values() and 3) entrySet()

The keySet() method returns a Set of the map's keys
The values() method returns a Collection of the map's values
The entrySet() method returns a Set of Map.Entry (great for looping)

To loop a map
 
Kirstin Jackson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for the welcome.

I have tried using the map.Entry before but blueJ says package modelMap does not exist when I go to compile the program.

 
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kirstin,

ItDoesntWorkIsUseless, please show us some code and tell us where the error occurs.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

I have changed your post to black text; many people find the green difficult to read.
I have suffered from BlueJ myself and am pleased to have recovered from it. I don't like it. If you are new enough to need it, you are better off with the command line, and if you are experienced enough to be able to use an IDE, you can use NetBeans Eclipse or IntelliJ etc. BlueJ seems to have difficulties with package names. Can you compile the whole thing from the command line?
Are you familiar with the different sorts of Collections and Maps? Do you know all about what the Java Tutorials will tell you? If you get the Set of Strings which are keys, would you know how to iterate the set, and put all values satisfying a particular condition into another set? Of course in Java8 you can get a Stream which can be filtered very concisely.
 
Kirstin Jackson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan,
I can post what I have and see if that aids a bit.


that's all I have at the moment cause I just came back on to edit the code a bit more.


Hi Campbell,
I am ready to see the back of bluej myself.

I am not certain I am very familiar with collections yet, but I earlier managed to get the program to list all the keys separate from values, just not able to get certain keys displayed. I am quite fine with the iterating and placing the keys into another string, it might be I need to change from a Map to a List or Set, etc. I am just not too sure how to change the Hashmap or Hashset into an ArrayList or ArraySet.
Not too sure what part is the command line, I know it will compile if I leave the code like it is above.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kirstin Jackson wrote:
I have tried using the map.Entry before but blueJ says package modelMap does not exist when I go to compile the program.



Kirstin Jackson wrote:
I can post what I have and see if that aids a bit.

that's all I have at the moment cause I just came back on to edit the code a bit more.



Since you mentioned that the error involved the package -- perhaps it would be good to show us the complete code? Including the part that includes the package statements?

Henry
 
Kirstin Jackson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,



I added in a bit more code at the part giving me trouble but when I execute it, all it gives me is [] and not the key which should be Renault..








 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kirstin Jackson wrote:
I added in a bit more code at the part giving me trouble but when I execute it, all it gives me is [] and not the key which should be Renault..



So, basically, you solved your compile error -- and is now having a completely different issue. Perhaps you can better describe your new issue?

Henry
 
Kirstin Jackson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what I can see, when the code is executed, it's not returning the key from the value I am entering. I am only getting [], going to try to see if I can sort the issue out or not.

Kirstin
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kirstin Jackson wrote:From what I can see, when the code is executed, it's not returning the key from the value I am entering. I am only getting [], going to try to see if I can sort the issue out or not.

Kirstin



Where is your main method, the entry point to your application? Without that I don't know which part of your code you're attempting to run. Which method is it that isn't doing what you expect?
 
Kirstin Jackson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to run the addValues() method, Mike. The rest of the methods work as expected.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that you are checking for the String "Clio" in your HashMap (modelMap), but the map does not contain a matching key. When you call contains() on a map it only checks the keys. The String you are looking for is in the Set that you have mapped to "Renault".
 
Kirstin Jackson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I see.

That why's it's returning []. Not sure how to call the set up with Renault, since I thought that was the key.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I don't know exactly what it is you were trying to achieve here. Maps are designed so that when you give it a key it will give you back the value mapped to that key.

So modelMap.contains("Renault") will return true if you have a key that matches "Renault" in the map. modelMap.get("Renault") will return you the Set that you mapped to Renault.

If instead you want to search for the word "Clio" or one of the other models then you cannot do that directly using the map interface. The map does not know that you have put sets in it, and will not search inside them for you.

To search in the sets you need to get a reference to the set and call the contains(...) method on it. You can get all the sets using modelMap.values() or by getting the collection of Entry objects and iterating through them. Alternatively you can get a specific set by calling modelMap.get(...)

So what specifically do you want to do?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic