Java – type iterator The next () method returns to the character class
•
Java
I tried to display the contents of the following HashMap:
HashMap<Character,Integer> hm = new HashMap<Character,Integer>();
I print out content using the following methods:
Set hmset = hm.entrySet(); Iterator iterator = hmset.iterator(); while(iterator.hasNext()) { Character key = new Character(iterator.next()); Sy@R_404_2354@.out.println("key : "+key+"value : "+(Integer)hm.get(key)); }
I received the following error:
error: constructor Character in class Character cannot be applied to given types;
I also tried the following type conversions:
Character key = (Character)iterator.next();
But that won't work Any help would be appreciated thank you..
Solution
Parameterize your iterator and use the keyset:
Iterator<Character> iterator = hm.keySet().iterator();
explain
>Iterator is a generic type and should be parameterized In this way, you can call next. > Calling entryset will return set < entry < character, integer > >. If you are iterating keys, it will make your life more complex
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码