Java Study Notes - Functional Interface - Stream

1. Functional interface

1.1, functional interface

Functional interface: an interface with one and only one abstract method
Functional programming in Java is embodied in Lambda expressions, so functional interfaces can be said to be applicable to Lambda interfaces
only by ensuring that there is and only one abstract method in the interface, Lambda in java can be deduced smoothly

@FunctionalInterface
public interface Demo {
    
    
	void show();
}

@FunctionalInterface is placed above the interface definition: if the interface is a functional interface, the compilation passes; if not, the compilation fails

If the parameter of the method is a functional interface, we can use lambda expression to pass as parameter

start(() -> System.out.println("1"));

If the return value of the method is a functional interface, we can use a lambda expression to return the result

private static Comparator<String> getcomparator(){
    
    
		return (s1,s2) -> s1.length() - s2.length();	
}

1.2. Commonly used functional interfaces

1. Supplier interface
Supplier: contains a parameterless method
T get(): get the result
This method does not require parameters, it will return a data according to some implementation logic (implemented by Lambda expression)
Supplier interface is also called production type Interface, if we specify what type of generic interface is, then the get method in the interface will produce what type of data for us to use.
2. Consumer interface
Consumer: contains two methods
void accept(T t): perform this operation on the given parameters
default ConsumerandThen(Consumer after): Return a combined Consumer, perform this operation in turn, and then execute after
Consumer interface is also It is called a consumer interface, and the data type of the data it consumes is specified by generics
. 3. Predicate interface
Predicate: four commonly used methods
boolean test(T t): judge the given parameters (judgment logic Lambda expression implementation) , returns a boolean value
default Predicatenegate(): returns a logical negative pair, corresponding to a logical non-
default Predicateand(Predicate other): returns a combined judgment, corresponding to short-circuit and
default Predicateor(Predicate other): returns a combined judgment, corresponding to short-circuit or
The Predicate interface is usually used to judge whether the parameters meet the specified conditions
4. Function interface
Function<T,R>: Two commonly used methods
R apply(T t): applies this function to the given arguments
default Function andThen(Function after): returns a combined function that first applies the function to the input, then Applying the after function to the result
Function<T,R> interface is usually used to process the parameters, transform (the processing logic is implemented by Lambda expressions), and then return a new value

2. Stream stream

2.1, the production method of Stream stream

Generate Streams: Generate streams
from data sources (collections, arrays, etc.)

list.stream();

Intermediate operations:
A stream can be followed by zero or more intermediate operations, the main purpose of which is to open the stream, do some degree of data filtering/mapping, and then return a new stream for use by the next operation

filter();

Finalization operation:
A stream can only have one finalization operation. When this operation is performed, the stream is used "light" and cannot be operated. So this must be the last operation of the stream

forEach();

Common Generation Methods of Stream Streams
Collections of Collection systems can use the default method stream() to generate streams

List<String> list = new ArrayList<String>();
Stream<String> ls = list.stream();
	
Set<String> set = new HashSet<String>();	
Stream<String> ss = set.stream();

default Stream<E>stream()
A collection of Map systems to generate streams indirectly

Map<String,Integer> map = new HashMap<String,Integer>();
Stream<String> s = map.keySet().stream();
Stream<Integer> i = map.values().stream();
Stream<Map.Entry<String, Integer>> en = map.entrySet().stream();

Arrays can generate streams through the static method of(T...values) of the Stream interface

String[] st = {
    
    "hello","java","world"};
Stream<String> sts = Stream.of(st);

2.2. Common intermediate operation methods of Stream stream

1. filter
Streamfilter(Predicate predicate): used to filter the data in the
stream, the method in the Predicae interface boolean test(T t): judge the given parameter and return a boolean value
2. limit skip
Streamlimit(long maxSize) : Returns a stream composed of elements in this stream, intercepts the data with the specified number of parameters before
Streamskip(long n): Skips the data with the specified number of parameters, and returns a stream composed of the remaining elements of the stream
3. concat distinct
staticStreamconcat( Stream a, Stream b): Combine two streams a and b into one stream
Streamdistinct(): Return a stream composed of different elements of the stream (according to Object.equals(Object))
4, sorted
Streamsorted(): Return from this A stream composed of elements of a stream, sorted according to the natural order
Streamsorted(Comparator comparator): Returns a stream composed of the elements of this stream, sorted according to the provided Comparator
5. map mapToInt
Streammap(Function mapper): Returns a stream applied by the given function A stream consisting of the results of the elements of this stream.
Methods in the Function interface R apply(T t)
IntStream mapToInt(ToIntFunction mapper): Returns an IntStream containing the result of applying the given function to the elements of this stream
IntStream: Represents
the method int applyAsInt(T value) in the ToIntFunction interface of the original int stream

2.3. Common termination operation methods of Stream stream

void forEach(Consumer action): performs an operation on each element of this stream.
Method in the Consumer interface void accept(T t): performs this operation on the given parameter
long count(): returns the number of elements in this stream

2.4, Stream collection operation

R collect(Collector collector)
But the parameter of this collection method is a Collector interface . The
tool class Collectors provides a specific collection method.
public staticCollector toList(): collects elements into List collection
public staticCollector toSet(): collects elements into Set collection Medium
public static Collector toMap(Function keyMapper, Function valueMapper): collect elements into the Map collection

Guess you like

Origin blog.csdn.net/weixin_45573296/article/details/123171986