嗨,我认为这是一个非常简单的问题,但我现在无法解决这个问题;
有两种对象 - RuleObject,TaskObject
以下是RuleObject,TaskObject
RuleObject
ruleID,RulePatternType,RulePrint
TaskObject
taskID,taskName,Org,ruleID
ruleArrayList
是RuleObjects
taskArrayList
是TaskObjects
最终形成将获取RuleObjects
使用的所有TaskObjects
并按RuleObjects
排列
如下例所示:
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
代码段:
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());
}
}
}
这里的问题是1 ruleID有多个arraylists因为我能够获得最后添加的列表。 任何人都可以提出更好的选择。
答案 0 :(得分:3)
答案 1 :(得分:2)
您必须为每个密钥存储一个Collection:
Map<KeyType, Collection<ValueType>> map =
new HashMap<KeyType, Collection<ValueType>>()
答案 2 :(得分:0)
只需使用Object作为值即可实现。并定义Map,如下所示。
Map map = new HashMap()
然后当你设置普通对象没有多个值时
为任何valueObject设置键和值
map.put("key", valueObject)
如果要添加多个,可以检查退出键map.containsKey("key")
,然后将其添加到arraylist并将剩余值添加到此列表map.put("key", valueObjectList)
。
注意:只需确保在下次检索时,在将值作为对象或列表访问之前进行instanceof
检查。
Alternate soln :将值存储为对象列表。编码更简单。