如何获得具有三个值的HashMap值 [英] How to get a HashMap value with three values
问题描述
如果我有一个带有这样的键的HashMap:
[pubDate,title,link]
和这样的值(例子):
[Thu,2013年1月3日21:50:02 +0100,Transferts - YBX:''改变尺寸',http:// www我可以检索 http://www.link.fr/info/link_informations.html
?
代码:
pre $ for(int i = 0; i< nl.getLength(); i ++){
//创建新的HashMap
map = new HashMap< String,String>();
Element e =(Element)nl.item(i);
//将每个子节点添加到HashMap key =>值
map.put(KEY_TITLE,parser.getValue(e,KEY_TITLE));
map.put(KEY_LINK,parser.getValue(e,KEY_LINK));
map.put(KEY_DATE,parser.getValue(e,KEY_DATE));
//map.put(KEY_DESC,parser.getValue(e,KEY_DESC));
//将HashList添加到ArrayList
menuItems.add(map);
}
我刚遇到同样的问题并决定将我的密钥映射到条目。这允许地图提供相同的索引功能,同时具有与键关联的多个属性。
地图< String,Entry< Action,Boolean>我们认为这是一个更加简洁的解决方案,可以创建单独的类或嵌套地图。 > actionMap = new HashMap< String,Entry< Action,Boolean>>();
actionMap.put(action_name,new SimpleEntry(action,true));
稍后使用数据:
条目< Action,Boolean> actionMapEntry = actionMap.get(action_name);
if(actionMapEntry.value())actionMapEntry.key()。applyAction();
我个人使用这个是一个命名函数的映射。通过名称选择函数后,该函数将运行,布尔值将确定是否需要在处理中进行清理。
If I have a HashMap with a such key:
[pubDate, title, link]
and such value(example):
[Thu, 03 Jan 2013 21:50:02 +0100, Transferts - YBX : ''Je change de dimension'', http://www.link.fr/info/link_informations.html]
Can I retrieve the link http://www.link.fr/info/link_informations.html
?
Code:
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
//map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
// adding HashList to ArrayList
menuItems.add(map);
}
I just faced the same issue and decided to map my key to an Entry. This allows for the same indexing features provided by a map while having more than one property associated with a key. I think it's a much neater solution that creating a separate class or nesting maps.
Map<String, Entry<Action, Boolean>> actionMap = new HashMap<String, Entry<Action, Boolean>>();
actionMap.put("action_name", new SimpleEntry(action, true));
To use the data later:
Entry<Action, Boolean> actionMapEntry = actionMap.get("action_name");
if(actionMapEntry.value()) actionMapEntry.key().applyAction();
My personal use of this was a map of named functions. Having selected the function by name, the function would be run and the boolean would determine whether or not cleanup was needed in processing.
这篇关于如何获得具有三个值的HashMap值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!