你如何在Java中创建一个字典? [英] How do you create a dictionary in Java?

查看:2422
本文介绍了你如何在Java中创建一个字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一本字典(如实体书)。我有一个单词列表及其含义。

I am trying to implement a dictionary (as in the physical book). I have a list of words and their meanings.

Java提供哪些数据结构/类型,用于将单词列表及其含义存储为键/值对。

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?

推荐答案

您将需要一个 Map< String,String> 。实施 地图的类界面包括(但不限于):

You'll want a Map<String, String>. Classes that implement the Map interface include (but are not limited to):

  • HashMap
  • LinkedHashMap
  • Hashtable

每个都是针对某些情况设计/优化的(请访问他们各自的文档以获取更多信息)。 HashMap 可能是最常见的;

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.

例如(使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆