简体   繁体   中英

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. Each of the elements has a unique ID with which it's supposed to be addressed. 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.

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<String, String> library = new HashMap<String, String>();

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

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

Map<IndexType, YourEntityType> . Map is an interface; the most commonly used implementation is HashMap .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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