It is equivalent to map (from Haskell) in Java 7
In Haskell, there is a function called map, which uses a class a list and a function f to map the value of type A to the value of type B It returns a list of type B so that each element of the result list comes from the value called f into the input list
For example, given
>A list M = ['a', 'B', 'C'], > and function f = {a '– > 1,' B '– > 2,' C '– > 3}, and then map (m, f) = [1,2,3]
Is there a library for Java 7 that provides books with map like functions? I've seen Apache collectionutils and found foralldo and transform, but they don't allow you to readjust collections of completely different types For the same reason, Google searches in other libraries failed
Be clear: I know how to solve this problem myself, but I strongly believe that there must be a good library to better perform this task
Bonus question: are there any equivalent Haskell functors (i.e. from set to iteratable) available for Java 7? Further explanation: is there a map function that requires an iterative < a > instead of a set < a > and returns an alternative < b > instead of a collection < b > (provided by the fitting function f)?
Solution
You require Java 7 (it's easier to use Java 8):
You can use guava, specifically fluent Iterable
final List<String> strings = Arrays.asList("a","b","c"); final List<Integer> integers = FluentIterable .from(strings) .transform(new Function<String,Integer>() { @Nullable @Override public Integer apply(@Nullable String input) { return input.equals("a") ? 1 : input.equals("b") ? 2 : input.equals("c") ? 3 : -1; } }) .toList();
Bonus problem: set is an iteration: -)