使用字符串和列表作为参数迭代哈希映射

时间:2014-03-14 03:37:19

标签: java hashmap treemap

我有一个场景,我必须遍历HashMap以检查空值并生成一个空bean。空bean将再次添加到新地图。

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);
                         }
            }
        }

布尔值总是为真,即使它不满足内循环条件。

有人能建议一个更好的解决方案来实现所需的输出吗?

提前致谢

3 个答案:

答案 0 :(得分:1)

您有2个名称相同的变量value。一个是String另一个布尔值。我想这种含糊不清使我们,你自己和编译器感到困惑。实际上你的代码甚至不应该编译。

答案 1 :(得分:1)

主要问题是您的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) {
        //...
    }
}

答案 2 :(得分:0)

有一次,您发现日期匹配,然后突破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);
                     }
        }
     }