• Post Reply Bookmark Topic Watch Topic
  • New Topic

Map not returning value or key  RSS feed

 
adebari olalekan
Ranch Hand
Posts: 61
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i created a map<> that will receive a string and a set<> as an argument. but when i tried to get the key or value what i receive is
null




hope someone can help.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, since the "todo" map is declared as taking a Set<String> as a key -- and *not* a string key -- shouldn't it be obvious why there is no value to any string key?

Henry
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you trying to debug your problem? 
Print out the values of the Map, the keySet,  the keys and the values to see what the code is doing.
 
adebari olalekan
Ranch Hand
Posts: 61
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:How are you trying to debug your problem? 
Print out the values of the Map, the keySet,  the keys and the values to see what the code is doing.


i have tried to print out the key as well as the value by ordinarily
and the keys as well as the values printed out perfectly.

i also tried to get only the keys with
and the keys printed out fine.
but when i tried getting a single value or key is when it returns
null
 
adebari olalekan
Ranch Hand
Posts: 61
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Well, since the "todo" map is declared as taking a Set<String> as a key -- and *not* a string key -- shouldn't it be obvious why there is no value to any string key?

Henry

i dont get your point well, but since i declared the map as  Map<Set<String>,String> todo = new LinkedHashMap<>(); i also supplied the Set<Strings>, with an array, convert it to String and use the add() method to add it to the Set. can you elucidate what you think is wrong further. thanks for the help
 
Carey Brown
Saloon Keeper
Posts: 3314
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are searching for "MONDAY:", which is a String. The key is expecting a **SET** that contains "MONDAY:".
 
Carey Brown
Saloon Keeper
Posts: 3314
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want a Map where you can search for a key of the String "MONDAY:" you would have had to declare the Map as:
 
adebari olalekan
Ranch Hand
Posts: 61
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:If you want a Map where you can search for a key of the String "MONDAY:" you would have had to declare the Map as:

the reason i used it that way is because i have been declaring the Map that way and i really admired how it worked without any memory flaws. let me show a code ii wrote and it worked fine by declaring map as Map<Set<String>,String> todo = new LinkedHashMap<>():



this code compiled and run perfectly , please can you tell why it worked and this one didnt
 
Carey Brown
Saloon Keeper
Posts: 3314
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
adebari olalekan wrote:
Carey Brown wrote:If you want a Map where you can search for a key of the String "MONDAY:" you would have had to declare the Map as:

the reason i used it that way is because i have been declaring the Map that way and i really admired how it worked without any memory flaws. let me show a code ii wrote and it worked fine by declaring map as Map<Set<String>,String> todo = new LinkedHashMap<>():

this code compiled and run perfectly , please can you tell why it worked and this one didnt

In your working code you declared the Map differently:

vs
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the keys as well as the values printed out perfectly.

No, there is a subtle difference:

        //{[MONDAY:]=4:00am: WRITE POEM, 7:00am:EXERCISE, 8:00am: VOCABULARY BUILDING, ...   < note extra [ ]s
        //{MONDAY:=4:00am: WRITE POEM, 7:00am:EXERCISE, 8:00am: VOCABULARY BUILDING, ...

The first line is for the Map<Set<String>,String>
the second line is for Map<String,String>
 
Carey Brown
Saloon Keeper
Posts: 3314
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question is, do you want the key to be a String or a Set<String>?
 
Carey Brown
Saloon Keeper
Posts: 3314
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I strongly suspect that you want the key to be a String, that way you can do a search by:

If you insist on the key being a Set<String> then you'd have to search like this:

It would help if you state what your use case is. Example
"I want to find all the things I need to do on Monday."
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see no need to tiptoe around this: Using a Set<String> as the map key in this case is just plain wrong.  You have a list of things to do that you want to map to the day on which you want to do them.  The key is a String. The "list" of things you want to do are also represented as a String.  There is nothing here that would justify the use of a set.  The most logical way to define the map would be as Map<String, String> if that's really what you want for your key and value.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even in the code that you say worked perfectly, there was no need to use a Set.  You only add one String to the set that you map to each country anyway. Using a set that only contains one element makes no sense in that case.  It would have worked equally well if you just used a Map<String, String> to map each country to its form of government.

That being said, a Map is not even a very good solution. Name and form of government are attributes of a Country, so it really would have been better if you created a Country class with a constructor that took the name and form of government as its arguments.
 
adebari olalekan
Ranch Hand
Posts: 61
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks much guys, i appreciated the explanations. i finally used Map<String,String>.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!