Update
To add an alternative, here's what the code would look like to do the work with two filter() operations. Note, this has the impact of iterating over the entire collection a second time, which may have a performance impact if this is a large collection.
I also went ahead and simplified some of the logic in regards to the string joining. Correct me if I missed anything there.
final List<String> courseIdList = new ArrayList<>();
final List<String> pastCourseIdList = new ArrayList<>();
equivalentCourses.stream().filter((current) -> current.getNcourse() != null)
.forEach((current) -> courseIdList.add(current.getNcourse().getId()));
equivalentCourses.stream().filter((current) -> current.getNcourse() != null && current.getPastCourse() != null)
.forEach((current) -> pastCourseIdList.add(current.getPastCourse().getId()));
String ncourseIds = String.join(",", courseIdList);
String pastCourseIds = String.join(",", pastCourseIdList);
Original answer
For your use case, it may make the most sense to use the forEach() lambda. This will be the easiest way to do the translation.
java.lang.Character cha = new java.lang.Character(',');
final StringBuilder ncourseIdBuilder = new StringBuilder();
final StringBuilder pastCourseIdBuilder = new StringBuilder();
equivalentCourses.stream().forEach((equivalentCourse) -> {
if (equivalentCourse.getNcourse() != null) {
ncourseIdBuilder.append(equivalentCourse.getNcourse().getId()).append(",");
} else if (equivalentCourse.getPastCourse() != null) {
pastCourseIdBuilder.append(equivalentCourse.getPastCourse().getId()).append(",");
}
});
String ncourseIds = ncourseIdBuilder.toString();
String pastCourseIds = pastCourseIdBuilder.toString();
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);
}
You can rewrite the code using filter() expressions, but it'll require a bigger re-working of the logic in the conditionals, which introduces the risk you might break something if this isn't tested well. The logic changes are exactly what @Holger and @Ole V.V. reference in their comments to the original question.
Whether you use forEach() or the filters, lambdas cannot access non-final variables within the expression, hence why I added the final StringBuilder variable outside the scope of the loop.
Characterinstead ofchar? This makes your code harder to read and wastes resources. Though, you don’t need it at all, if you replace!ncourseIds.isEmpty() && ha.equals(ncourseIds.charAt(ncourseIds.length()-1))with a simplencourseIds.endsWith(",")and likewise,!pastCourseIds.isEmpty() && cha.equals(pastCourseIds.charAt(pastCourseIds.length()-1))withpastCourseIds.endsWith(","). To collect two strings with streams, you can simple perform two stream operations.getNcourse() != nulland one forgetNcourse() == null && getPastCourse() != null).