• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap dilemma

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to figure out whether it is possible to read a HashMap (or other collection) of objects, and, their values. Moreover, when compiling the code below using the command line, I am getting warnings reading unchecked call to put (K,V) as a member of the raw type java.util.HashMap.



Command line
>javac -Xlint IterateValuesOfHashMapExample.java
IterateValuesOfHashMapExample.java:26: warning: [unchecked] unchecked call to pu
t(K,V) as a member of the raw type java.util.HashMap
hMap.put("1","One");
^
IterateValuesOfHashMapExample.java:27: warning: [unchecked] unchecked call to pu
t(K,V) as a member of the raw type java.util.HashMap
hMap.put("2","Two");
^
IterateValuesOfHashMapExample.java:28: warning: [unchecked] unchecked call to pu
t(K,V) as a member of the raw type java.util.HashMap
hMap.put("3","Three");
^
IterateValuesOfHashMapExample.java:30: warning: [unchecked] unchecked call to pu
t(K,V) as a member of the raw type java.util.HashMap
mapofEmployees.put("1", employeeOne);
^
IterateValuesOfHashMapExample.java:31: warning: [unchecked] unchecked call to pu
t(K,V) as a member of the raw type java.util.HashMap
mapofEmployees.put("2", employeeTwo);
^
IterateValuesOfHashMapExample.java:32: warning: [unchecked] unchecked call to pu
t(K,V) as a member of the raw type java.util.HashMap
mapofEmployees.put("3", employeeThree);
^
6 warnings





 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You get those warnings because you are using a raw HashMap, so the compiler cannot check if your code is typesafe. Use generics to enable the compiler to check this:

Likewise for the Collection (line 33) and the Iterator (line 36).
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:You get those warnings because you are using a raw HashMap, so the compiler cannot check if your code is typesafe. Use generics to enable the compiler to check this:

Likewise for the Collection (line 33) and the Iterator (line 36).


Thanks, that seemed to work, is there some way I can read methods from the BuggyEmployee method by iterating through the Map?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "read methods from the BuggyEmployee method"? When you iterate through the values of the map, you iterate over BuggyEmployee objects, and ofcourse you can call any methods that are defined in class BuggyEmployee on those objects.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:What do you mean by "read methods from the BuggyEmployee method"? When you iterate through the values of the map, you iterate over BuggyEmployee objects, and ofcourse you can call any methods that are defined in class BuggyEmployee on those objects.


How can I call any methods defined in class BuggyEmployee, whilst iterating through the values of the map?
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your Map has the right generic types. That means that, if you are able to iterate over it, you can access the elements without needing a cast.

Check out the Javadoc page of java.util.Map. Find all methods that return something you can iterate over: java.util.Collection, java.util.List, java.util.Set, java.util.Queue, java.util.Deque, etc. Post the results here, and tell us for each of them why that one is good or not.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Your Map has the right generic types. That means that, if you are able to iterate over it, you can access the elements without needing a cast.

Check out the Javadoc page of java.util.Map. Find all methods that return something you can iterate over: java.util.Collection, java.util.List, java.util.Set, java.util.Queue, java.util.Deque, etc. Post the results here, and tell us for each of them why that one is good or not.


Well this does not seem to work...

 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Map is like an address book. You "put" by writing the name and address in it (two things), then you "get " by looking up the name (one thing) and finding the address. So you need to check how many arguments you ought to pass to the get method.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:A Map is like an address book. You "put" by writing the name and address in it (two things), then you "get " by looking up the name (one thing) and finding the address. So you need to check how many arguments you ought to pass to the get method.


Oh right, this works, somehow I did not get it together the first time round. By the way what its.hasNext() - see line 41 below; it does not seem to be equal to the hashCode()



Output
BuggyEmployee@19821f
BuggyEmployee@addbf1
BuggyEmployee@42e816
1: Joe
1 hashCode() = : 4384790
2: Peter
2 hashCode() = : 11394033
3: Marsa
3 hashCode() = : 1671711



Now thinking that my code was a bit of a "dirty hack", I did an override on the BuggyEmployee.toString(), so it's more in line with what my book says



 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Text like "BuggyEmployee@19821f" is what you get when you don't override toString(), i.e. that's what the default toString() of class Object returns. Lookup the documentation of toString() in class java.lang.Object for more information.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Camilleri wrote:By the way what its.hasNext() - see line 41 below; it does not seem to be equal to the hashCode()

I know a great site for you: http://download.oracle.com/javase/6/docs/api/. Learn to use it, to find classes, methods, etc. Then lookup what these methods will do. I'll help you out to narrow down your search a bit: http://download.oracle.com/javase/6/docs/api/index-files/index-8.html. Scroll down until you find a method that looks like that one, belonging to the right class.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
Jon Camilleri wrote:By the way what its.hasNext() - see line 41 below; it does not seem to be equal to the hashCode()

I know a great site for you: http://download.oracle.com/javase/6/docs/api/. Learn to use it, to find classes, methods, etc. Then lookup what these methods will do. I'll help you out to narrow down your search a bit: http://download.oracle.com/javase/6/docs/api/index-files/index-8.html. Scroll down until you find a method that looks like that one, belonging to the right class.


Noted, I had a "blond moment"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic