Reputation: 31
I know how to utilize intermediate and terminal function with streams, and many other basic things. but the task I have been given is confusing for me to implement. I have been struggling for last 36 hours non-stop. my assignment is as follow:
Implement one static method in a functional way, that takes a
List<T>
, filters it, orders it, returns only a certain amount of values and skips a certain amount of values and applies a mapping function to it. The returnvalue shall be aList<R>
. T and R are unspecified generic types. All needed values shall be parameters. The limit and offset shall be optional. Test your method using JUnit.
so far I could only manage to write the code and I am stuck with first step filter. confusion is, if I don't know type of list how can I filter it.
public class GeneralMethods<T,R> {
private List<T> recList;
private List<R> retList;
public void manipulateList(){
retList = recList.stream().filter(s -> s.)
}
}
This is my assignment I have struggled alot, I would appreciate your help.
Upvotes: 3
Views: 298
Reputation: 5606
Your one liner should look something like
public class GeneralMethods<T,R> {
public List<R> manipulateList(List<T> recList,
Predicate<T> filterPredicate,
Comparator<T> comparatorT,
Function<T,R> mapTtoR,
long ... limitAndOffset){
return recList.stream()
.filter(filterPredicate)
.sorted(comparatorT)
.skip(limitAndOffset.length > 1 ? limitAndOffset[1] : 0)
.limit(limitAndOffset.length > 0 ? limitAndOffset[0] : Long.MAX_VALUE)
.map(mapTtoR)
.collect(toList());
}
}
Then on the JUnit you must provide implementations of filterPredicate, comparatorT, and mapTtoR parameters.
Upvotes: 2