Via twitter, I discovered that blog post .
Since I am currently exploring Java 8, I tried running the examples in it.
One thing is that I can not find the class Lists (which implements a map) like in the examples:
Function<String, String> identity = (s -> s); // return the argument
List<String> ls = Arrays.asList("Alice", "Bob", "Christine");
List<String> l2 = Lists.map(ls, identity); // [Alice, Bob, Christine] <- the same thing!
Anyone know where I can find it in order to make the example run ?
Thank you
There's no such method in JDK as well as in popular libraries. However you may write it by yourself:
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Lists {
public static <T, R> List<R> map(List<T> input,
Function<? super T, ? extends R> mapper) {
return input.stream().map(mapper).collect(Collectors.toList());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With