java-16-feature
***** 已完成 jdk 16 feature [#B] [100%] :task:
CLOSED: [2021-03-31 三 21:28] SCHEDULED: <2021-03-31 三>
:PROPERTIES:
:REF: https://www.youtube.com/watch?v=1hyWJTjxeGM
:END:
- State "已完成" from "进行中" [2021-03-31 三 21:28]
- State "进行中" from "未开始" [2021-03-31 三 20:35]
1. Record
# MyRecord
public record MyRecord(String name, int id) {
public MyRecord{
validateMyFields(id, name);
}
public MyRecord(String name, int id){
validateMyFields(id, name);
this.name = name;
this.id = id;
}
private void validateMyFields(int id, String name){
// do validate fields
}
}
# Test MyRecord
new MyRecord("Trisha", 4326493)
2. Pattern Matching for InstanceOf
public class PatternMatchingForInstanceOf{
public void inspectionToConvertInstanceof(Person person){
if(person instanceof Employee employee){
if(employee.isBasedInOffice()){
employee.workFromHome();
}
}
}
}
3. New methods on Stream
var strings = ArrayList<String>();
# ModernJava
strings.stream()
.filter(s -> s.startsWith("S"))
.sorted()
.collect(Collectors.toUnmodifiableList());
# JDK16
strings.stream()
.filter(s-> s.startsWith("S"))
.sorted()
.toList();
4. Warning for Value-Based Classes
# deprecated in java 9, but new for jdk16 marked for removal JEP 390
var aDouble = new Double(1.0);
var aDouble = 1.0;
5. Local Types
1. local records
private Set<Person> filterForPeopleWithFiveOrders(List<Person> people){
record PersonAndOrders(Person person, List<Order> orders){}
return people.stream()
.map(person -> new PersonAndOrders(person, getOrdersFor(person)))
.filter(personAndOrders -> personAndOrders.orders().size() == 5)
.map(PersonAndOrders::person)
.collect(toUnmodifiableSet());
}
2. local enums
private void organisePeople(List<Person> people){
enum Role{Employee, Customer, Both, None}
var peopleByRole = new HashMap<Role, List<Perso>>();
people.stream()
.filter(Person::isCustomer)
.forEach(person -> peopleByRole.computeIfAbsent(Role.Customer, role -> new ArrayList<>())
.add(person));
}
6. Static methods/classes on inner classes
public class StaticOnInnerClasses{
static class Outer{
class NonStaticInnerClass{
void instanceMethod(){
}
# jdk16 static methods on inner classes
static void staticMethod(){
}
# jdk16 static classes inside inner classes
static class StaticInnerClass{
}
}
}
7. java 16 preview features
1. Sealed Types
public sealed class Colour permits Black, Pink{
}
public final class Black extends Colour{
}
public non-sealed class Pink extends Colour permits Rose, Magenta{
}
public final class Rose extends Pink{
}
public final class Magenta extends Pink{
}
8. Also available in Java 16
1. Text Blocks (jdk 14, jdk 13)
2. Switch Expressions (jdk 12, jdk 13)
9. Java 11 Features
1. Local Variable Type Inference
var
2. Convenience Factory Methods for Collections
1. List.of()
2. Set.of()
3. Map.of()
4. Map.ofEntries()
3. New Methods on Optional
https://javadevcentral.com/optional-new-methods
1. ifPresentOrElse()
2. or()
3. Stream()
4. orElseThrow()
5. isEmpty()
10. Let your tools do the hard work
Entered on [2021-03-31 三 20:32]
- 标题: java-16-feature
- 作者: The Redefine Team
- 创建于 : 2021-03-31 21:29:51
- 更新于 : 2023-05-23 18:52:03
- 链接: https://redefine.ohevan.com/2021/03/31/java-16-feature/
- 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论