4

What is wrong with this instantiation :

Map<String, String, HashMap<String,String>> map = new HashMap<String, String, HashMap<String,String>>();
London
  • 14,986
  • 35
  • 106
  • 147
  • 3
    (You might want to consider declaring it with `Map` instead of `HashMap`: `Map> map = new HashMap>();`. Also you might want to introduce an object that represents a composite key so you only need one map.) – Tom Hawtin - tackline Jul 14 '10 at 21:57

4 Answers4

21

A Map<K,V> is a mapping from keys of type K to values of type V. There are only 2 type parameters to a map.

You attempted to define a map with 3 type parameters; this is not possible, and has nothing to do with the fact that you're putting a Map inside a Map.

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

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

It's possible that you need something like Map< Pair<L,R>, Map<K,V> >. Java does not have generic Pair<L,R> type, but see related questions below for solutions.

Related questions

On pairs/tuples:

On nested maps:

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • I'm trying to create method to create XML element, where element name and value are in one hashmap, and attributes and values of those attributes in other hashmap, do you think it is a good idea, or pass 4 arraylists to a method each containing only elem names, values, attributes, attribute values – London Jul 14 '10 at 22:06
  • 1
    but I guess the problem is that hashmap doesn't allow duplicates, where duplicates can be node name with different values or attributes – London Jul 14 '10 at 22:08
  • @London: to be precise, _any_ `Map` can only map one key to one value. The same value can be mapped by several keys, i.e. it's a many-to-one relation, not one-to-many, and not one-to-one. You can define a `Map>` to emulate mapping a key to several values (or better yet, use Guava's Multimap), but I'm still not sure what you're doing to actually give any advice. You may want to ask another question where you clearly specify what is it that you're doing. – polygenelubricants Jul 14 '10 at 22:10
  • 1
    actually there is a generic pair implementation, though it is not so popular. See SimpleEntry (http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/util/AbstractMap.SimpleEntry.html). My life changed since I discovered it :) – Eyal Schneider Jul 14 '10 at 22:48
  • @Eyal Schneider There are OOD arguments against a generic Pair class in Java. See this good post: http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java – Paul Jackson Oct 13 '11 at 02:27
  • 1
    @Paul Jackson: I agree with the OOD arguments. Actually the class I mentioned can be used as a generic pair, but it has a more specific semantics: key-value relationship. So I agree that it shouldn't be used unless this is the actual relationship. – Eyal Schneider Oct 16 '11 at 19:42
5

Maps only have 2 type parameters, you have 3 (in your "outer" Map).

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
5

Map interface (as well as HashMap class) expects only 2 generic type arguments: one for the key type and one for the value type. You provide 3...

Eyal Schneider
  • 22,166
  • 5
  • 47
  • 78
1

If you wish you can use something of this kind

Map<Object,Map<String,String>>

This object can be an object of a Class holding two Strings. Hope this solves your problem.

Class Xyz {
String s1;
String s2;
}

An object of Xyz can be used as key in the above map.

subbu
  • 524
  • 6
  • 16