简体   繁体   中英

Optimize the code to get blackboard user or course by its unique run id

I am fetching a course object by its course run id. Below is my code that works perfectly ok.

public static CourseVO getBlackboardCourseObjectByCourseRunID(String RunID){
        CourseVO[] courseVOList = null;
        try {
            courseVOList = BlackboardCoursesDAO.getAllBlackBoardCourse();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            throw new RuntimeException("Not able to fetch blackboard courses");
        } 
        CourseVO courseVO = new CourseVO();

        int length=courseVOList.length;
        System.out.println("length : "+length);
        int i=0;
        courseVO = courseVOList[i];
        while(!courseVO.getId().equals(RunID) && i<length){
            courseVO = courseVOList[i];
            //System.out.println("in while loop ");
            i++;
        }
        return courseVO;
    }

But it troubles when I try to fetch this object in a loop. If anyone can provide some better code, it will be great help.

Thanks.

After line 8, you can try to change lines with this>

Stream<CourseVO> courseList = Arrays.stream(courseVOList);

CourseVO foundCourse = courseList.filter(s -> s.getId().equals(RunID) )
  .findFirst().get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM