0

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> {

}

3 Answers 3

1

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;
    }
 }
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

A Map can be used as key in another Map, at least from a technical perspective. For instance java.util.HashMap differentiates its keys by their hashcodes. Therefore anything that implements hashCode method correctly would work. But the question remains whether it is useful. There are cases where a simple 1:1 mapping is not what you want. In this situation a multivalued map might be the way to go,
0

I believe you've to do something like this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.