Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to add keys and values to a map?  RSS feed

 
Rickey Fuller
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to answer the following question:

Create a method called addMapEntry() which takes 2 arguments representing a key and a value. It adds the key and value to the map and if the key is already present the existing value is overwritten. Returns no value.




I run it, but it doesn't work how I want it to. Say if I gave it the key "Gemma" and the value "Reading", it adds reading to Gemma again instead of overwriting it. How would I go about adding a new key to the map as well? If I put a name that isn't already in the map, then I get a java.lang.NullPointerException. I'm not sure if I am being silly or just misunderstanding the question

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66203
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at line 41. What happens if the the get() specifies a key that does not exist?
 
Rickey Fuller
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Look at line 41. What happens if the the get() specifies a key that does not exist?



That explains why I keep getting the error! I feel stupid now lol

I tried clientMap.put(name).add(hobby); but I get another error saying "method put in interface java.util.Map<K,V> cannot be applied to given types". How would I add a new key and value to it?
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the first issue it sounds like you should be overwriting the whole entry? In this case the entry is the Set<String>, not a single String in the Set.
Were you given the class to work with, or is this something you put together?

ETA: It's not that clear to me, so take what I say with a large pinch of salt and get clarification from whoever set the requirements.
 
Rickey Fuller
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:For the first issue it sounds like you should be overwriting the whole entry? In this case the entry is the Set<String>, not a single String in the Set.
Were you given the class to work with, or is this something you put together?

ETA: It's not that clear to me, so take what I say with a large pinch of salt and get clarification from whoever set the requirements.


It is a code I made based on the questions given. I did a bit of searching on my university forums and found someone who was having a similar to problem to me. Someone replied with:

"You don't have to create a new list. The method should be taking as an argument your key along with your value.

method(KeyType aKey, ValueType aValue)

// you then add aKey and aValue to the map

you then create an instance of your class, a key and value then use these as an argument for the method"

Not sure if that is any help, but it has just made me more confused. I think I am gonna take a break from and come back later
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rickey Fuller wrote:"You don't have to create a new list. The method should be taking as an argument your key along with your value.

method(KeyType aKey, ValueType aValue)

// you then add aKey and aValue to the map

you then create an instance of your class, a key and value then use these as an argument for the method"

Not sure if that is any help, but it has just made me more confused. I think I am gonna take a break from and come back later


Well, that shows your method has an incorrect signature.
And now it makes sense.

Your value type is a Set<String>, not a String.
You would then do a put().
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Well, that shows your method has an incorrect signature.
And now it makes sense.
Your value type is a Set<String>, not a String.
You would then do a put().

Alternatively you could do something like

This would manage the sets without the rest of the program needing to know that Set was used behind the scenes.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except that doesn't fulfil the requirements.

'
Create a method called addMapEntry() which takes 2 arguments representing a key and a value. It adds the key and value to the map and if the key is already present the existing value is overwritten. Returns no value.
'

String is not the value. Set<String> is, at least for the example the OP has chosen to write.

The idea seems to be that the method replaces the entry in the map or creates a new one. All a bit pointless as put() already does that, but then there could be an aspect to this we don't know about.
 
Rickey Fuller
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Except that doesn't fulfil the requirements.

'
Create a method called addMapEntry() which takes 2 arguments representing a key and a value. It adds the key and value to the map and if the key is already present the existing value is overwritten. Returns no value.
'

String is not the value. Set<String> is, at least for the example the OP has chosen to write.

The idea seems to be that the method replaces the entry in the map or creates a new one. All a bit pointless as put() already does that, but then there could be an aspect to this we don't know about.


Thank you for all your help! Managed to do it now
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rickey Fuller wrote:Create a method called addMapEntry() which takes 2 arguments representing a key and a value....

Question: Why is 'interests' a member of your class?

You're plainly using it in such a way that it doesn't cause any compilation problems; but the definition itself is redundant, and may run you into difficulties later on.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!