简体   繁体   中英

Java Generics: Useing multiple generic types in one collection type

I would like to use two generic types as a key for a hash map. Effectively:

Map<CollectionType<Integer, Integer>, Character> map = new HashMap<>();

I am trying to find if this is possible, and if so which collection type I could use. The right type needs to be able to accept duplicate values. ie <1, 0>, <1, 1>, <2, 0> could all be used as a key in the map

for additional background the key in the map will be coordinates on a hex grid, and the value is what is currently stored at that location.

Use a Pair<Integer, Integer> , provided by many libraries like in org.apache.commons.lang3.tuple.Pair or in jdk as javafx.util.Pair .

equals() and hashcode() are overridden, so it can work as a key in the Map.

Map<Pair<Integer, Integer>, Character> map = new HashMap<>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM