Search results for: Java

How to declare an array in Java? Print array elements in Java Split a comma-separated String in Java How to break out of the loop in Java? Check if Array contains a value in Java Convert InputStream to a String in Java Find duplicate elements in an Array in Java Escape double quotes in Java…

Read More Java Example Programs

In this tutorial, we are going to see how we can throw custom exceptions with a custom payload in a Spring Boot application and deploy it on AWS as a serverless Lambda function. Let us start by creating a simple Spring boot project. In this project, we are going to create a Request Handler that…

Read More Handle Custom JAVA Exceptions in AWS Lambda with Spring Boot

class CheckIfArrayContainsValue { public static void main(String[] args) { String[] programmingLanguages = {“Python”, “Kotlin”, “Ruby”, “JavaScript”, “C#”, “Java”, “Flutter”}; for (String lang : programmingLanguages) { if (lang.equals(“Java”)) { System.out.println(“It does contain!”); break; // value found, exit the loop } } } } Output: It does contain!   Here, we are iterating over the array, and…

Read More Check if Array Contains a Value in Java

Output: Some String   Here, we put the creation of the Scanner object into a try-with-resources because we want to close the Scanner after it reads all the data. We passed the “\\A” to tell the Scanner to read the entire input stream. import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; class ConvertInputStreamToString { /** *…

Read More Convert InputStream to a String in Java

In this tutorial, we will explore different methods to find duplicate elements in a Java array, including using nested loops, sets, streams, and hash tables. We will also discuss the time and space complexity of each method, so you can choose the most efficient one for your specific use case. Whether you’re a beginner or…

Read More How to Find Duplicate Elements in a Java Array

class EscapeDoubleQuotesInString { public static void main(String[] args) { String str = “Hello, I’m \”Steve\” and I am \”Java\” developer”; System.out.println(str); } } Output: Hello, I’m “Steve” and I am “Java” developer Escape double quotes in a JSON object in Java There are many cases where you might need to escape double quotes. In this…

Read More Escape Double Quotes in Java String

<dependency> <groupid>org.apache.poi</groupid> <artifactid>poi-ooxml</artifactid> <version>5.0.0</version> </dependency> If you are not familiar with Maven, you can check out this tutorial: Create Java Project with Maven. import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Product { private int id; private String color; private String size; private int cost; private String description; } import org.apache.poi.ss.usermodel.Cell;…

Read More Read and write Excel files in Java

This is a Java JSON tutorial. Here, you will learn what JSON is and how to work with JSON format in Java. You will see where to find the required JSON libraries and parse Java Objects to JSON and vice versa. Let’s get started. What is JSON? JSON (JavaScript Object Notation) is a lightweight text…

Read More Java JSON tutorial

In this tutorial, we are going to see how to handle exceptions from a Lambda function in the API Gateway. For Lambda exceptions, we have to map the error status returned by the Lamba functions to the HTTP Status of the methods in API Gateway. If not, the API Gateway returns a 200 OK status…

Read More Handling Exceptions from AWS Lambda written in JAVA

class ReactiveJavaTutorial { public static void main(String[] args) throws InterruptedException { // Create a Flux Flux<String> flux = Flux.just(“data1”, “data2”, “data3”); // Subscribe to a Flux and print the elements flux.subscribe(System.out::println); } } Output: data1 data2 data3   When we call the subscribe(), we are telling the Publisher to start sending data. class ReactiveJavaTutorial {…

Read More Subscribe to Flux in Java Reactor

import reactor.core.publisher.Flux; class ReactiveJavaTutorial { public static void main(String[] args) throws InterruptedException { // create an empty Flux Flux flux1 = Flux.just(); // create a Flux that will hold only one value Flux<String> flux2 = Flux.just(“data”); // create a Flux that will hold multiple values Flux<String> flux3 = Flux.just(“data1”, “data2”, “data3”); } } import reactor.core.publisher.Flux;…

Read More Create a Flux in Java Reactor

import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { String dataFromMono = getMono().block(); System.out.println(“Data from Mono: ” + dataFromMono); } private static Mono getMono() { return Mono.fromSupplier(() -> { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } return “Hello!”; }); } } Output: Data from Mono: Hello! class ReactiveJavaTutorial { public…

Read More Extract Data from Mono in Java

import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { // create a Mono Mono<String> mono = Mono.just(“Hello”); // subscribe to a Mono mono.subscribe(); } } import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { // create a Mono Mono<String> mono = Mono.just(“Hello”); // subscribe to a Mono mono.subscribe(data -> System.out.println(data)); }…

Read More Subscribe to a Mono in Java Reactor

public static <T> Mono<T> just(T data) import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { Mono<String> mono = Mono.just(“Hello”); } } import org.reactivestreams.Publisher; import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { Publisher<String> mono = Mono.just(“Hello”); } } import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { // will…

Read More Create a Mono in Java Reactor