public final interface Function<F, T> { T apply(F from); }
public static void main() { // "", intToString. Function<Integer, String> intToString = new Function<Integer, String>() { @Override public String apply(Integer from) { return from.toString(); } }; intToString.apply(9000); // . "9000". }
public String joinNumbers(Collection<? extends Integer> numbers) { StringBuilder result = new StringBuilder(); boolean first = true; for (Integer number : numbers) { if (first) first = false; else result.append(", "); result.append(number); } return result; }
public static final Function<Integer, String> INT_TO_STRING = ... // // from, transformer // . public static <F, T> List<T> map(Collection<F> from, Function<? super F,? extends T> transformer) { ArrayList<T> result = new ArrayList<T>(); for (F element : from) result.add(transformer.apply(element)); return result; } // public static <T> String join(Collection<T> from, String separator) { StringBuilder result = new StringBuilder(); boolean first = true; for (T element : from) { if (first) first = false; else result.append(separator); result.append(element); } return result.toString(); }
public String joinNumbers(Collection<? extends Integer> numbers) { return join(map(numbers, INT_TO_STRING), ", "); }
map
and join
methods are fairly generalized, that is, they can be used not only to solve this problem. This means that they could be allocated to a utility class, and then use this class in different parts of the project.Collection
class in the map
method, it would be possible to pass Iterable
and return a new Iterable
, retrieving data from the transferred collection as the data in the returned collection is crawled, that is, it is lazy to retrieve elements, step by step, and not all at once. Such an implementation will allow, for example, to create a chain of data conversion, highlighting each stage of the conversion into a separate simple function, while the efficiency of the algorithm will remain on the order of O (n):map(map(numbers, MULTIPLY_X_2), INT_TO_STRING); // .
interface Function<F, T>
. An interface similar to the one above.Iterables.filter
. Takes a collection and a predicate function (a function that returns a boolean value). In response, returns a collection containing all the elements of the source, to which the specified function returned true. Conveniently, for example, if we want to filter out all the even numbers from the collection: Iterables.filter(numbers, IS_ODD);
Iterables.transform
. Does the same thing as the map function in my example above.Functions.compose
. Takes two functions. Returns a new function — their composition, that is, the function that receives the element, submits it to the second function, returns the result to the first function, and returns the result obtained from the first function to the user. Composition can be used, for example, as follows: Iterables.transform(numbers, Functions.compose(INT_TO_STRING, MULTIPLY_X_2));
Source: https://habr.com/ru/post/122919/
All Articles