爪哇 - 使得与键/值对的对象? [英] Java - making objects with key/value pairs?

查看:153
本文介绍了爪哇 - 使得与键/值对的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想作以下类型的对象。这是我的,我想在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 Maps rather than Lists. Check the Javadoc for Map implementations; the most common is HashMap, 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屋!

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