Map inside map in java

What is wrong with this instance:

Map<String, String, HashMap<String,String>> map = new HashMap<String, String, HashMap<String,String>>(); 
+4
source share
4 answers

A Map<K,V> is a mapping from keys of type K to values ​​of type V There are only 2 types for a card.

You tried to define a map with three type parameters; this is impossible, and has nothing to do with the fact that you put a Map inside a Map .

A Map<K1,Map<K2,V2>> works fine.

A Map<X,Y,Z> does not work.

Perhaps you need something like Map< Pair<L,R>, Map<K,V> > . Java does not have a generic type Pair<L,R> , but see Related Questions below for solutions.

Related Questions

In pairs / sets:

On nested maps:

+21
source

Cards have only 2 type parameters, you have 3 (on your "external" card).

+5
source

The map interface (as well as the HashMap class) expects only 2 arguments of a general type: one for the key type and one for the value type. You provide 3 ...

+5
source

If you want, you can use something like that

 Map<Object,Map<String,String>> 

This object can be an object of a class containing two lines. Hope this solves your problem.

 Class Xyz { String s1; String s2; } 

The Xyz object can be used as a key in the map above.

+1
source

Source: https://habr.com/ru/post/1315725/


All Articles