Posts

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Example 1: Java 8 program to print odd numbers from a List   import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; /*Java 8 Program to find Odd Numbers from a List*/ public class DriverClass { public static void main( String [] args) { List < Integer > numbers = Arrays. asList( 1 , 4 , 8 , 40 , 11 , 22 , 33 , 99 ); List < Integer > oddNumbers = numbers.stream(). filter(o -> o % 2 != 0 ). collect(Collectors.toList()); System.out.println(oddNumbers); } } 1. Initialize List using Arrays.asList() method. Java's built-in Arrays.asList() method converts an array into a list. This method takes an array as an argument and returns a List object. 2.  Java List interface provides stream() method which returns a sequential Stream with list of Integer as its source. 3. The Java stream filter() method allow

Spring Boot Google Cloud Logging example

Image
In this section,  we will deploy a Spring Boot application in Google Cloud App Engine, and  we will introduce how to write logs to Google Cloud Logging and how to check the logs in the Logs Explorer. 1. Creating a simple spring boot web application First, open the Spring initializr  https://start.spring.io/   Then, Provide the Group and Artifact name. We have provided Group name  com.knf.dev.demo  and Artifact  spring-boot-gcp-logging-example . Here I selected the  Maven  project - language  Java 11  -  Spring Boot  2.7.9  and  add  Spring web dependency,  and GCP Support . Then, click on the Generate button. When we click on the Generate button, it starts packing the project in a .zip( spring-boot-gcp-logging-example ) file and downloads the project. Then, Extract the Zip file.  Then, import the project on your favourite IDE. Final Project directory: In the pom.xml, add Stackdriver Logging Starter: <!-- Starter for Stackriver Logging --> < dependency > < groupId >