Question

i have a scenario where i had to loop through a HashMap to check for null values and generate a empty bean .The empty bean will again be added to a new map.

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

The boolean value is always true even if it doesn't satisfy the inner loop condition.

Can anyone suggest a better solution to achieve the desired output?

Thanks in advance

Was it helpful?

Solution

The main problem is that your valueExists variable is initialized before the for loop where is needed for every validation. Rewrite it as this:

//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) {
        //...
    }
}

OTHER TIPS

You have 2 variables with the same name value. One is String the other boolean. I guess this ambiguity confuses us, yourself and the compiler. In fact your code should not even compile.

Once, you find that dates are matching, then break out of the for loop. Because, you are iterating over the list, so your boolean variable will be false only if the last entry in the bean list is false.

See the code review, with possible working solution

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);
                     }
        }
     }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top