Reputation: 179
The data in hashtable is getting overridden for the same key.I am trying to add 'n' number of data against a same key at different intervals,the data added to hashtable peviously is getting overridden,how to solve this issue?
if (value == RepeatRule.DAILY) {
setHashRepeatData(repDates, eventBean,
listRepeatEvents);
}
if (value == RepeatRule.WEEKLY) {
setHashRepeatData(repDates, eventBean,
listWeekEvents);
}
private void setHashRepeatData(Vector repDates, EventData eventBean,
Vector listOfRepeatData) {
if (repDates != null) {
System.out.println("the size of repDates is :" + repDates.size());
System.out.println("summ" + eventBean.getSummary());
listOfRepeatData.addElement(eventBean);
for (int i = 0; i < repDates.size(); i++) {
String currentRepDate = (String) repDates.elementAt(i);
System.out.println("currentRepDate" + currentRepDate);
listUserEvents.put(currentRepDate, listOfRepeatData);
}
}
}
I am calling the above method at different intervals and trying to set the data for same key.I am not getting how to solve the issue.
Upvotes: 1
Views: 806
Reputation:
This would be a sample implementation of what you want to achieve, if you do it yourself:
// I took a set because I wanted have the inputs sorted
HashMap<String, Set<String>> var = new HashMap<String, Set<String>>();
String key= "key";
String value = "value";
if(var.containsKey(key)){
// the set is already there we can proceed to add the value
} else {
//first time you have to create the List
var.put(key, new TreeSet<String>());
}
var.get(key).add(value);
You will have to modify it accordingly to your case like:
HashMap<String, Vector<String>> var = new HashMap<String, Vector<String>>();
String key= "key";
String value = "value";
if(var.containsKey(key)){
// the set is already there we can proceed to add the value
} else {
//first time you have to create the List
var.put(key, new Vector<String>());
}
var.get(key).addElement(value);
Upvotes: 0
Reputation: 47637
You are looking for a Multi-value map (for the same key, you can have more than one value).
Either you implement this yourself (by changing your Map<K,V>
to Map<K,List<V>>
), but it is a bit painful to writer.
Or use Guava which offers that feature: Multimaps (I would recommend this approach)
Upvotes: 1