小编Sha*_*aee的帖子

在 java-8 lambda 表达式中使用 if-else 语句

我在 java-7 中有一个 for 语句,它工作正常:

Character cha = new Character(',');
String ncourseIds = null;
String pastCourseIds = null;
for (EquivalentCourse equivalentCourse : equivalentCourses) {
  if(equivalentCourse.getNcourse() != null){
    ncourseIds += equivalentCourse.getNcourse().getId()+ ",";   
  } else if(equivalentCourse.getPastCourse() != null) {
    pastCourseIds +=equivalentCourse.getPastCourse().getId()+","; 
  }
}
if(!ncourseIds.isEmpty() &&cha.equals(ncourseIds.charAt(ncourseIds.length()-1))) {
  ncourseIds = ncourseIds.substring(0, ncourseIds.length()-1);
}
if(!pastCourseIds.isEmpty()&& cha.equals(pastCourseIds.charAt(pastCourseIds.length()-1))) {
  pastCourseIds = pastCourseIds.substring(0,pastCourseIds.length()-1);
}
Run Code Online (Sandbox Code Playgroud)

现在我想将我的代码转换为Stream&collect在 java-8 中,我实现了一半关于 filter not null 的业务Ncourse

equivalentCourses.stream().filter(obj -> obj.getNcourse() != null )
                 .map(obj -> obj.getNcourse().getId()).collect(Collectors.joining(",")); 
Run Code Online (Sandbox Code Playgroud)

但我不知道实施它的else-statement …

java lambda if-statement java-8

5
推荐指数
1
解决办法
5万
查看次数

标签 统计

if-statement ×1

java ×1

java-8 ×1

lambda ×1