目录
New Java 8 features simplify the concurrency operations (Runnable, Comparor)
Local Variable Type Inference (LVTI) - var (start from Java 10)
switch expressions and pattern matching
Unnamed Classes and Instance Main Methods
LTS (Long Term Support): 8, 11, 17, 21
https://openjdk.org/projects/jdk8/
https://openjdk.org/projects/jdk/11
https://openjdk.org/projects/jdk/17/
https://openjdk.org/projects/jdk/21/
Java 8
Functional Programming
Functional Interfaces
Method References
Lambdas
Streams API
Optionals
New Date/Time APIs
New Java 8 features simplify the concurrency operations (Runnable, Comparor)
New Functional Interfaces in Java 8:
- Consumer – BiConsumer, IntConsumer, DoubleConsumer, LongConsumer
- Predicate – BiPredicate, IntPredicate, LongPredicate
- Function – BiFunction, UnaryOperator, BinaryOperator, IntFunction, DoubleFunction, LongFunction,IntToDoubleFunction, IntoLongFunction,DoubletoIntFunction, DoubletoLongFunction,LongtoIntFunction, LongtoDoubleFunction,ToIntFunction, ToDoubleFunction,ToLongFunction
- Supplier – IntSupplier, LongSupplier, DoubleSupplier, BooleanSupplier
IntelliJ: shift twice to search Consumer
Notes: "Bi"就是接受两个参数
Imperative vs Declarative Programming
Intellij IDEA 快捷方式:Options+Enter可以把lambda转成method reference
使用函数和使用方法之间的区别和使用场景
方法(Method)是指属于某个类或对象的行为或操作,它是面向对象编程的基本组成部分。方法通常用于描述对象的行为,可以访问对象的属性并对其进行操作。
函数(Function)是独立于对象的,可以独立存在的一段代码,它接受输入参数并返回一个值。在Java中,函数通常指的是静态方法(static method)或者Lambda表达式。
使用方法:
当需要描述某个对象的行为或操作时,通常应该使用方法。方法可以访问对象的属性,并且可以通过关键字 this 来引用当前对象。方法可以被继承,可以被重写,可以实现接口,从而实现多态。
使用函数:
当某个行为或操作独立于具体对象时,可以考虑使用函数。函数可以独立存在,不依赖于特定的对象实例。函数可以作为参数传递给其他函数,可以作为返回值返回,可以用于函数式编程等场景。
List<String> studentActivities=StudentDataBase.getAllStudents().
stream()//Stream<Student>
.map(Student::getActivities)//<Stream<List<Activites>>
.flatMap(List::stream)//<Stream<String>
.distinct()//removesduplicates
.collect(Collectors.toList());//collectsittoalist.
Default and Static Methods in Interfaces
Java 11
Local Variable Type Inference (LVTI) - var (start from Java 10)
Private Interface Methods
Annotations
Security
Modules
Java 17
Sealed classes / interfaces
records
switch expressions and pattern matching
text blocks
Sealed classes / interfaces
Used to just allow some inheritance
Text Blocks
Java 21
Java21 于2023年9月19日发布,是继之前的Java17之后的最新LTS版本。
https://openjdk.org/projects/jdk/21/
JEP: Java/JDK Enhancement Proposal
- Unnamed Classes and Instance Main Methods
- Record Patterns
- Pattern-matching for switch
- Sequenced Collections
Unnamed Classes and Instance Main Methods
Record Patterns
Pattern-matching for switch
Sequenced Collections