-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo.java
More file actions
92 lines (79 loc) · 3.27 KB
/
Copy pathDemo.java
File metadata and controls
92 lines (79 loc) · 3.27 KB
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
package java21_;
import java.awt.*;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.SequencedMap;
import java.util.TreeMap;
public class Demo {
record StudentRecord(Long id, String name) { };
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>(); //random entry when print
SequencedMap<Integer, String> sequencedMap = new LinkedHashMap<>(); // linked insertion order
Map<Object, Object> objectObjectHashtable = new LinkedHashMap<>();
map.put(1, "one");
map.put(6, "six");
map.put(4, "four");
map.put(5, "five");
map.put(2, "two");
map.put(3, "three");
map.put(100, "three");
map.put(20, "three");
map.put(40, "three");
map.put(80, "three");
sequencedMap.putFirst(1, "one");
sequencedMap.putFirst(6, "six");
sequencedMap.putFirst(4, "four");
sequencedMap.putFirst(5, "five");
sequencedMap.putFirst(2, "two");
sequencedMap.putFirst(3, "three");
sequencedMap.putFirst(100, "three");
sequencedMap.putFirst(20, "three");
sequencedMap.putFirst(40, "three");
sequencedMap.putFirst(80, "three");
System.out.println("Sequenced Map: "+sequencedMap);
System.out.println("Map: "+map);
}
public static String checkObType(Object ob) {
return switch (ob) {
case String s1 when s1.length() > 5 -> s1 + "'s long String";
case String s2 -> s2 + "'s short string";
case Integer i -> i + "'s an int ";
case Double d -> d + "'s an Double ";
case Long l -> l + "'s an Long ";
case Float f -> f + "'s an Float ";
case StudentRecord(Long id, String name) -> "this is Record Student";
case null -> "It's null!";
default -> "Unknown type";
};
}
public static void testObType() {
System.out.println("Result 1: " + checkObType("ABC"));
System.out.println("Result 2: " + checkObType(12));
System.out.println("Result 3: " + checkObType(1D));
System.out.println("Result 4: " + checkObType(3000000L));
System.out.println("Result 5: " + checkObType(12f));
System.out.println("Result 6: " + checkObType(null));
System.out.println("Result 7: " + checkObType('1'));
System.out.println("Result 7: " + checkObType(new StudentRecord(1L, "Alan")));
System.out.println("Result 8: " + checkObType("12421412412"));
}
sealed interface Shape permits Circle, Square { }
record Circle(double radius) implements Shape { }
record Square(double side) implements Shape { }
public static String exhaustivenessChecks(Shape shape) {
return switch (shape) {
case Circle c -> "Circle with r = " + c.radius();
case Square s -> "Square with side = " + s.side();
default -> "Unknown type";
};
}
public static String checkStudentRecord(Object ob) {
return switch (ob) {
case StudentRecord(Long id, String name) -> "this is Record Student (" + id + ", " + name + ")";
case null -> "It's null!";
default -> "Unknown type";
};
}
}