What is functional programming
The characteristics of
- Function as the first object
- Focus on descriptions rather than execution steps (similar to SQL)
- More concerned with the relationships between algebraic structures
- immutable
features
- Immutable (don’t worry about concurrency, object immutable reduces bugs)
- Inertia is evaluated
- closure
- Higher-order functions
- Currie,
- Part of the application
- Combined with the rate of
Functional programming scenarios that existed before JAVA8
- Create a thread
- The strategy pattern
- The UI programming
- An asynchronous callback
Lambda expressions
Lambda expression syntax sugar
- Parameter types can be deduced
- Braces may be omitted for a single line
- Single arguments can omit the parentheses
Functional interface SAM
- Built-in common functional interfaces
Method references
- Static methods -> need to tell which class classX::methodName belongs to
- The constructor -> needs to tell which class classX::new belongs to
- Specify instance methods -> Need to tell which instance belongs to instance::methodName
Functional interface conversion
Because Java is strongly typed, conversions can be made in cases where we do not require the function signature to be exactly the same
- Ignore the input: Function< -supplier
- Ignore to return: Consumer< -function
- Ignore the input and return: Runnable< -supplier
Special void-compatibility rules:
- If lambada is a statement expression, then a function whose return value is signed void can be assigned even if the lambda has a return value
Stream
Stream VS List
- Stream can be infinite
- Streams can be processed in parallel
- Stream may delay processing
Create the Stream:
- Static data stream.of ()
- Container collection. The stream ()
- Dynamic: Stream. Iterate
- Other apis