A method that transforms all the elements in an object
public class Fruit {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("apple");
list.add("orange");
list.add("banana");
List<String> ret = list.stream().map(s -> s.toUpperCase()).collect(Collectors.toList());
ret.forEach(System.out::println);
}
}
//The output result has uppercase elements
(.Collect (Collectors.toList ()); is attached to return as List)
** If you don't use a lambda expression, it looks like this: ** **
List<String> ret = list.stream().map(new Function<String,String>(){
@Override
public String apply(String s) {
System.out.println(s);
return s.toUpperCase();
}
}).collect(Collectors.toList());
map () has a Function as an argument
Interface Function <T, R> T… Argument type R ... Return type
Why can apply be used suddenly? Since there are only apply methods in the interface Function (default is ignored)
Recommended Posts