Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

java - How to get a HashMap value with three values

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);
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...