This book is quoted unless otherwise specified.
Lambda expressions are powerful notations, as if you could pass the process itself to a method argument, etc.
List<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 3, 2, 1, 4, 5);
//Writing without lambda expression (anonymous class)
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1, o2);
}
});
//How to write using a lambda expression
Collections.sort(list,
(o1, o2) -> Integer.compare(o1, o2)
);
The lambda expression is just "easy to write an implementation method of an anonymous class", but when combined with the Stream API described later, it can be written in an easy-to-understand manner.
Often used in event listener settings.
//How to write when setting a click event on Android
findViewById(R.id.hogehoge).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Processing at the time of clicking
}
});
Functional interface: An interface with only one abstract method
[Try] Check JavaDoc. JavaDoc Comparator
Functional interfaces are replaced by lambda expressions
(argument)-> {processing}
return
and curly braces (when there is one process)//Lambda expression basic grammar
Collections.sort(list,
(Integer o1, Integer o2) -> {
return Integer.compare(o1, o2);
}
);
//Omit argument type
Collections.sort(list,
(o1, o2) -> {
return Integer.compare(o1, o2);
}
);
//Argument type, return, and curly braces omitted
Collections.sort(list,
(o1, o2) -> Integer.compare(o1, o2)
);
The method itself can also be assigned.
List<String> list = Arrays.asList("X", "Y", "Z");
//How to write using method references
list.forEach(System.out::println);
//How to write using a lambda expression
list.forEach(str -> System.out.println(str));
The argument of the forEach
method is a functional interface called java.util.function.Consumer
.
2.Stream API
The Stream API was introduced as a way to efficiently describe "stream processing" that sequentially processes large amounts of data.
List<Integer> list = Arrays.asList(100, 60, 30, 50, 70);
list.stream() //Stream instantiation
.filter(s -> s>=70) //Intermediate operation.//Extract over 70.
.forEach(s -> System.out.println(s)); //Termination operation.
Display TODO Stream image
Stream API is "API that describes What, not How".
How: Individual processing
What: Purpose of processing
Stream API writes the purpose without writing the loop processing
Same idea as SQL.
//Create Stream from List or Set
List<Integer> list = Arrays.asList(100, 60, 30, 50, 70);
list.stream() //Stream instantiation
.forEach(System.out::println);
//Create a Stream from a numeric range
IntStream.range(1, 5) //Does not include the end
.forEach(System.out::println);
IntStream.rangeClosed(1, 5) //Including the end
.forEach(System.out::println);
List<Integer> list = Arrays.asList(10, 6, 3, 5, 7);
//map:Replace element with another value
list.stream()
.map(s -> s * s)
.forEach(System.out::println);
//filter:Narrow down only the elements that match the conditions
list.stream()
.filter(s -> s>=7)
.forEach(System.out::println);
//sort:
list.stream()
.sorted((s1,s2) -> s2 - s1) //descending order
.forEach(System.out::println);
List<Integer> list = Arrays.asList(10, 6, 3, 5, 7);
//forEach:Take action on an element
list.stream()
.forEach(System.out::println);
//collect:Create results
List<Integer> newList = list.stream()
.filter(s -> s>=7)
.collect(Collectors.toList());
//average:Returns the average(Aggregation operation)
list.stream()
.average();
JavaScript too. .. ..
Recommended Posts