lambda - Extending List<T> in Java 8 -
i want map 1 list list. example if had list of people, , wanted list of names, do:
goal
list<person> people = ... ; list<string> names = people.map(x -> x.getname());
something possible java 8:
java 8 version
list<string> names = people.stream() .map(x -> x.getname()) .collect(collectors.tolist());
but not nice. in fact, think using guava cleaner:
guava version
list<string> names = lists.transform(people, x -> x.getname());
however, chaining. so, goal possible?
i have heard people java 8 default methods similar c# extension methods. c# extension method, add helper method ienumerable<t>
:
public static ienumerable<tret> map<t, tret>(this ienumerable<t> list, func<t, tret> selector) { return list.select(selector); }
however can't figure out how use default methods extend existing interface.
also, trivial example. in general, able extend list<t>
, iterable<t>
interfaces make interacting streams api easier.
no; can't that.
default methods not same extension methods; can defined within original interface.
Comments
Post a Comment