aspose file tools*
The moose likes Beginning Java and the fly likes HashMap dilemma Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashMap dilemma" Watch "HashMap dilemma" New topic
Author

HashMap dilemma

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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






Jon
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

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).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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

Joined: Aug 16, 2005
Posts: 14350
    
  22

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

Joined: Apr 25, 2008
Posts: 660

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

Joined: Oct 27, 2005
Posts: 19762
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

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

Joined: Oct 13, 2005
Posts: 39869
    
  28
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

Joined: Apr 25, 2008
Posts: 660

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

Joined: Aug 16, 2005
Posts: 14350
    
  22

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

Joined: Oct 27, 2005
Posts: 19762
    
  20

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

Joined: Apr 25, 2008
Posts: 660

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"
 
Consider Paul's rocket mass heater.
 
subject: HashMap dilemma