Search code examples
javahashmapbounded-wildcardbounded-types

Java Bounded Type Parameters in HashMap


I am just learning how to use wildcards and bounded type parameters. I want to use (I think) bounded wildcards in a method that is passed a HashMap. I've seen examples of bounded type parameters and bounded wildcards but I haven't found anything that shows me how to pass a HashMap into a method where the HashMap can contain different value objects.

Map<Integer, Company>
Map<Integer, Employee>
Map<Integer, Location>

This is what I have as an example for the first Map listed above:

public Map<Integer, Company> readXML(Map<Integer, Company> companies) {

I want to use something like the following to enable this method to deal with any one of my Maps listed above.

public Map<Integer, ?> readXML(Map<Integer, ?> values) {

Can somebody show me an example for how I can use wildcards for the Map values in this method?

1) Do I need to create a Map class that extends the value objects (Company, Employee, Location)?

2) Or is there a better way to accomplish this? In other words, am I doing it wrong?

Thank you for your advice.


Solution

  • Since your method readXML(Map<Integer, ?(V)> companies) can accept V of type Company, Employee and Location. You can solve this in below mentioned ways.

    1. You can make all the mentioned classes to extend new class or can be made to implement our custom Type interface and use that type for V or

    2. Add additional parameter to your method which sends Class information to it, while calling the api.

      public <V> Map<Integer, V> readXML(Map<Integer, V> values, Class<V> clazz) {
          if(clazz == Integer.class) {
              ...
          } else if// or if all the class type has same implementation use the 
                   // || operator in the above if condition only. 
                  ...
          //and finally
          else {
              // either throw **IllegalArgument/Unsupported operation** exception
              // for the type 'V' Or handle in any other way you like to implement
          }
      }
      

      Lastly, IllegalArgument / UnsupportedOperation exception is unchecked exception. Make sure you properly document your method, if you decide to throw the exception.