简体   繁体   English

替代C#字典 <char, char[]> 在Java中

[英]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, 在Java中重写C#类时,我想知道以下最合适的替代方法,

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. 实际上,我还没有真正理解Java中的Map,HashMap概念。 Could not find an abstract alternative to Dictionary so far. 到目前为止找不到Dictionary的抽象替代品。

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. 根据您希望集合的行为方式,您必须区分不同类型的地图,但通常HashMap就是您想要的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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