Commit 9571402
committed
feat(Java11): add demo showcasing HTTP Client, var in lambdas, and BiFunction
What
- Added JAVA11 class in package Java11 to demonstrate new Java 11 features.
- Included:
- HTTP Client API example with synchronous request/response.
- Sends GET request to https://jsonplaceholder.typicode.com/posts/1.
- Prints response body as string.
- Local-variable syntax for lambda parameters using var.
- Iterates names list with forEach((var name) -> ...).
- BiFunction<Integer, Integer, Integer> with var syntax in lambda.
- Adds two integers and prints result (30).
Why
- Shows modernized features introduced in Java 11.
- Demonstrates usage of standard, built-in HTTP client replacing older libraries.
- Highlights var keyword in lambda parameters, improving consistency with local variable declarations.
- Provides example of BiFunction with explicit var parameter typing.
How
- Created HttpClient with HttpClient.newHttpClient().
- Built request using HttpRequest.newBuilder().uri(...).build().
- Sent request synchronously using client.send(request, BodyHandlers.ofString()).
- Printed response body.
- Created list ["Alice","Bob","Charlie"] and iterated with forEach((var name) -> ...).
- Defined BiFunction with (var x, var y) -> x + y and applied with inputs 10 and 20.
Logic
- Inputs:
- HTTP GET request URI: https://jsonplaceholder.typicode.com/posts/1.
- List of names ["Alice","Bob","Charlie"].
- Integers 10 and 20.
- Outputs:
- Prints response body from API.
- Prints greetings: Hello, Alice / Hello, Bob / Hello, Charlie.
- Prints "Sum using BiFunction with var: 30".
- Flow:
1. Construct HTTP request and send.
2. Print API response.
3. Iterate list with lambda using var parameter.
4. Execute BiFunction add with var parameters.
- Edge cases:
- Network errors may throw IOException/InterruptedException.
- API response may vary or be unavailable.
- Complexity / performance:
- HTTP request cost depends on network latency.
- List iteration and BiFunction execution are O(n) and O(1), respectively.
- Concurrency / thread-safety:
- HttpClient is immutable and thread-safe.
- Lambdas are stateless; safe to run concurrently.
- Error handling:
- Method declares IOException and InterruptedException for safe propagation.
Real-life applications
- HTTP Client API for REST API calls in microservices, integrations, or data fetching.
- var in lambda improves readability and reduces boilerplate in functional programming.
- BiFunction with var shows flexible lambda typing in modern Java.
Notes
- In production, prefer proper error handling instead of throwing exceptions from main.
- Method reference System.out::println could simplify forEach example.
- For asynchronous HTTP calls, HttpClient provides sendAsync() for non-blocking requests.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 5d73a34 commit 9571402
1 file changed
+37
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
0 commit comments