views:

79

answers:

2

i need to see my hashMap keys and values in order to check if it s working properly.but im getting an error for the below lines:

Iterator iterator =  myHashMap.keySet().iterator();
    Flows flows = new Flows();
    while(iterator.hasNext()){
        Object key = iterator.next();
        Object value = myHashMap.get(key); // <--
        //here is the error.  suspicious call to java.util.Map.get 
        //expected type Flows, actual type object

        System.out.println(key+" "+value);
    }

my keys are type of Flows and my values are FlowsStatics.

+3  A: 

Your iterator will automatically return objects of class Flows if you declare your Map as Map<Flows, FlowsStatics>, which you really should:

while(iterator.hasNext()){
    Flows key = iterator.next();
    FlowsStatics value = myHashMap.get(key);
mwittrock
Or typecast to "Flow" and "FlowStatics" if you haven't used generics.
pavanlimo
yes this is my map: "final HashMap<Flows, FlowStatics> myHashMap = new HashMap<Flows, FlowStatics>();"
Red Lion
@mwittrock: You also need to declare the Iterator with a Type parameter in order for your answer to compile!
Adrian Pronk
+4  A: 

Have you declared myHashMap using a Generic type: for example HashMap<Flows, FlowStatics> ?

If so, you should use Generics throughout:

Iterator<Flows> iterator =  myHashMap.keySet().iterator();
while(iterator.hasNext()){
    Flows key = iterator.next();
    FlowStatics value = myHashMap.get(key); // <--

or even:

for(Flows key: myHashMap.keySet().iterator()){
    FlowStatics value = myHashMap.get(key);

or even:

for(Map.Entry<Flows, FlowStatics> entry: myHashMap.entrySet().iterator()){
    Flows key = entry.getKey();
    FlowStatics value = entry.getValue();
Adrian Pronk