Iterating over a map entryset

后端 未结 6 966
生来不讨喜
生来不讨喜 2020-12-16 13:37

I need to iterate over the entry set of a map from which I do not know its parameterized types.

When iterating over such entryset, why this does not compile ?

<
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 13:55

    You're getting the compile time rrror because you are not specifying the type in HashMap

    Like

    HashMap <(Integer,String> hm=new HashMap<(Integer,String>();  
    

    If you specify the type as Integer and String, you don't get the compile time error.

    IF you Dont Know which Value is going to Add in your HashMap then You Use

    HashMap<(Object,Object> hm=new HashMap();  
    

提交回复
热议问题