你如何在 Java 中创建字典? [英] How do you create a dictionary in Java?
问题描述
我正在尝试实现一本字典(如在实体书中).我有一个单词列表及其含义.
Java 提供什么数据结构/类型来将单词列表及其含义存储为键/值对.
给定一个键,我如何找到并返回值?
你需要一个 Map
.实现 Map
<的类/a> 接口包括(但不限于):
每个都是针对特定情况设计/优化的(有关更多信息,请参阅各自的文档).HashMap
可能是最常见的;首选默认值.
例如(使用HashMap
):
Mapmap = new HashMap();map.put("狗", "动物类型");System.out.println(map.get("dog"));
<前>动物类型
I am trying to implement a dictionary (as in the physical book). I have a list of words and their meanings.
What data structure / type does Java provide to store a list of words and their meanings as key/value pairs.
How, given a key, can I find and return the value?
You'll want a Map<String, String>
. Classes that implement the Map
interface include (but are not limited to):
Each is designed/optimized for certain situations (go to their respective docs for more info). HashMap
is probably the most common; the go-to default.
For example (using a HashMap
):
Map<String, String> map = new HashMap<String, String>();
map.put("dog", "type of animal");
System.out.println(map.get("dog"));
type of animal
这篇关于你如何在 Java 中创建字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!