Skip to content

Commit b018c76

Browse files
committed
older snippets
1 parent 62b3c61 commit b018c76

7 files changed

+59
-0
lines changed

Diff for: java21/HelloWorld.snippet

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var name = "Duke";
2+
System.out.println("👋 Hello, " + name);
3+

Diff for: java21/Record_1.snippet

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
record Player(String last, String first, int level) {}
2+
var jane = new Player("Doe", "Jane", 42);
3+
System.out.println(jane);

Diff for: java21/Record_2.snippet

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
record Population(int population) {}
2+
record City(String name, Population population) {
3+
// static methods are allowed in records
4+
public static City of(String name, int p) {
5+
var population = new Population(p);
6+
return new City(name, population);
7+
}
8+
}
9+
10+
var paris = City.of("Paris", 2_161);
11+
System.out.println(paris);

Diff for: java21/Record_3.snippet

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
record City(String name) {
2+
3+
public boolean equals(Object other) {
4+
return other instanceof City(String name) &&
5+
this.name.equalsIgnoreCase(name);
6+
}
7+
8+
public int hashCode() {
9+
return name != null ? name.toUpperCase().hashCode() : 0;
10+
}
11+
}
12+
13+
var paris1 = new City("Paris");
14+
var paris2 = new City("paris");
15+
var paris3 = new City("PARIS");
16+
System.out.println("1 == 2 ? " + paris1.equals(paris2));
17+
System.out.println("2 == 3 ? " + paris2.equals(paris3));
18+
System.out.println("1 == 3 ? " + paris1.equals(paris3));

Diff for: java21/Stream_1.snippet

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
List<String> cities =
2+
Arrays.asList("Shenzhen", "Brussels", "Taipei", "Buenos Aires", "Sydney", "Bristol");
3+
4+
cities.stream()
5+
.filter(s -> s.startsWith("B"))
6+
.map(String::toUpperCase)
7+
.sorted()
8+
.forEach(System.out::println);

Diff for: java21/Stream_2.snippet

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Stream<String> stream = Stream.of("Shenzhen", "Brussels", "Taipei", "Buenos Aires", "Sydney", "Bristol");
2+
3+
List<String> cities = stream.sorted().collect(Collectors.toList());
4+
5+
System.out.println(cities);

Diff for: java21/Stream_3.snippet

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var cities = """
2+
San Francisco
3+
Casablanca
4+
Antwerp
5+
New Delhi
6+
Osaka
7+
""";
8+
9+
Stream<String> lines = cities.lines();
10+
11+
System.out.println(lines.toList());

0 commit comments

Comments
 (0)