how to call an Iterator over a hashMap
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.
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();
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);
精彩评论