How to Declare an Array in Java?
dataType[] arr; Where: int[] intArray = new int[5]; String[] arr = new String[3]; arr[0] = “value 1”; arr[1] = “value 2”; arr[2] = “value 3”;
Read More How to Declare an Array in Java?dataType[] arr; Where: int[] intArray = new int[5]; String[] arr = new String[3]; arr[0] = “value 1”; arr[1] = “value 2”; arr[2] = “value 3”;
Read More How to Declare an Array in Java?synchronized void method() {} public class SynchronizedInstanceMethodExample implements Runnable { public static void main(String[] args) { SynchronizedInstanceMethodExample runnable = new SynchronizedInstanceMethodExample(); new Thread(runnable).start(); // creates one thread new Thread(runnable).start(); // creates second thread } // Inherited run method from the Runnable interface @Override public void run() { try { // Calling the non-synchronized method print()…
Read More Java Synchronized Blocks and Methodsclass PrintArrayElementsExample { public static void main(String[] args) { int[] arr1 = {1, 7, 9, 5, 2, 8, 3}; String[] arr2 = {“Megan”, “Tom”, “Melissa”, “John”, “Steve”}; // print elements of the arr1 System.out.println(Arrays.toString(arr1)); // print elements of the arr2 System.out.println(Arrays.toString(arr2)); } } Output: [1, 7, 9, 5, 2, 8, 3] [Megan, Tom, Melissa, John,…
Read More Print Array Elements in Javapublic class SplitString { public static void main(String[] args) { String names = “Tom,Steve,John,Megan,Melissa”; // split string String[] arr = names.split(“,”); // print the size of the array System.out.println(arr.length); // print the elements Stream.of(arr).forEach(System.out::println); } } Output: 5 Tom Steve John Megan Melissa If you need a list, instead of an array, use the…
Read More Split a comma-separated String in Javaimport org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @SpringBootApplication public class ScheduledDemoApplication { public static void main(String[] args) { SpringApplication.run(ScheduledDemoApplication.class, args); } } import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduledTaskService { @Scheduled(fixedRate = 5000) public void execute() { // some logic that will be executed on a schedule System.out.println(“Code is being executed…”); } } Output:…
Read More How to Schedule a Task in Spring Bootsome data in a file import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.util.FileCopyUtils; import java.io.*; import java.nio.charset.StandardCharsets; @SpringBootApplication public class DemoApplication { public static void main(String[] args) throws IOException { SpringApplication.run(DemoApplication.class, args); readFile(); } public static void readFile() throws IOException { // read a file Resource resource = new ClassPathResource(“classpath:data.txt”); // get inputStream…
Read More Read a File From the Resources Folder in Spring Bootclass BreakOutOfLoop { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5}; // iterate over array for (int i = 0; i < arr.length; i++) { if (arr[i] == 3) { // break out of the loop if the current value is equals to 3 System.out.println(“Breaking out of the loop!”);…
Read More How to break out of the loop in Java?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 JavaOutput: 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 Javaclass FindDuplicateElements { public static void main(String[] args) { int[] array = new int[]{2, 4, 7, 2, 11, 5, 7, 14, 22, 11, 49, 58, 14, 101, 1, 3, 205, 49, 101, 12}; for (int i = 0; i < array.length; i++) { // outer loop for (int j = i + 1; j <…
Read More Find duplicate elements in an Array in Javaclass 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<dependency> <groupid>io.projectreactor</groupid> <artifactid>reactor-test</artifactid> <version>3.1.0.RELEASE</version> <scope>test</scope> </dependency> class ReactiveJavaTutorialTest { @Test public void testFlux() { //Create a Flux Flux <Integer> fluxToTest = Flux.just(1, 2, 3, 4, 5); // Create a test StepVerifier.create(fluxToTest) .expectNext(1) .expectNext(2) .expectNext(3) .expectNext(4) .expectNext(5) .expectComplete() // we expect Flux to complete after sending the number 5 which is the last element .verify(); }…
Read More Testing with StepVerifier in Project Reactorclass ReactiveJavaTutorial { public static void main(String[] args) { Flux<String> cities = Flux.fromIterable( new ArrayList<>(Arrays.asList(“New York”, “London”, “Paris”, “Toronto”, “Rome”))); cities.log().subscribe(); } } Output: INFO 6832 — [ main] reactor.Flux.Iterable.1 : | onSubscribe([Synchronous Fuseable] FluxIterable.IterableSubscription) INFO 6832 — [ main] reactor.Flux.Iterable.1 : | request(unbounded) INFO 6832 — [ main] reactor.Flux.Iterable.1 : | onNext(New York) INFO…
Read More Implementing Backpressure in Project Reactorclass ReactiveJavaTutorial { public static void main(String[] args) { Flux<String> cities = Flux.just(“New York”, “London”, “Paris”, “Amsterdam”) .map(String::toUpperCase) .filter(cityName -> cityName.length() <= 8) .map(cityName -> cityName.concat(” City”)) .log(); cities.subscribe(); } Output: INFO 14040 — [main] reactor.Flux.MapFuseable.1 : | onSubscribe([Fuseable] FluxMapFuseable.MapFuseableSubscriber) INFO 14040 — [main] reactor.Flux.MapFuseable.1 : | request(unbounded) INFO 14040 — [main] reactor.Flux.MapFuseable.1 : |…
Read More subscribeOn and publishOn operators in Project Reactor