Skip to main content

Posts

Selection sort Implementation

  Selection sort  : In selection sort we will divide the Array in 2 parts , sorted and unsorted and select the min element from unsorted and will copy to sorted array.      Time Complexity for selection sort also is O(n^2).      space complexity for bubble sort will be O(1). class SelectionSort { public static void sort ( int [] array) { int n = array. length ; for ( int i = 0 ; i < n - 1 ; i++) { int min = i ; for ( int j = i + 1 ; j < n ; j++) { if (array[j] < array[min]) { min = j ; } } swap (array , i , min) ; } } private static void swap ( int [] array , int i , int j) { int temp = array[i] ; array[i] = array[j] ; array[j] = temp ; } private static void print ( int [] array) { System. out .print( "[" ) ; for ( int i = 0 ; i < array. length ; i++) { System

Singly Linked List

  Single Linked List :  In Single link list each node will have 2 parts , first part will have Data while next part will have a pointer which will keep the address of its next node, while node itself will not be aware of anything apart from its next node address. Below is sample program without Tail. In this program appending an element to link list will have time complexity as O(n) as we need to travers till end of the link list to find out the last element in link list. But if we will use Tail node along with Head then time complexity for adding an element will be O(n) but still insertion of a node after a specific node will be O(n). Sample program without Tail node. import java.io.IOException ; import java.util.Scanner ; class Node { Node next = null; int data ; public Node ( int val) { data = val ; } } class LinkList { Node head = null; public void append ( int val) { if ( head == null ) { head = new Node(val) ; }

Spring Boot Annotations

Spring Boot Annotations :   As explained in my previous post Spring uses conditional interface to do auto configuration , below are the most common annotation used in Spring boot application. We will try to understand these annotations by developing a spring boot application from scratch. We are going to create a restful webservice using spring Boot. Generally there can be at least six or more packages as below : Configuration Controllers Services DAO Entities Utilities 1. Configuration ( To Enable Web security , Database Configuration , MQ Configuration ) To do so we will create our custom configuration class and annotate with @Configuration and for properties from configuration file @ ConfigurationProperties("nameOfPropertiesIdentfier") Inside that we will set all the required properties inside the bean of Respective configuration for example in case of Database , DataSource , JdbcTemplate etc. First we need to put the below dependency then we need create a configurat

Spring Boot Overview

What is Spring ?  Spring Framework is an application framework and inversion of control container (IOC).  What is inversion of control ? By name only you can predict that it inverses the control , but question is control of what ? And the answer to this is creation of Beans. To understand this lets understand Association which is of 2 types 1)Aggregation and 2) Composition In Aggregation we will have instances of one class to other class and in Composition also its same, only difference is that in composition it will be more dependent to each other while in Aggregation both classes will be independent .  But in both the scenarios they need instances of each other. Let's consider below example before Spring how we used to associate a class into another class. class Student{ String name ; String id ; Address Address = new Address() ; int mobile ; } class Address{ int houseNumber ; String roadName ; String landMark ; String district ; Strin

JAVA 8 STREAM

 JAVA 8 STREAM : Stream is in util package of java, introduced in Java 8. Stream on data list is kind of traversing on it, we can't store the streaming data or do modification on it.   We can only apply our customised transformation and condition check to achieve our desired data out of it. That's the reason there is a common interview questions: What are ternary operators in java stream? . But before coming to this question let me tell you why this question is important , Stream API works in Lazy loading style , it means when there will be a ternary operator then only it will start the stream and apply intermediate logics like map(), filter etc and will return you a List , Set or any primitives value.  Lets consider below example to prove lazy loading: List<String> list= new ArrayList<>() ; list.add( "Neeraj" ) ; list.add( "Chopra" ) ; list.add( "Champion" ) ; list.stream().filter(e -> { System. out .println(e) ;return e.len

JAVA 8 FUNTIONAL INTERFACE

JAVA 8 FUNTIONAL INTERFACE  Funtional Interface :     An interface which has only one abstract method can be called a funtional interface. Comparable , Runnable , Callable all these interfaces has only one abstract method and can be consider as funtional interface.    How it works :        Once we will create a interface with one abstract method then java internally predicates the input type and based on the Interface reference it apply the logic mentioned after lambda expression    lets consider we have created an interface as below @FunctionalInterface public interface FuntionalExample { public int cal ( int a , int b) ; } And below class to test our funtional Interface. public class FuntionalInterfaceExample { public static void main (String args[]) { FuntionalExample addition=( int a , int b) -> a+b ; FuntionalExample subtraction=( int a , int b) -> a-b ; FuntionalExample multiplication=( int a , int b) -> a*b ; Funtional

JAVA MEMORY LEAK, UTILISATION AND MONITORING USING JFR using Mission Control

JAVA MEMORY LEAK, UTILISATION AND MONITORING USING JFR using Mission Control Java flight recording(JFR) help us to analyse and find the root cause of any slowness in our program along with CPU usage , hot methods and garbage collection , profiling etc. To visualise we need to feed .jfr file to JDK mission control present in JDK bin folder. After successful compilation , we should run the program with below option which will generate the .jfr and feed to mission control.   command :  j ava -XX:+UnlockCommercialFeatures -XX:+FlightRecorder  -XX:StartFlightRecording=duration=200s,filename=flight.jfr -cp ./out/ path-and-class-name Below are some example to understand how this JFR can be helpful. 1. Lets consider we have created a java program in which we have used LinkList to store the elements and in same program we are using contains method inside a for loop of 1 million , in this case each time this contains method will be called then 1 million records will be scanned each tim