Skip to content

Commit 247719d

Browse files
committed
filter method implementation
1 parent 4ad71a0 commit 247719d

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/Lists/ArrayListCustom.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.*;
44
import java.util.function.Consumer;
5+
import java.util.function.Function;
6+
import java.util.function.Predicate;
57

68
public class ArrayListCustom<T> implements Cloneable {
79

@@ -120,6 +122,16 @@ public void forEach(Consumer<T> o) {
120122
}
121123
}
122124

125+
public ArrayListCustom<T> filter(Predicate<T> o) {
126+
ArrayListCustom<T> newList = new ArrayListCustom<>();
127+
this.forEach((item) -> {
128+
if (o.test(item)) {
129+
newList.add(item);
130+
}
131+
});
132+
return newList;
133+
}
134+
123135
public Object[] toArray() {
124136
Object[] ret = new Object[size];
125137
for (int i = 0; i < size; i++) {

0 commit comments

Comments
 (0)