Monday 15 June 2015

java - Chain of Map method references -



java - Chain of Map method references -

i'm working twitter4j. question i'm asking more general. want access user id of given tweet. currently, have next 2 options:

//option 1 stream.map(status -> status.getuser().getid()) .foreach(system.out::println); //option 2: stream.map(status::getuser) .map(user:getid) .foreach(system.out::println);

i don't lambda look in first option, nor beingness forced phone call 2 maps in sec one. there way create chain of method references? know status::getuser::getid not work, wondering if there alternative.

nope, these 2 ways of doing it. else end beingness less clear.

but, since asked, here options.

static<t,u,r> function<t,r> chain( function<? super t, ? extends u> f1, function<? super u, ? extends r> f2) { homecoming t -> f2.apply(f1.apply(t)); } stream.map(chain(status::getuser, user::getid))

or

static<t,r> function<t,r> func(function<t,r> f) { homecoming f; } stream.map(func(status::getuser).andthen(user::getid))

java java-8

No comments:

Post a Comment