Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lists class in java 8

Tags:

java-8

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

like image 676
ccheneson Avatar asked Aug 28 '26 15:08

ccheneson


1 Answers

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());
    }
}
like image 185
Tagir Valeev Avatar answered Aug 31 '26 18:08

Tagir Valeev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!