Posts

Showing posts from August, 2024

Functional interface in java8.

Image
            Interface is the one which has only abstract methods. It has only declaration. It implements the concept of abstraction. Functional interfaces are important concept in java 8. What is functional interface??               Functional interface has only one abstract method. This uses lambda expressions to implement the method. Built in package: java.util.function Common functional interfaces: The below functional interfaces are commonly used from java SE1.8 onwards. ActionListener, Runnable, Callable, Comparable. Simple program to implement functional interface using Runnable: class Test {     public static void main(String args[])     {       Runnable runnable = () -> System.out.println("A threat is running separately");       new Thread(runnable).start();  ...

how to use Lambda expressions in java?

Image
               Java 8 has an important functionality called lambda expressions. It involves functional interfaces. Functional interfaces are the one which has single abstract method. Syntax of the lambda expression is given below. (parameters) -> body Where Parameters – It holds the input value. è -> - It is the one which separates the parameters and body. Body – It is the code to be executed. Eg: The parameters may be single or multiple in number. (a)-> a+a (a,b)-> a-b (a,b,c)-> a+b+c How to use lambda expressions???               Generally, Lambda expressions are used to pass arguments to functions. It can be stored as variables or method parameters. But the type should be an interface of single method. Java Program to use lambda expressions: è In the beginning, create an interface SumInterface. It is a functional interface which...

How to write a java program to print pattern???

Image
       Pattern is an arrangement of elements.  It may be a mathematical shape like square, rectangle, triangle.  These shapes may be printed as hollow pattern, solid pattern or number pattern. In this blog post, we create three different patterns.    Solid Square Pattern    Hollow Rectangle pattern    Number Triangle pattern   1.How to print Solid Square pattern in java: ·        This java program uses “#” pattern to print solid square. For printing this pattern, a function “printIt()” is written. ·        This function has two for loops. One for row and another one for column value. Based on the loops, it prints the pattern. ·        In the main function, row and column value is assigned and the printIt() is called using the row and column value. #Java Program to print Solid square pattern   public class Squa...

Java program to implement in order traversal in binary tree:

Image
              Binary tree is a data structure which stores data in tree structure. It has a root with two child(Left and right child). Root is a parent element. In order Traversal:               It follows a simple pattern for traversal (Left -Root -Right).   In the beginning, traverse a left sub tree followed by root node. Finally, Right sub tree is traversed. Java program to implement in order traversal in binary tree: ·        First, create a binary tree. It needs a node to be defined. So, create a class “node” with three elements(Left and right nodes and key). ·        Next, create a member function Node. Assign the value to key. Left and right values as null. ·        Develop a BinaryTree class. Create a object for Node. Initialise the constructor. ·     ...

Java program to calculate the execution time of methods:

Image
       Execution is the final part in coding. It loads and links the object code into memory. While executing a code, it is important to measure the execution time. There are many methods to measure the execution time           1.    Using nanoTime()         2.     Using currentTimeMillis()         3.     Using Instant and Duration 1.Using nanoTime():               Here, we  use nanoTime() built in method. This program gets two inputs as starting time and ending time using System.nanoTime(). Find the difference and store it in a variable. Print the variable. public class nano {     public static void main(String[] args) {         // Reads the system time. let it be the starting time.         long sTime = System.nanoTime(); ...

How to pass a lambda expression as a method argument in java??

Image
                  Lambda expression is introduced in Java SE8. It is used in functional interfaces. Functional interface has a single abstraction method. This is a block of code that doesn’t belong to any class. Features:    The functionality can be a method. ·        The method is independent one. ·        This function is used anywhere whenever the program needs. How to pass a lambda expression as a method argument in java:               It can be done many ways. Generally, we follow two methods. 1.       1.  Using Runnable 2.        2. Using custom functional interface 1.Using Runnable :        This method starts   from creating a class “Main3” and save the program as “Main3.java”. In the public static void main(), ...

How to write a java program to implement merge sort??

Image
               Sorting. It is a way of arranging data elements. There are many algorithms to perform sorting. One of the efficient algorithms is merge sort. Who invented merge sort? John von Neumann invented merge sort in 1945. How the merge sort algorithm works?               This algorithm uses divide and conquer concept. ·        First, divide the entire list into sub lists. Each sub list should contain at least one element. ·        Next, check each sub list and sort it separately. Repeat this process for all sub lists. ·        Finally, all the sorted sub lists are merged. Here, is the java program to implement merge sort. //Java Program to implement merge sort. import java.util.Arrays; public class MergeSort {       void merge(int array[], i...

Java Program to convert a stack trace to a string using StringWriter and PrintWriter:

Image
               Whenever a program is written, it should be compiled and executed. Both of this process makes use of memory. During the time of execution, stack trace is generated. Stack trace is useful for debugging purpose. ·        Let us create a java program to display stack trace.   It uses dumpStack() built in function. ·        First, create a class Main1 and include the function calls demo(),demo1(),demo2() ,demo3() and demo4(). ·        Add the built in function call Thread.dumpStack() in demo4() . public class Main1 {   public static void main (String args[]) {     demo();   }   static void demo() {     demo1();   }   static void demo1() {     demo2();   }   static void demo2() {     demo3();   }   static v...