-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBeyondJava8.java
138 lines (114 loc) · 3.8 KB
/
BeyondJava8.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package others.youtube;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* Life Beyond Java 8, by Trisha Gee / JetBrains Technology Day for Java (2020)
* https://www.youtube.com/watch?v=gKestt55Q4M
* @todo java 15 and above
*/
public class BeyondJava8 {
public static void main( String[] args ) {
//unmodifiableList();
//unmodifiableMap();
//collectToUnmodifiableList();
//predicateNot();
//newSwitchStatement(new Random().nextInt());
//textBlocks();
/*Person p1 = new Employee("zaki");
instanceOfInJava14(p1);*/
usingRecordInJava14();
}
private static void usingRecordInJava14() {
User user = new User(1, "Trisha");
//with record we get overridden methods out of the box...
System.out.println("user.id() = " + user.id());
System.out.println("user.name() = " + user.name());
System.out.println("user.toString() = " + user.toString());
}
private static void instanceOfInJava14(Person person) {
if (person instanceof Employee)
{
Employee employee = ((Employee) person);
System.out.println("employee.getEmployeeName() = " + employee.getEmployeeName());
}
//in Java 14, we can write the above code as follows
if (person instanceof Employee employee)
{
System.out.println("employee.getEmployeeName() = " + employee.getEmployeeName());
}
}
//java 13, for html, sql queries, json ......
private static void textBlocks() {
String multiLinesText =
"""
Hello Brother, I'm using Java14;
""";
System.out.println("multiLinesText = " + multiLinesText);
}
// the new switch syntax
private static void newSwitchStatement(int i) {
String value = switch (i){
case 0 -> "0";
case 1, 2 -> "1";
default -> "NOT in [0-2]";
};
System.out.println("value = " + value);
}
private static void predicateNot() {
List.of(1,2,3,4,5).stream()
.filter(Predicate.not(x->x>3)) //also using not(x->x>3)
.forEach(System.out::println);
}
//collecting to an unmodifiable List
private static void collectToUnmodifiableList() {
List<String> modifiableList = new ArrayList();
modifiableList.add("A");modifiableList.add("B");modifiableList.add("C");
List<String> unmodifiableList = modifiableList.stream()
.collect(Collectors.toUnmodifiableList());
}
//creating an unmodifiableMap
private static void unmodifiableMap() {
final Map<String, Integer> entries = Map.ofEntries(
Map.entry("A", 0),
Map.entry("B", 1),
Map.entry("C", 2)
);
entries.forEach(( s, i ) -> System.out.println(s+ " => "+ i));
}
//will throw an unsupportedOperationException
public static void unmodifiableList(){
//the old way
List<String> strings = Arrays.asList("A", "B");
List<String> stringList = Collections.unmodifiableList(strings);
stringList.set(0, "C");
//the new one
List<String> stringList2 = List.of("A", "B");
stringList2.set(0, "C");
}
}
//for pattern matching
class Person{
String name;
public Person(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
}
class Employee extends Person{
public Employee(String name) {
super(name);
}
public String getEmployeeName(){
return "Employee{" +
"name='" + this.name + '\'' +
'}';
}
}
record User(int id, String name){
}