• 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

HashMap dilemma

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





 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Marshal
Posts: 79151
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
reply
    Bookmark Topic Watch Topic
  • New Topic