开发者

How to add more than one Objects of same type with same Key inside a HashMap

Hi I think this is very simple problem but I am not able to get through it right now; There are two kinds of Objects-RuleObject,TaskObject Following are definitions of RuleObject,TaskObject

RuleObject
  ruleID,RulePatternType,RulePrint

TaskObject
  taskID,taskName,Org,ruleID

ruleArrayList is all objects of RuleObjects

taskArrayList is all objects of TaskObjects

The final formation will be to fetch all RuleObjects used by TaskObjects and arrange by RuleObjects like example shown below:


RuleObject.RulePatternType1

 TaskName1  TaskOrg1    RUleObject.rulePrint1
 TaskName2  Task开发者_运维知识库Org2    RuleObject.rulePrint1

RuleObject.RulePatternType2

 TaskName3  TaskOrg1    RUleObject.rulePrint2
 TaskName4  TaskOrg2    RuleObject.rulePrint2
 TaskName5  TaskOrg3    RUleObject.rulePrint2
 TaskName6  TaskOrg4    RuleObject.rulePrint2

Code snippet:

List<TaskObject> taskArrayList = compModIF.getRecurringTasksForOrgsAndEffDate(allOrgIds, effDate);        
        List<RuleObject> ruleArrayList = compModIF.getComplianceTaskRecurrenceRules();
        Map ruleTypes = new HashMap();
        Map groupTaskTypes = new HashMap();
        Map groupRecurRulesNames = new HashMap();
        Map masterMapOfallMaps = new HashMap();
        Map recurPrintMap = new HashMap();
        Map recurPatternTypeMap = new HashMap();
        List groupRecuringTaskTypesList = null;
        Map filterRules = new HashMap();
        List completedList = new ArrayList();
        for(Iterator iter = ruleArrayList .iterator(); iter.hasNext();)
        {
            RuleObject ruleBase = (RuleObject)iter.next();              
            ruleTypes.put(ruleBase.getRecurRuleID(),ruleBase);

        }

        if (recurringTaskList != null)
        {
            for (Iterator it = taskArrayList .iterator(); it.hasNext();)
            {               
                TaskObject aTaskDef = (TaskObject)it.next();
                groupRecuringTaskTypesList = new ArrayList();
                if(ruleTypes.containsKey(aTaskDef.getTaskRecurRuleIDAsLong()))
                {
                    RuleObject ruleBase = (RuleObject)ruleTypes.get(aTaskDef.getTaskRecurRuleIDAsLong());                   
                    groupRecuringTaskTypesList.add(aTaskDef);                   
                    groupTaskTypes.put(ruleBase.getRecurRuleID(),groupRecuringTaskTypesList);
                    groupRecurRulesNames.put(ruleBase.getRecurRuleID(), ruleBase.getRecurRuleName());
                    if(ruleBase.getRecurPatternType()==ComplianceCodes.TASK_RECUR_PATTERN_TYPE_DAILY)
                    {
                        completedList= getDailyRecursCommentsAsCompleted(ruleBase.printRule());
                        recurPrintMap.put(ruleBase.getRecurRuleID(), completedList);
                    }
                    //groupRecuringTaskTypes = new ArrayList();
                    recurPatternTypeMap.put(ruleBase.getRecurRuleID(), ruleBase.getRecurPatternType());

                }
            }
       }

The problem here is for 1 ruleID there are multiple arraylists because of which I am able to get the last added list. Can any one suggest better alternative for this.


I didn't really follow your example too closely, but it sounds like what you need is a multimap - a mapping from a single key to multiple values.

Guava provides an interface for this and various implementations, usually accessed via Multimaps.


You have to store a Collection for each key:

Map<KeyType, Collection<ValueType>> map = 
                       new HashMap<KeyType, Collection<ValueType>>()


You can attain it simply by using Object as the value. and define the Map as shown below.

Map map = new HashMap()

Then when u set an ordinary object without multiple value set for the key and value as any valueObject map.put("key", valueObject)

when you want to add more than one, you can check for exiting key map.containsKey("key") and then add it to an arraylist and add remaining values into this list map.put("key", valueObjectList).

Note: just make sure that when u retrive it next time, do a instanceof check before you access the value as an object or list.
Alternate soln: store value as a list of object. it is simpler to code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜