1

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.

harqs
  • 177
  • 1
  • 11
  • Find answer on this link: http://stackoverflow.com/questions/1062960/map-implementation-with-duplicate-keys – rizzz86 May 16 '12 at 09:29

2 Answers2

1

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)

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
0

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);
  • ,in my case the value for hashmap is vector ,i have to just add the if loop? – harqs May 16 '12 at 09:50
  • how can i modify for vector,please let me know,as i find your solution perfect – harqs May 16 '12 at 09:51
  • well you just replace the set with a vector that should do it ... since vector has the same method add the rest should work ... for creating the list you would need a Vector too. –  May 16 '12 at 10:00
  • var.get(key).add(value); does not works,because u cannot find add after var.get(key) – harqs May 16 '12 at 10:46
  • it would be better to use addElement() i'll correct quickly. Like this you will increase the Vector by one too. –  May 16 '12 at 11:39
  • but this should works syntactically (Java 6,7) what version of java are you using? –  May 16 '12 at 11:41