mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +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() {
|
private boolean setNextObject() {
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
final E object = iterator.next();
|
final E object = iterator.next();
|
||||||
if (null != filter && filter.accept(object)) {
|
if (null == filter || filter.accept(object)) {
|
||||||
nextObject = object;
|
nextObject = object;
|
||||||
nextObjectSet = true;
|
nextObjectSet = true;
|
||||||
return 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…
x
Reference in New Issue
Block a user