Typecasting the return type of iterator.next() method to Character class

kajfhk

I am trying to display the contents of the following HashMap :

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

I have used the following method to print out the contents :

Set hmset = hm.entrySet();
Iterator iterator = hmset.iterator();
while(iterator.hasNext())
    {
    Character key = new Character(iterator.next());
    System.out.println("key : "+key+"value : "+(Integer)hm.get(key));
}

I am getting the following error :

error: constructor Character in class Character cannot be applied to given types;

I have also tried the following way of type casting :

Character key = (Character)iterator.next();

but that would'nt work either. Any help greatly appreciated. Thanks..

Mena

Parametrize your Iterator and use keySet:

Iterator<Character> iterator = hm.keySet().iterator();

Explanation

  • Iterator is a generic type and should be parametrized. This way, you invoke next without having to cast from Object to your desired type.
  • Invoking entrySet will return a Set<Entry<Character, Integer>>, which complicates your life unnecessarily if you're iterating the keys

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Which class implements iterator's Next() method?

From Dev

Calling a Factory without typecasting the return type

From Dev

Method parametrized by a class' method return type

From Dev

Why int type method is allowing character type as return value?

From Dev

Java Method Unbounded Type or Class Return

From Dev

Generics - method return type as extending class

From Dev

Private class as return type from public method

From Dev

Infer class generic property method return type

From Dev

How to specify the return type of the method of an abstract class

From Dev

How to specify the return type of the method of an abstract class

From Dev

How to check template class method return type?

From Dev

What is method return type of class InfoChyb

From Dev

How to create a next method for a class when the next method is supposed to return the values of two generators?

From Dev

Java language spec: "an invocation of Class" as return type of method in annotation type

From Dev

Can a Base Class Method return the type of the derived class?

From Dev

Return type of child class from parent class method

From Dev

Using "next" in an "iterator" and getting type mismatch error

From Dev

How can I return a dependent type from templated class method?

From Dev

C++ method inheritance with return type depending on class

From Dev

Make a interface method return an object of type of the class which implemented it

From Dev

Private class member vs. Method with return type

From Dev

Unable to create a method of return type Object in a child class

From Dev

C++ Return a Type of object in a method of the same class

From Dev

Method in generic class does not return type specified. Why?

From Dev

Typescript parent class method return type change by child property

From Dev

Make a interface method return an object of type of the class which implemented it

From Dev

override the return type of method declared in the interface after its implemented in class

From Dev

Single method to return an instance of different generic type of a class

From Dev

What is the return type for JPA query method when query result is not a class?

Related Related

  1. 1

    Which class implements iterator's Next() method?

  2. 2

    Calling a Factory without typecasting the return type

  3. 3

    Method parametrized by a class' method return type

  4. 4

    Why int type method is allowing character type as return value?

  5. 5

    Java Method Unbounded Type or Class Return

  6. 6

    Generics - method return type as extending class

  7. 7

    Private class as return type from public method

  8. 8

    Infer class generic property method return type

  9. 9

    How to specify the return type of the method of an abstract class

  10. 10

    How to specify the return type of the method of an abstract class

  11. 11

    How to check template class method return type?

  12. 12

    What is method return type of class InfoChyb

  13. 13

    How to create a next method for a class when the next method is supposed to return the values of two generators?

  14. 14

    Java language spec: "an invocation of Class" as return type of method in annotation type

  15. 15

    Can a Base Class Method return the type of the derived class?

  16. 16

    Return type of child class from parent class method

  17. 17

    Using "next" in an "iterator" and getting type mismatch error

  18. 18

    How can I return a dependent type from templated class method?

  19. 19

    C++ method inheritance with return type depending on class

  20. 20

    Make a interface method return an object of type of the class which implemented it

  21. 21

    Private class member vs. Method with return type

  22. 22

    Unable to create a method of return type Object in a child class

  23. 23

    C++ Return a Type of object in a method of the same class

  24. 24

    Method in generic class does not return type specified. Why?

  25. 25

    Typescript parent class method return type change by child property

  26. 26

    Make a interface method return an object of type of the class which implemented it

  27. 27

    override the return type of method declared in the interface after its implemented in class

  28. 28

    Single method to return an instance of different generic type of a class

  29. 29

    What is the return type for JPA query method when query result is not a class?

HotTag

Archive