Skip to content

Commit 9571402

Browse files
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

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package Java11;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.net.http.HttpClient;
6+
import java.net.http.HttpRequest;
7+
import java.net.http.HttpResponse;
8+
import java.net.http.HttpResponse.BodyHandlers;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
import java.util.function.BiFunction;
12+
13+
public class JAVA11 {
14+
public static void main(String[] args) throws IOException, InterruptedException {
15+
// HTTP Client API Example (Synchronous)
16+
System.out.println("Java 11 HTTP Client Example:");
17+
18+
HttpClient client = HttpClient.newHttpClient();
19+
HttpRequest request = HttpRequest.newBuilder()
20+
.uri(URI.create("https://jsonplaceholder.typicode.com/posts/1"))
21+
.build();
22+
23+
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
24+
System.out.println("Response from API:\n" + response.body());
25+
26+
// Local-variable syntax for lambda parameters (var)
27+
System.out.println("\nUsing 'var' in lambda parameters:");
28+
29+
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
30+
names.forEach((var name) -> System.out.println("Hello, " + name));
31+
32+
// BiFunction with var in lambda
33+
BiFunction<Integer, Integer, Integer> add = (var x, var y) -> x + y;
34+
int result = add.apply(10, 20);
35+
System.out.println("Sum using BiFunction with var: " + result);
36+
}
37+
}

0 commit comments

Comments
 (0)