I need a map function. Is there something like this in Java already?
(For those who wonder: I of course know how to implement this trivial function myself...)
I need a map function. Is there something like this in Java already?
(For those who wonder: I of course know how to implement this trivial function myself...)
There is a wonderful library called Functional Java which handles many of the things you'd want Java to have but it doesn't. Then again, there's also this wonderful language Scala which does everything Java should have done but doesn't while still being compatible with anything written for the JVM.
There is no notion of a function in the JDK as of java 6.
Guava has a Function interface though and the
Collections2.transform(Collection, Function)
method provides the functionality you require.
Example:
// example, converts a collection of integers to their
// hexadecimal string representations
final Collection<Integer> input = Arrays.asList(10, 20, 30, 40, 50);
final Collection<String> output =
Collections2.transform(input, new Function<Integer, String>(){
@Override
public String apply(final Integer input){
return Integer.toHexString(input.intValue());
}
});
System.out.println(output);
Output:
[a, 14, 1e, 28, 32]
map(function,iterable) i think this style of coding is with functional style of language, languages that support passing function as a argument. So this is possibly not in java.