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

查看:29
本文介绍了你如何在 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屋!

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