簡體   English   中英

使用字符串和列表作為參數迭代哈希映射

[英]Iterate through a Hash Map with string and list as parameters

我有一個場景,我必須循環通過一個HashMap來檢查空值並生成一個空豆。空豆將再次添加到一個新的地圖。

for (String course_date : shiftSet) {
        Bean courseBean = null; 
        boolean  valueExists = false;

                for (Entry<String, List<Bean>> entry: courseMap.entrySet()){

                    String studentDetail = entry.getKey();
                    String [] studentSet =  StringUtils.getArray(studentDetail , ",");
                    String studentId = studentSet[0];
                    String courseId = studentSet[1];

                    for(Bean resultBean : entry.getValue()){

                        if(course_date.equalsIgnoreCase(resultBean.getCourseDate()){
                            valueExists = true;
                        }
                }
                        if(!valueExists ) {
                            courseBean = new Bean();
                            courseBean.setStudent(studentId);
                            courseBean.setCourse(courseId);
                            List<Bean> courseList = entry.getValue();
                            courseList.add(courseBean);
                            outputMap.put(studentId+courseId, courseList);
                         }
            }
        }

即使布爾值不滿足內部循環條件,它也始終為true。

誰能建議更好的解決方案來實現所需的輸出?

提前致謝

您有2個具有相同名稱value變量。 一個是String另一個布爾值。 我想這種含糊不清使我們,你自己和編譯器感到困惑。 實際上你的代碼甚至不應該編譯。

主要問題是你的valueExists變量在for循環之前初始化,每次驗證都需要。 將其重寫為:

//declare it here (regardless this "initial" value)
boolean valueExists = false;
for (Entry<String, List<Bean>> entry: courseMap.entrySet()) {
    //initialize it here
    valueExists = false;
    //...
    for (Bean resultBean : entry.getValue()) {
        if(course_date.equalsIgnoreCase(resultBean.getCourseDate()){
            valueExists = true;
            //also, add a break here since you already found the value
            //you don't need to keep iterating through the rest of items
            break;
        }
    }
    if (valueExists) {
        //...
    }
}

有一次,你發現日期是匹配的,然后突破for循環。 因為,您正在迭代列表,所以只有當bean列表中的最后一個條目為false時,您的布爾變量才為false。

請參閱代碼審查,並提供可行的解決方案

for (String course_date : shiftSet) {
        Bean courseBean = null; 

            for (Entry<String, List<Bean>> entry: courseMap.entrySet()){

                // Declare this boolean variable inside, since you are dealing with a single entry in the map at a time.
                boolean matchCourseDate = false;

                //Declare variable with more meaningful naming conventions.
                String key = entry.getKey();
                String [] studentSet =  StringUtils.getArray(key, ",");
                String studentId = studentSet[0];
                String courseId = studentSet[1];

                // Since, you are iterating over the list, make sure once you found the date match break out if this loop
                for(Bean resultBean : entry.getValue()){
                    if(course_date.equalsIgnoreCase(resultBean.getCourseDate()){
                        matchCourseDate = true;
                    }

                    // If the match is found, break. Otherwise, keep going.
                    if(matchCourseDate) 
                        break;
                }

                // If no match is found, then create a new bean and put it into output map.
                    if(!matchCourseDate) {
                        courseBean = new Bean();
                        courseBean.setStudent(studentId);
                        courseBean.setCourse(courseId);
                        List<Bean> courseList = entry.getValue();
                        courseList.add(courseBean);
                        outputMap.put(studentId+courseId, courseList);
                     }
        }
     }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM