Skip to content

Commit 531e460

Browse files
committed
Java 22 new features
1 parent dc53288 commit 531e460

4 files changed

Lines changed: 185 additions & 0 deletions

File tree

section29/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Eclipse ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
bin/
15+
!**/src/main/**/bin/
16+
!**/src/test/**/bin/
17+
18+
### NetBeans ###
19+
/nbproject/private/
20+
/nbbuild/
21+
/dist/
22+
/nbdist/
23+
/.nb-gradle/
24+
25+
### VS Code ###
26+
.vscode/
27+
28+
### Mac OS ###
29+
.DS_Store
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.eazybytes.java22;
2+
3+
public class UnnamedPatterns {
4+
5+
public static void main(String[] args) {
6+
Vehicle vehicle = new Truck("Toyota");
7+
8+
switch (vehicle) {
9+
case Car _ -> processCar(vehicle);
10+
case Bike _ -> processBike(vehicle);
11+
case Truck _ -> processTruck(vehicle);
12+
}
13+
}
14+
15+
public static void processCar(Vehicle vehicle) {
16+
if(vehicle instanceof Car car) {
17+
System.out.println("Processing car model: " + car.getModel());
18+
}
19+
}
20+
21+
public static void processBike(Vehicle vehicle) {
22+
if(vehicle instanceof Bike bike) {
23+
System.out.println("Processing bike brand: " + bike.getBrand());
24+
}
25+
}
26+
27+
public static void processTruck(Vehicle vehicle) {
28+
if(vehicle instanceof Truck truck) {
29+
System.out.println("Processing truck manufacturer: " + truck.getManufacturer());
30+
}
31+
}
32+
33+
}
34+
35+
36+
37+
38+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.eazybytes.java22;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileReader;
6+
import java.io.IOException;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.stream.Collectors;
11+
12+
public class UnnamedVariables {
13+
14+
public static void main(String[] args) {
15+
// Scenario 1
16+
List<String> wordList = Arrays.asList("apple", "banana", "orange", "grape", "kiwi");
17+
int totalWords = countWords(wordList);
18+
System.out.println(totalWords);
19+
20+
// Scenario 2
21+
List<String> fruits = Arrays.asList("apple", "banana", "orange");
22+
Map<String, String> fruitMap = fruits.stream()
23+
.collect(Collectors.toMap(f -> f, _ -> "Fruit"));
24+
System.out.println(fruitMap);
25+
26+
// Scenario 3
27+
boolean isValid = convertAndDisplay("45");
28+
29+
// Scenario 4
30+
executeIfFilePresent();
31+
32+
// Scenario 5
33+
Point point = new Point(7, 3);
34+
if (point instanceof Point(int x, _)) {
35+
// Only use the 'x' property from the Point record
36+
System.out.printf("Point object with value of x: %d%n", x);
37+
}
38+
}
39+
40+
public static int countWords(Iterable<String> words) {
41+
int totalWords = 0;
42+
for (String _ : words) {
43+
totalWords++;
44+
}
45+
return totalWords;
46+
}
47+
48+
public static boolean convertAndDisplay(String input) {
49+
boolean isValid;
50+
try{
51+
int _ = Integer.parseInt(input);
52+
isValid = true;
53+
}catch (NumberFormatException _) {
54+
System.out.println("NumberFormatException due to invalid input: " + input);
55+
isValid = false;
56+
}catch (Exception _) {
57+
System.out.println("Exception due to invalid input: " + input);
58+
isValid = false;
59+
}
60+
return isValid;
61+
}
62+
63+
public static void executeIfFilePresent() {
64+
String filePath = "example.txt";
65+
try(BufferedReader _ = new BufferedReader(new FileReader(filePath));){
66+
System.out.println("Executing some logic");
67+
} catch (FileNotFoundException _) {
68+
System.out.println("FileNotFoundException");
69+
} catch (IOException _) {
70+
System.out.println("IOException");
71+
}
72+
}
73+
74+
}
75+
76+
record Point(int x, int y) { }
77+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.eazybytes.java22;
2+
3+
public sealed interface Vehicle permits Car, Bike, Truck {
4+
5+
}
6+
7+
final class Bike implements Vehicle {
8+
private final String brand;
9+
10+
public Bike(String brand) {
11+
this.brand = brand;
12+
}
13+
14+
public String getBrand() {
15+
return brand;
16+
}
17+
}
18+
19+
final class Car implements Vehicle {
20+
private final String model;
21+
22+
public Car(String model) {
23+
this.model = model;
24+
}
25+
26+
public String getModel() {
27+
return model;
28+
}
29+
}
30+
31+
final class Truck implements Vehicle {
32+
private final String manufacturer;
33+
34+
public Truck(String manufacturer) {
35+
this.manufacturer = manufacturer;
36+
}
37+
38+
public String getManufacturer() {
39+
return manufacturer;
40+
}
41+
}

0 commit comments

Comments
 (0)