Posts

Functional Programming

Image
Functional programming is a software development paradigm out of various available software development methodology. It is based on function which is nothing but a computation unit and it's result depends on the argument and variable defined in the scope. In computer program, a functional programming treats every computation as an evaluation of mathematical functions and never use mutable data to change it's state. The output value of a pure function depends on the arguments which are input to the function and will produce the same result every time for the same set of inputs.  It means f(x) will always produce the same result y for same value of x. A function is full of functions or declarations instead of statements. Languages: Scala, Python, Erlang, Haskell etc. Various concepts of functional programming : Higher Order Functions First Class Function  Pure Function Immutability     

Java Stream Coding Interview Questions and Solutions

Image
Java Stream API introduced in Java 8 and use functional way (lambda function f(x) -> y) to process a collection. It operates over a source data and returns a transformed immutable object. It is one of the most frequently asked interview topic. If you're preparing for Java developer roles, make sure to go through the below consolidated and compiled coding question and answers to bookmark and practice.   Q. Convert a collection of string to upper case : List<String> list = Arrays . asList ( "Bob" , "Piter" , "Andi" , "Joy" ); List<String> listUpper = list .stream() .map( String::toUpperCase ) .collect(Collectors. toList ()); System. out .println( "listUpper - " +listUpper); Output: listUpper - [BOB, PITER, ANDI, JOY] Q. Find Max value from a collection of integer : List<Integer> listInt = Arrays . asList ( 4 , 5 , 7 , 3 , 23 , 89 ); Optional<Integer> value = listInt ...

Null check of Object and Object Chain in Java

Image
JDK 7 and earlier: Avoid using null check in if statement. Use ternary operator for null/exist check and assign a default value before using. This will make the code clear and more understandable as comparable to using multiple if, else   statements. JDK 8 and above : Java-8 provides improved API to deal with object null check. Use isPresent() function available in Java.util.Optional interface for null check of single object. Ex – Employee employee = new Employee(); employee .setName( "Pramoda" ); // Getting optional value Optional<Employee> optionalEmployee = Optional. ofNullable ( employee ) .map(Root::getEmployee); System. out .println( "Is Employee present : + optionalemployee .isPresent()); Use Java.util.Optional functional interface for object null check and assign default value to avoid null pointer exception. Ex – Employee employee = new Employee(); employee .setNa...