2014-03-14 36 views
0

我有一个场景,我必须通过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

初始化'FALSE'值的变量在第一'for'环... –

+0

不相关的问题,但_please_不写'String value = entry.getKey();'。一个条目有一个键和一个值。我们不要将他们的名字换掉。 –

+0

@ Luiggi你能帮我解释一下如何确定我的病例的循环索引。感谢您的意见。 – jaggs

回答

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

感谢Luiggi.I尝试了同样的事情。但错误地放置了valueExists部分的声明。如果我声明下一个外部for循环,iam会得到一个空指针异常。不知道它为什么会发生。 – jaggs

+0

@jegadees我不'理解你的评论。也许如果你编辑你的问题并添加(不要删除任何东西)你的新代码与堆栈跟踪我可以找到问题。 –

+0

当valueexists为false时,我在循环中出现错误。生成并添加空bean的部分。 – jaggs

1

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

+0

对不起,我的错误,而复制代码..现在改..感谢 – jaggs

+0

这修复了任何错字,但不是主要问题。 –

+0

不应该对此作出评论吗? – drewmoore

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

我得到一个空指针异常里面的matchcourseDate等于false loop..When豆被添加到该列表中,发生错误.. – jaggs