简体   繁体   中英

Alternative to C# Dictionary<char, char[]> in Java

While rewriting C# class in Java, I would like to know most appropriate alternative to the following,

Dictionary<char, char[]> CharMappings = new Dictionary<char,char[]>();

I tried several options but none of them seems to be working well. In fact, I do not really understand the Map, HashMap concepts in Java yet. Could not find an abstract alternative to Dictionary so far.

Depending on how you want the collection to behave you'll have to differentiate between the different types of map, but generally a HashMap is what you want.

Map<Character, char[]> charMappings = new HashMap<>();

Note that you have to use the reference types instead of primitives.

Sample usage:

Map<Character, char[]> charMappings = new HashMap<>();

charMappings.put((char) 5, "lola".toCharArray());
System.out.println(charMappings.get((char) 5));

Output:

lola

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