Binary search tree

Generally the tree have one root node and child node, each child node can have leaf nodes.



How elements will insert in a tree ?
 Assume your going to insert a element 10 in the tree.
 As there are no elements it will insert as root node.
Then again if we try to insert element 20, it will insert right side of tree because 20  is greater then 10.
What if element is less then 10 ? - It goes to left side of tree.
Finally the tree will look like this.

Java SHA-256

Secure Hash Algorithm-256:
Most of the cryptographic hash functions runs on the digital data.
Using SHA-256 algorithm we can determine the data integrity.

 For example, computing the hash of a downloaded file and comparing the result to a previously published hash result can show whether the download has been modified or tampered with some other content.

In addition, cryptographic hash functions are extremely collision-resistant.

Cryptographic hash functions:
   It takes input of any length and gives us an output in fixed length.

Collision-resistant ?
In other words, it should be extremely difficult to produce the same hash output from two different input values using a cryptographic hash function.

This is the example to print the SHA-256 value for given string:

import java.io.*;
import java.util.*;
import java.security.*;
import java.math.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        try{
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        System.out.printf("%064x\n", new BigInteger(1, md.digest(s.getBytes())));
        }catch(NoSuchAlgorithmException e){
            //Handle exception
        }
    }
}

Also, It consists of six identical hashing algorithms(i.e., SHA-256, SHA-512, SHA-224, SHA-384, SHA-512/224, SHA-512/256) with a variable digest size.

SHA-256 is a 256-bit ( 32 byte) hashing algorithm which can calculate a hash code for an input of up to 264-1 bits. It undergoes 64 rounds of hashing and calculates a hash code that is a 64-digit hexadecimal number.

Source: https://www.hackerrank.com

Java MD5

 Message Digest algorithm version 5:

MD5 is a widely-used cryptographic hash function with a -bit hash value. Here are some common uses for MD5:
  • To store a one-way hash of a password.
  • To provide some assurance that a transferred file has arrived intact.
Note: It has been compromised using Flame malware in 2012.

This is the example to encrypt the alphanumeric string in java.

We have package named security in java(Check here).

Below code take the string as input and encrypt using MD5 algorithm.
import java.io.*;
import java.util.*;
import java.security.*;
import java.math.BigInteger;

public class MleClass {

    public static void main(String[] args) {
     
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        try{
        MessageDigest md=  MessageDigest.getInstance("MD5");
        
        System.out.printf("%032x\n", new BigInteger(1, md.digest(s.getBytes())));
        }catch(NoSuchAlgorithmException e){
            // Handle exceptions
        }
        
    }
}


Method reference.

Before reading this I would like to suggest to go through Lamda expressions

We have seen the how to pass the behaviour as parameter.
In java8 we can pass method reference as argument.
It's not different,  same as Lambda exprssion but syntax is little change.

With lambda expression we will write like this.

interface MleInterface{
    void show();
}
 
 
public class HelloWorld{
 
  void showMsg( MleInterface mle){
      // Write some logic
      mle.show();
  }
     public static void main(String []args){
         HelloWorld  obj = new HelloWorld();
         /* Simplified Lambda expression.  */
         obj.showMsg(() -> System.out.println("I'm from Lambda expression"));
     }
}

Same we can write with method reference like below.

interface MleInterface{
    void show();
}
 
public class HelloWorld{
  void showMsg( MleInterface mle){
      // Write some logic
      System.out.println("Show method");
      mle.show();
  }
  public static void printMsg(){
      System.out.println("I'm from printMsg");
  }
  public static void main(String []args){
         HelloWorld  obj = new HelloWorld();
         /* Passing method reference.
           This way new syntax in java8.
          */
         obj.showMsg(HelloWorld :: printMsg);
     }
}


Output: Show method
I'm from printMsg

Lambda expressions

 All we know Lambda expressions are new concept in Java 8.
Why we need it ?
  1. Enable the functional program.
    Really do we need functional programming in java.
    We have been writing the millions of line java code  with OOPS concepts.
    Nothing new to do with functional programing, whatever we have been doing with OOPS same we can achieve with functional programing.
    Why we need the new functional  programming feature in JAVA ?
      - It's readable and maintainable.
      When we are working on client project requirement makes us write millions of lines java code

    It' hard to maintain the complex code.
    So, functional programing came in to the picture to improve the code readability and maintain it in elegant way.


    How can we improve coding style with functional programing in JAVA?