Skip to content

Commit 898d2b1

Browse files
author
saurabh.kumar
committed
jdk 23 features
1 parent a5b94b4 commit 898d2b1

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.saurabh.java;
2+
3+
public class MarkdownDoc {
4+
5+
/// Returns a hash code value for the object. This method is
6+
/// supported for the benefit of hash tables such as those provided by
7+
/// [java.util.HashMap].
8+
///
9+
/// The general contract of `hashCode` is:
10+
///
11+
/// - Whenever it is invoked on the same object more than once during
12+
/// an execution of a Java application, the `hashCode` method
13+
/// must consistently return the same integer, provided no information
14+
/// used in `equals` comparisons on the object is modified.
15+
/// This integer need not remain consistent from one execution of an
16+
/// application to another execution of the same application.
17+
/// - If two objects are equal according to the
18+
/// [equals][#equals(Object)] method, then calling the
19+
/// `hashCode` method on each of the two objects must produce the
20+
/// same integer result.
21+
/// - It is _not_ required that if two objects are unequal
22+
/// according to the [equals][#equals(Object)] method, then
23+
/// calling the `hashCode` method on each of the two objects
24+
/// must produce distinct integer results. However, the programmer
25+
/// should be aware that producing distinct integer results for
26+
/// unequal objects may improve the performance of hash tables.
27+
///
28+
/// @return a hash code value for this object.
29+
/// @implSpec As far as is reasonably practical, the `hashCode` method defined
30+
/// by class `Object` returns distinct integers for distinct objects.
31+
/// @see java.lang.Object#equals(java.lang.Object)
32+
/// @see java.lang.System#identityHashCode
33+
void md() {
34+
}
35+
36+
/**
37+
* Returns a hash code value for the object. This method is
38+
* supported for the benefit of hash tables such as those provided by
39+
* {@link java.util.HashMap}.
40+
* <p>
41+
* The general contract of {@code hashCode} is:
42+
* <ul>
43+
* <li>Whenever it is invoked on the same object more than once during
44+
* an execution of a Java application, the {@code hashCode} method
45+
* must consistently return the same integer, provided no information
46+
* used in {@code equals} comparisons on the object is modified.
47+
* This integer need not remain consistent from one execution of an
48+
* application to another execution of the same application.
49+
* <li>If two objects are equal according to the {@link
50+
* #equals(Object) equals} method, then calling the {@code
51+
* hashCode} method on each of the two objects must produce the
52+
* same integer result.
53+
* <li>It is <em>not</em> required that if two objects are unequal
54+
* according to the {@link #equals(Object) equals} method, then
55+
* calling the {@code hashCode} method on each of the two objects
56+
* must produce distinct integer results. However, the programmer
57+
* should be aware that producing distinct integer results for
58+
* unequal objects may improve the performance of hash tables.
59+
* </ul>
60+
*
61+
* @return a hash code value for this object.
62+
* @implSpec As far as is reasonably practical, the {@code hashCode} method defined
63+
* by class {@code Object} returns distinct integers for distinct objects.
64+
* @see java.lang.Object#equals(java.lang.Object)
65+
* @see java.lang.System#identityHashCode
66+
*/
67+
void javadoc() {
68+
}
69+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.saurabh.java;
2+
3+
import static java.io.IO.*;
4+
import module java.base;
5+
6+
class ModuleImport {
7+
void main() {
8+
final var authors = List.of("James", "Bill", "Guy", "Alex", "Dan", "Gavin");
9+
for (var name : authors) {
10+
println(name + ": " + name.length());
11+
}
12+
}
13+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.saurabh.java;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.util.Arrays;
7+
import java.util.stream.Gatherers;
8+
import java.util.stream.Stream;
9+
10+
import static java.util.Arrays.asList;
11+
12+
/**
13+
* https://cr.openjdk.org/~vklang/Gatherers.html
14+
* https://www.infoworld.com/article/2510405/stream-gatherers-a-new-way-to-manipulate-java-streams-2.html
15+
*/
16+
@SuppressWarnings({"java:S3655", "java:S2629", "all"})
17+
public class StreamGatherers22 {
18+
19+
private static final Logger LOG = LoggerFactory.getLogger(StreamGatherers22.class);
20+
21+
public static void main(String[] args) {
22+
23+
final var numbers = asList(1, 2, 3, 4, 5, 6);
24+
LOG.info("{}", Arrays.toString(numbers.stream().filter(number -> number % 2 == 0).toArray())); // result: { 2, 4, 6 }
25+
26+
// windowFixed method is a simpler way to gather your windowed elements into buckets
27+
LOG.info(
28+
"\nGather WindowFixed\n{}", Stream.iterate(0, i -> i + 1)
29+
.gather(Gatherers.windowFixed(2))
30+
.limit(5)
31+
.toList()
32+
);
33+
34+
// Scan is something like windowFixed but it accumulates the elements into a single element instead of an array
35+
LOG.info(
36+
"\nGather Scan\n{}", Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9)
37+
.gather(
38+
Gatherers.scan(() -> "", (string, number) -> string + number)
39+
)
40+
.toList()
41+
);
42+
43+
// windowSliding works like windowFixed() except each window starts on the next element in the source array
44+
LOG.info(
45+
"\nGather WindowSliding\n{}", Stream.iterate(0, i -> i + 1)
46+
.gather(Gatherers.windowSliding(2))
47+
.limit(5)
48+
.toList()
49+
);
50+
51+
// Folding is a generalization of reduction. With reduction, the result type is the same as the element type, the combiner is associative,
52+
// and the initial value is an identity for the combiner. For a fold, these conditions are not required, though we give up parallelizability.
53+
LOG.info(
54+
"\nGather fold\n{}\nStream Reduce\n{}", Stream.of("hello", "world", "how", "are", "you?")
55+
.gather(
56+
Gatherers.fold(
57+
() -> "",
58+
(acc, element) -> acc.isEmpty() ? element : acc + "," + element
59+
)
60+
)
61+
.findFirst()
62+
.orElseThrow(),
63+
Stream.of("hello", "world", "how", "are", "you?")
64+
.reduce("", (acc, element) -> acc.isEmpty() ? element : acc + "," + element)
65+
);
66+
67+
// mapConcurrent can specify a maximum number of threads to use concurrently in running the map function provided. Virtual threads will be used.
68+
LOG.info(
69+
"\nGather MapConcurrent\n{}", Stream.of(1, 2, 3, 4, 5)
70+
.gather(Gatherers.mapConcurrent(4, x -> x * x)).toList()
71+
);
72+
73+
}
74+
75+
}

0 commit comments

Comments
 (0)