functional programming in Java -
how can emulate functional programming in java, specifically, doing things map function collection of items?
map(func, new string[]{"a","b","c"});
what's least verbose & awkward way it?
unfortunately attempts of functional programming have part of verbose and/or awkward in java.
the direct way provide function
interface (such this 1 form guava) , provide kinds of methods take , call (such collections#transfrom()
think map()
method should do).
the bad thing need implement function
, anonymous inner class, has terribly verbose syntax:
collection<outputtype> result = collections.transform(input, new function<inputtype,outputtype>() { public outputtype apply(inputtype input) { return frobnicate(input); } });
it should noted planned lambda feature of java 8 make considerably easier (and possibly faster!). equivalent code in (the current form of) lambdas this:
collection<outputtype> result = collections.transform(input, someclass::frobnicate);
or more verbose, more flexible:
collection<outputtype> result = collections.transform(input, in -> frobnicate(in));
Comments
Post a Comment