Using two (or more) objects as a HashMap key

2019-01-22 04:04发布

I want to store certain objects in a HashMap. The problem is, usually you just use a single object as a key. (You can, for example, use a String.) What I want to do it to use multiple object. For example, a Class and a String. Is there a simple and clean way to implement that?

标签: java hash map
8条回答
再贱就再见
2楼-- · 2019-01-22 04:50

Do you mean that the object will be keyed by two keys, or rather a key which consists of two things.

If you want the first case. That is, an objected keyed by two keys, say a class or an object, you need to use two maps.

Map<Key1, value>

Map<Key2, value>

In the second case you need a map of maps, so:

Map<Key1, Map<Key2, value>>
查看更多
倾城 Initia
3楼-- · 2019-01-22 04:51

I tend to use a list

map.put(Arrays.asList(keyClass, keyString), value)
查看更多
登录 后发表回答