0

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  TaskOrg2    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.

2
  • 3
    Your code would be a lot clearer to start with if you would avoid using raw types for all your variables (ruleTypes etc). Commented Sep 13, 2011 at 12:19
  • 1
    You should use generic maps if at all possible. How about having a map whose key is a rule or rule id, and whose value is a List of List of tasks. In other words, a List<List<TaskObject>> Commented Sep 13, 2011 at 12:23

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

wow!Is this free to use ?any licences information will be helpful
@GustlyWind: From the first linked page: "Code license: Apache License 2.0".
2

You have to store a Collection for each key:

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.