mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-11 08:08:01 +08:00
解决FilterIter 类中构造器filter 传入null 无法进行迭代器迭代
This commit is contained in:
parent
ef0fc738f1
commit
030a27a0df
@ -84,7 +84,7 @@ public class FilterIter<E> implements Iterator<E> {
|
||||
private boolean setNextObject() {
|
||||
while (iterator.hasNext()) {
|
||||
final E object = iterator.next();
|
||||
if (null != filter && filter.accept(object)) {
|
||||
if (null == filter || filter.accept(object)) {
|
||||
nextObject = object;
|
||||
nextObjectSet = true;
|
||||
return true;
|
||||
|
@ -0,0 +1,31 @@
|
||||
package cn.hutool.core.collection;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* {@link FilterIter} 单元测试
|
||||
* @author chao.wang
|
||||
*/
|
||||
public class FilterIterTest {
|
||||
|
||||
@Test
|
||||
public void checkFilterIter() {
|
||||
Iterator<String> it = ListUtil.of("1", "2").iterator();
|
||||
// filter 为null
|
||||
FilterIter<String> filterIter = new FilterIter<>(it, null);
|
||||
while (filterIter.hasNext()) {
|
||||
System.out.println(filterIter.next());
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
it = ListUtil.of("1", "2").iterator();
|
||||
// filter 不为空
|
||||
filterIter = new FilterIter<>(it, (key) -> key.equals("1"));
|
||||
while (filterIter.hasNext()) {
|
||||
System.out.println(filterIter.next());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user