predicate.test的返回结果不能为null;

This commit is contained in:
emptypoint 2022-09-18 12:49:01 +08:00
parent 705a15c3e5
commit aba35cc6c2
2 changed files with 4 additions and 7 deletions

View File

@ -313,11 +313,8 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
final Predicate<T> parentPredicate) {
Objects.requireNonNull(parentPredicate);
final List<T> list = toList();
final List<T> parents = EasyStream.of(list).filter(e ->
// 此处是为了适配 parentPredicate.test空指针 情况
// 因为Predicate.test的返回值是boolean所以如果 e -> null 这种返回null的情况会直接抛出NPE
Opt.ofTry(() -> parentPredicate.test(e)).filter(Boolean::booleanValue).isPresent())
.toList();
// 根节点列表
final List<T> parents = EasyStream.of(list).filter(parentPredicate).toList();
return getChildrenFromMapByPidAndSet(idGetter, childrenSetter, EasyStream.of(list).group(pIdGetter), parents);
}

View File

@ -476,7 +476,7 @@ public class EasyStreamTest {
Student.builder().id(8L).name("jobob").parentId(5L).build()
)
// just 4 lambda ,top by condition
.toTree(Student::getId, Student::getParentId, Student::setChildren, Student::getMatchParent);
.toTree(Student::getId, Student::getParentId, Student::setChildren, Student::isMatchParent);
Assert.assertEquals(asList(
Student.builder().id(1L).name("dromara").matchParent(true)
.children(asList(Student.builder().id(3L).name("hutool").parentId(1L)
@ -540,7 +540,7 @@ public class EasyStreamTest {
private Long id;
private Long parentId;
private List<Student> children;
private Boolean matchParent = false;
private boolean matchParent;
@Tolerate
public Student() {