Posts

Java - Swap Two Numbers

In this post, you will learn how to code Java Program to Swap Two Numbers using 2 different methods. 1. Using third variable 2. Without using third variable Java Program to Swap Two Numbers Method 1 : using third variable import java.io.BufferedReader ; import java.io.IOException ; import java.io.InputStreamReader ; public class Main { public static void main (String []args) throws IOException { InputStreamReader isr = new InputStreamReader(System. in ) ; BufferedReader br = new BufferedReader(isr) ; System. out .println( "Enter first number: " ) ; int firstNumber = Integer. parseInt (br.readLine()) ; System. out .println( "Enter second number: " ) ; int secondNumber = Integer. parseInt (br.readLine()) ; //Before Swapping two numbers System. out .println( "Before Swapping :" ) ; System. out .println( "First number is " +firstNumber) ; System. out .println( "S

Java 8 Streams - Count Frequency Of Words In a List

Image
Example 1   import java.util.Arrays ; import java.util.List ; import java.util.Map ; import java.util.function.Function ; import java.util.stream.Collectors ; public class Main { public static void main ( String [] args) { List < String > words = Arrays . asList ( "cat" , "rat" , "bat" , "cow" , "cat" , "bat" ); // For Long values Map < String , Long > result = words .stream() .collect( Collectors . groupingBy ( Function . identity (), Collectors . counting ())); System . out .println( result ); } } The List interface in Java provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. The asList () method of java.util.Arrays class is used to return a fixed-size list backed by the specified array. This metho

Java 8 Streams - Program To Break A List Into Batches Of Given Size

Image
In this post, we will show how to break a list into batches of a given size. Example 1: Convert List of Characters into batches of a given size import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; public class Main { public static void main( String [] args) { List < Character > list = Arrays .asList( 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' ); int chunkSize = 3 ; AtomicInteger ai = new AtomicInteger(); Collection < List < Character >> chunkedOrders = list.stream() .collect(Collectors.groupingBy(item -> ai .getAndIncrement() / chunkSize)) .values(); chun

Spring Boot, Thymeleaf - File Upload, Download & Delete - Example

Image
Hello everyone, Hope you are doing well. In this post,  will learn how to upload, download, and delete the file with Spring Boot and Thymeleaf. You could download the source code from our GitHub repository. Backend Project Directory Maven [Pom.xml] A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details utilized by Maven to build the project.  < project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns: xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi :schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > < modelVersion >4.0.0</ modelVersion > < groupId >com.knf.dev.demo</ groupId > < artifactId >spring-thymeleaf-file-upload-download</ artifactId > < version >1.0</ version > < parent > < groupId >or

Generate Qr Code in Spring boot + Thymeleaf application with ZXing - Example

Image
Hello everyone, today we will learn how to generate the  Qr Code in Spring boot + Thymeleaf  sample application with ZXing . Technology Used JDK 17 Spring Boot 2.7.0 Thymeleaf ZXing library 3.5.0 Maven Final Project Directory Maven[Pom.xml] ZXing Core:  Core barcode encoding/decoding library ZXing Java SE extensions:  Java SE-specific extensions to the core ZXing library <? xml version ="1.0" encoding ="UTF-8" ?> < project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion >4.0.0</ modelVersion > < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >2.7.0</ version > < relativePath /> </ parent &