Skip to content

Commit 594388c

Browse files
committed
Update challenges.md
1 parent ea90b21 commit 594388c

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/streams/challenges.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,65 @@
11
# Challenges
2+
3+
4+
Remember the rules for this are
5+
6+
- Try to use only the information given up to this point in this book.
7+
- Try not to give up until you've given it a solid attempt
8+
9+
## Challenge 1.
10+
11+
Translate the following code using a for-loop to code using streams.
12+
13+
```java,editable
14+
import module java.base;
15+
16+
class Main {
17+
void main() {
18+
for (int i = 0; i < 10; i++) {
19+
IO.println(i);
20+
}
21+
}
22+
}
23+
```
24+
25+
## Challenge 2.
26+
27+
Translate the following code using a for-loop to code using streams.
28+
29+
```java
30+
import module java.base;
31+
32+
class Main {
33+
void main() {
34+
List<Integer> timestamps = List.of(1, 1756137441);
35+
36+
Set<Instant> instants = new HashSet<>();
37+
for (int timestamp : timestamps) {
38+
instants.add(Instant.ofEpochSecond(timestamp));
39+
}
40+
41+
IO.println(instants);
42+
}
43+
}
44+
```
45+
46+
## Challenge 3.
47+
48+
Read the documentation on [`Collector`](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/util/stream/Collector.html) and [`Collectors`](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/util/stream/Collector.html).
49+
50+
Make an implementation of `Collector` that can collect elements into `MySpecialList`.
51+
52+
```java
53+
import module java.base;
54+
55+
class MySpecialList<T> extends ArrayList<T> {}
56+
57+
class Main {
58+
// CODE HERE
59+
60+
void main() {
61+
MySpecialList<Integer> l = Stream.of(1, 2, 3)
62+
.collect(/* CODE HERE */);
63+
}
64+
}
65+
```

0 commit comments

Comments
 (0)