爪哇 - 使得与键/值对的对象? [英] Java - making objects with key/value pairs?
问题描述
我想作以下类型的对象。这是我的,我想在Java中找出更高级别的愿望:
的ListObject(键,字符串):每一个按键对应一个字符串值;关键是一个字符串本身
的ListObject(键,的String []):每个键对应一个字符串数组;关键是一个字符串本身
的ListObject(键,字符串,字符串[]):同样的交易,但每个密钥的两个值字段
我怎么会做这种类型的(和使用!)对象?
感谢。
您似乎需要一些的 地图
真是让人不是列表
秒。检查为Map实现的Javadoc ;最常见的就是的HashMap
,但也有分类,并发,确定性迭代实现等提供了。
的ListObject:每一个按键对应一个字符串值;关键是一个字符串本身
块引用>地图<字符串,字符串>
的ListObject:每个键对应一个字符串数组;关键是一个字符串本身
块引用>地图<字符串,字符串[]>
(或preferably
地图<字符串列表<串GT;>
)
的ListObject:同样的交易,但每个密钥的两个值字段
块引用>地图<弦乐,UserDefinedClassWithTwoFields>
I want to make the following types of objects. This is my higher-level desire that I'd like to figure out in Java:
ListObject(key, String): every key corresponds to a String value; key is a string itself
ListObject(key, String[]): every key corresponds to an array of Strings; key is a string itself
ListObject(key, String, String[]): same deal but with two value fields per key.
How would I make (and use!) objects of this type?
Thanks.
解决方案You seem to need some
Map
s rather thanList
s. Check the Javadoc for Map implementations; the most common isHashMap
, but there are sorted, concurrent, deterministically iterable implementations etc. available too.ListObject: every key corresponds to a String value; key is a string itself
Map<String, String>
ListObject: every key corresponds to an array of Strings; key is a string itself
Map<String, String[]>
(or preferably
Map<String, List<String>>
)ListObject: same deal but with two value fields per key.
Map<String, UserDefinedClassWithTwoFields>
这篇关于爪哇 - 使得与键/值对的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!