How to get HashMap value with 3 values
If I have a HashMap with a key like this:
[pubDate, title, link]
and a value like this (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 get a 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);
}
source to share
You create an object that contains all three key subsections as attributes. Be sure to follow equals
and hashCode
.
public class MyKey {
public MyKey(String subkey1, String subkey2, String subkey3) {
...
}
}
Use an object like this as the key for your card:
Map<MyKey, String> myMap = ....;
myMap.get(new MyKey("Thu", "03 Jan 2013 21:50:02 +0100", "Transferts - YBX"));
Good luck!
source to share
I just ran into the same problem and decided to map my key to Entry . This allows you to use the same indexing functionality as provided by the map by having more than one property associated with a key. I think this is a much faster solution, 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 to display named functions. By selecting a function by name, the function will run and a boolean value will determine whether processing is needed during processing.
source to share
You can also try to use a nested hash file, such
HashMap<String, HashMap<String, HashMap<String, String>>>
as where you then formulate your query as
map.get(date).get(title).get(link)
A better solution would be to encapsulate this nested hash file in your own class as such:
public class NestedHashmap {
private Map<String, Map<String, Map<String, String>>> map;
public NestedHashMap() {
map = new HashMap<String, HashMap<String, HashMap<String, String>>>;
}
public put(String date, String title, String link, String value){
if(map.get(date) == null) {
map.put(date, new HashMap<String, HashMap<String, String>>;
}
if(map.get(date).get(title) == null){
map.get(date).put(new HashMap<String, String>);
}
map.get(date).get(title).put(link, value);
}
public get(String date, String title, String link) {
// ...mostly analogous to put...
}
}
source to share
You can create a composite key that contains all the desired values in the key. An easy way to do this would be to concatenate together the values that make up your key, limited to a value that you are sure will not appear in the key values.
eg:.
String makeCompoundKey(String pubDate, String title) {
return pubDate + "|" + title;
}
HashMap<String,String> map = new HashMap<String,String>();
map.put(makeComoundKey(pubDate,title), link)
To avoid the problem of choosing a character that does not appear in any of the key values for handling non-String keys, and probably to improve performance, you can declare a new class containing your key values and override the methods equals
and hashCode
:
final class DateAndTitle {
private final Date pubDate;
private final String title;
@Overrde
boolean equals(Object rhs) {
// let eclipse generate this for you, but it will probably look like ...
return rhs != null && rhs.getClass() == getClass() &&
pubDate.equals(rhs.pubDate) && title.equals(rhs.title);
}
@Overrde
int hashCode(Object) {
// let eclipse generate this for you...
...
}
}
Then you can define your card like this:
HashMap
and use DateAndTitle object to index your map.
source to share