hades
hades

Reputation: 4694

Java - Accept Map as parameter in class

Sorry, im not familiar with generics, i can create a generic class like the following:

public class InfoField <T, U> {

}

The class above can accept values like:

<String, User>
<String, String>
<Set<UserGroup>, User>

But can i have class that accept map object as value? something like

<Map<String, String>, String>

public class InfoFieldMap <Map<T,U>, K> {

}

Upvotes: 0

Views: 80

Answers (3)

Massimo Petrus
Massimo Petrus

Reputation: 1891

I believe you've to do something like this

  public class InfoFieldMap < Z extends Map<K,V>, K, V>

Upvotes: 0

Binyamin Regev
Binyamin Regev

Reputation: 1030

I am not sure that you can have a Map as the key of another Map as you declared in your InfoFieldMap class. see this simple example: map-in-java-with-example

Your declation of InfoFieldMap class should be:

public class InfoFieldMap <K, Map<K,V>> {

}

Where K is key field of the Map and V is the value of the Map. V can be a Map or another collection.

Upvotes: 1

Dana
Dana

Reputation: 310

Works as you original had it:

class InfoField <T, U> {
    public static void main( String[] args )
    {
        //Example InfoField declaration
        InfoField<Map<String,String>, String> test;
    }
 }

Upvotes: 1

Related Questions