• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing a map as a parameter  RSS feed

 
John Goole
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using Java in BlueJ where I'm trying to write a class to store cycling club membership details in a map. I'm trying to pass a map as a parameter to a function that will iterate over the map and print out the member details. My code is:


When I execute the following in the OU workspace:


I can see a map populated with the expected details.

When I execute the following in the OU workspace:


I get the following error:
java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.String
in CtcMembers.printMap(CtcMembers.java:81)
in (OUWorkspace:1)
I wasn't aware I was trying to cast anything so this has got me baffled.
Any help appreciated.
 
Jarrod Rackley
Ranch Hand
Posts: 30
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that the method requires a map<String, String> and the map you create is Map<String, Set<String>>. Adjust the parameter required by the method and you should be good to go.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you get the error is that your getMap() method returns a raw Map (no declared generic type parameters), but your print method takes a Map<String, Set<String>>. A raw Map is not assignable to a Map with declared type parameters because the compiler cannot guarantee type safety.

I don't understand why you are taking your Map out of your members object and then passing it back in to be printed though. It would be much better if your members object abstracted the Map from the callers. It already has a reference to the Map, so it doesn't need to be told which Map to print.
 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… and welcome to the Ranch
 
John Goole
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies

Mike. J. Thompson wrote:I don't understand why you are taking your Map out of your members object and then passing it back in to be printed though.


Neither do I now you come to mention it.

Mike. J. Thompson wrote:It would be much better if your members object abstracted the Map from the callers.


But I'm too dim to know what this means. Any chance you could explain?

Thanks
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I mean is that the code that uses your CtcMembers object does not need to know it contains a Map. The print(...) method should print the members from the Map in the object.
 
John Goole
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:What I mean is that the code that uses your CtcMembers object does not need to know it contains a Map. The print(...) method should print the members from the Map in the object.


Ah! The penny has dropped.


Then execute:


Thanks very much for your help.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!