LinkedHashMap实现分组排序(保证有序性),并实现map的一个key值对应多个value

xiaoxiao2021-02-28  99

final Map<Long,Map<String,Boolean>> questionMap = new LinkedHashMap<>(); Map<String ,List<LMSExamPaperQuestionReference>> map = new LinkedHashMap<>(); List<LMSExamPaperQuestionReference> examPaperQuestionReferences = (List<LMSExamPaperQuestionReference>)examPaperQuestionReferenceCriteria.list(); for(int i =0;i<examPaperQuestionReferences.size();i++){ String groupName = examPaperQuestionReferences.get(i).getExamQuestion().getQuestionGroup(); if (!map.containsKey(groupName)){ map.put(examPaperQuestionReferences.get(i).getExamQuestion().getQuestionGroup(), new LinkedList()); } map.get(groupName).add(examPaperQuestionReferences.get(i)); //实现按groupName分组,即一个key对应多个value } for(Map.Entry<String,List<LMSExamPaperQuestionReference>> entry : map.entrySet()){ //遍历map得到value List<LMSExamPaperQuestionReference> list2 = entry.getValue(); int index = 0 ; for (LMSExamPaperQuestionReference item : list2) { Map<String,Boolean> questionMapItem = new LinkedHashMap<String, Boolean>(); questionMapItem.put("up",true); questionMapItem.put("down",true); questionMap.put(item.getExamQuestion().getId(),questionMapItem); if(index == 0){ questionMap.get(item.getExamQuestion().getId()).put("up",false); } if(index == list2.size() - 1){ questionMap.get(item.getExamQuestion().getId()).put("down",false); } index ++; } }
转载请注明原文地址: https://www.6miu.com/read-69554.html

最新回复(0)