lambda - How to implement my own finisher in a collector? -
welcome people!
i wondering how implement own finisher not trival, identity function. header of collector is
public class sequentialppscollector<t> implements collector<pair<t, double>, list<pair<t, double>>, list<t>> {...} inside there finisher method should transform list<pair<t, double>> list<t>
@override public function<list<pair<t, double>>, list<t>> finisher() { return ... } this job
return list -> list .stream() .map(pair::getleft) .collect(collectors.tolist());
here's how can transform list<pair<t, double>> list<t>:
list<t> listoft = list.stream() .map(pair::getfirst) .collect(collectors.tolist()); so finisher function looks this:
@override public function<list<pair<t, double>>, list<t>> finisher() { return list -> list.stream().map(pair::getfirst).collect(tolist()); } although example bit simple because in case declare collector this:
class sequentialppscollector<t> implements collector<pair<t, double>, list<t>, list<t>> {...} and let accumulator grab type t of pair object:
@override public biconsumer<list<t>, pair<t, double>> accumulator() { return (list, p) -> list.add(p.getfirst()); } so finisher identity function. first part of answer should give starting point though.
Comments
Post a Comment