I have a little piece of code where i created my own java.util.stream realization.
I need to parametrize it using PECS rule. But either I didn't understand PECS rule well or my class designed bad - I don't know how to correctly implement it.
When I'm trying to implement it (? extends T) in filter() method realization, for example - I can't use foreach cycle.
Maybe you have some ideas? Thanks in advance.
public class Streams<T> {
private final List<T> list;
private List<T> resultList = new ArrayList<>();
private Streams(List<T> list) {
this.list = list;
}
public static <E> Streams<E> of(List<E> list) {
return new Streams<>(list);
}
public Streams<T> filter(Predicate<T> predicate) {
for (T elem : list) {
if (predicate.test(elem)) {
resultList.add(elem);
}
}
return this;
}
public Streams<T> transform(Function<? super T, ? extends T> function) {
for (T elem : resultList) {
resultList.set(resultList.indexOf(elem), function.apply(elem));
}
return this;
}
public <E, I> Map<E, I> toMap(Function<T, E> function1, Function<T, I> function2) {
HashMap<E, I> map = new HashMap<>();
for (T elem : resultList) {
map.put(function1.apply(elem), function2.apply(elem));
}
return map;
}
}