简体   繁体   English

对于具有唯一可寻址索引的元素集合,Java通用的正确方法是什么?

[英]What's the right Java generic for a collection of elements with unique addressable indices?

I'm on my way to programming a database application and in our course we are told to implement a library of elements using one of the Java Collections. 我正在对数据库应用程序进行编程,在我们的课程中,我们被告知要使用其中一个Java集合来实现元素库。 Each of the elements has a unique ID with which it's supposed to be addressed. 每个元素都有一个唯一的ID,应该使用该ID进行寻址。 Now I am wondering how this can be done. 现在我想知道如何做到这一点。

I though about using a ListArray but this won't work because the only way of addressing List elements is through the index which you can't control. 我虽然要使用ListArray,但是这行不通,因为寻址List元素的唯一方法是通过无法控制的索引。

Do you have some advice for me? 你对我有什么建议吗?

As the comments have said, you want to use a Map , of which HashMap is an implementation. 正如评论所说,您想使用Map ,其中HashMap是其实现。

Map<String, String> library = new HashMap<String, String>();

library.put("key", "value");

String value = library.get("key");

Map<IndexType, YourEntityType> . Map<IndexType, YourEntityType> Map is an interface; Map是一个接口; the most commonly used implementation is HashMap . 最常用的实现是HashMap

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

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