Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a HashMap value with three values

Tags:

java

hashmap

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);  } 
like image 435
androniennn Avatar asked Jan 03 '13 22:01

androniennn


People also ask

Can we store 3 values in HashMap?

HashMap can be used to store key-value pairs. But sometimes you may want to store multiple values for the same key.

Can a HashMap key have multiple values?

The Map interface stores the elements as key-value pairs. It does not allow duplicate keys but allows duplicate values. HashMap and LinkedHashMap classes are the widely used implementations of the Map interface. But the limitation of the Map interface is that multiple values cannot be stored against a single key.

Can Java Map have multiple values?

2. Standard Maps. Java has several implementations of the interface Map, each one with its own particularities. However, none of the existing Java core Map implementations allow a Map to handle multiple values for a single key.


2 Answers

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.

like image 144
ndm13 Avatar answered Sep 21 '22 15:09

ndm13


You create an object that holds all three subkeys of the key as attributes. Make sure to implement equals and hashCode properly.

public class MyKey {   public MyKey(String subkey1, String subkey2, String subkey3) {     ...   } } 

Use such object as a key of your map:

Map<MyKey, String> myMap = ....; myMap.get(new MyKey("Thu", "03 Jan 2013 21:50:02 +0100", "Transferts - YBX")); 

Have fun!

like image 40
SJuan76 Avatar answered Sep 21 '22 15:09

SJuan76