• 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

Map not returning value or key

 
Ranch Hand
Posts: 61
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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

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
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5008
38
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks much guys, i appreciated the explanations. i finally used Map<String,String>.
 
reply
    Bookmark Topic Watch Topic
  • New Topic