Skip to content

Commit b75c209

Browse files
authored
Create lambda.md
1 parent 54c9800 commit b75c209

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

lambda/lambda.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Lambda
2+
3+
4+
## throw IOException
5+
6+
```
7+
import java.io.IOException;
8+
import java.nio.file.FileStore;
9+
import java.nio.file.FileSystem;
10+
import java.nio.file.FileSystems;
11+
import java.util.function.Consumer;
12+
13+
@FunctionalInterface
14+
public interface ThrowingConsumer<T> extends Consumer<T> {
15+
16+
@Override
17+
default void accept(final T elem) {
18+
try {
19+
acceptThrows(elem);
20+
} catch (final Exception e) {
21+
// Implement your own exception handling logic here..
22+
// For example:
23+
// System.out.println("handling an exception...");
24+
// Or ...
25+
throw new RuntimeException(e);
26+
}
27+
}
28+
29+
void acceptThrows(T elem) throws Exception;
30+
31+
}
32+
33+
void main(String[] args) throws IOException {
34+
35+
FileSystem fileSystem = FileSystems.getDefault();
36+
37+
System.out.printf("%30s | %10s | %23s | %20s \n", "", "Type", "Total space", "Free space");
38+
System.out.println("-------------------------------------------------"
39+
+ "----------------------------------------------------------");
40+
41+
Iterable<FileStore> fileStores = fileSystem.getFileStores();
42+
43+
// for (var fileStore : fileStores) {
44+
// System.out.printf("%30s | %10s | %20s GB | %20s GB\n", fileStore, fileStore.type(),
45+
// (fileStore.getTotalSpace() / 1073741824f),
46+
// (fileStore.getUsableSpace() / 1073741824f));
47+
// }
48+
49+
fileStores.forEach((ThrowingConsumer<FileStore>) fileStore -> {
50+
51+
System.out.printf("%30s | %10s | %20s GB | %20s GB\n", fileStore, fileStore.type(),
52+
(fileStore.getTotalSpace() / 1073741824f),
53+
(fileStore.getUsableSpace() / 1073741824f));
54+
});
55+
}
56+
57+
58+
59+
60+
//class MyConsumer implements Consumer<FileStore>
61+
//{
62+
//
63+
// @Override
64+
// public void accept(FileStore fileStore) throws IOException {
65+
// System.out.printf("%30s | %10s | %20s GB | %20s GB\n", fileStore, fileStore.type(),
66+
// (fileStore.getTotalSpace() / 1073741824f),
67+
// (fileStore.getUsableSpace() / 1073741824f));
68+
// }
69+
//}
70+
```

0 commit comments

Comments
 (0)