Indigo
Indigo

Reputation: 2997

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.

Upvotes: 1

Views: 2558

Answers (1)

Jeroen Vannevel
Jeroen Vannevel

Reputation: 44449

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

Upvotes: 3

Related Questions