Wednesday, 20 July 2022

What is Predicate in JAVA 8 ? Mphasis Interview Question-2022

 Predicate in JAVA 8:

A predicate is a built in functional interface present in java.util.function package. It is specifically used for code management and unit testing. It contains many built in methods which is as follows:

test(T t): It evaluates the predicate on the value passed to this function as the argument and returning a boolean value(true / false).

TestExample.java

import java.util.function.Predicate; 


class TestExample

{

public static void main(String[] args)

{

Predicate<Integer> greater_than = x -> (x > 100);  

  

        // calling test method of the predicate

        System.out.println(greater_than.test(200));  

    } 



Thursday, 2 April 2020

What is dependency injection in JAVA spring framework ?

Spring is an open source, light weight, and non-invasive framework which is specifically used to develop web and enterprise application. 

Dependency Injection is a process where the spring framework injecting dependency to a dependent object.

Dependency Injection is a software design pattern that implements inversion of control for resolving dependencies.




Our Services:
JAVA training ||  JAVA projects  ||  JAVA Internship  || Python training  || AI & Machine Learning training  ||  Data Science training

Call us: 0674-2361252

Tuesday, 23 July 2019

How to kill a thread in JAVA ??

Ans:

'Killing a thread' is not the right phrase to use. Here is one way we can implement graceful exit of the thread:
class TaskThread implements Runnable {

    boolean shouldStop;

    public TaskThread(boolean shouldStop) {
        this.shouldStop = shouldStop;
    }

    @Override
    public void run() {

        System.out.println("Thread has started");

        while (!shouldStop) {
            // do something
        }

        System.out.println("Thread has ended");

    }
public void stop() {
        shouldStop = true;
    }


}
public class ThreadStop
{
public static void main(String[] args)
{
System.out.println("Start");

// Start the thread
TaskThread task = new TaskThread(false);
Thread t = new Thread(task);
t.start();

  // Stop the thread
task.stop();

  System.out.println("End");

}
}

Friday, 5 July 2019

JAVA StringBuffer and StringBuilder object conversion to String


We can convert a StringBuffer or StringBuilder object  to String by using toString() method of Object class.
StringConversion.java
class StringConversion
{
          public static void main(String[] args)
          {
                   StringBuffer sb1=new StringBuffer("Silan");
                   StringBuilder sb2=new StringBuilder("Technology");
                   String s1=sb1.toString();
                   String s2=sb2.toString();
                   System.out.println(s1+" "+s2);
          }
}
Output:
Silan Technology


StringConversion1.java
class StringConversion1
{
          public static void main(String[] args)
          {
                   StringBuffer sb1=new StringBuffer("Silan");
                   StringBuffer sb2=new StringBuffer("Silan");
                   String s1=sb1.toString();
                   String s2=sb2.toString();
                   if(s1.equals(s2))
                   {
                             System.out.println("s1 and s2 contents are equal");
                   }
                   else
                   {
                             System.out.println("s1 and s2 contents are not equal");
                   }
          }
}
Output:
s1 and s2 contents are equal

Saturday, 9 February 2019

JAVA : How to generate OTP


OtpExample.java
package java8s;

import java.util.*;

public class OtpExample {
    
     static char[] show(int len)
    { 
        System.out.print("Your OTP is : ");
 
        String numbers = "0123456789";
 
        Random rd = new Random();
 
        char[] otp = new char[len];
 
        for (int i = 0; i < len; i++)
        {
            
            otp[i] =
             numbers.charAt(rd.nextInt(numbers.length()));
        }
        return otp;
    }

     public static void main(String[] args) {
          
           int length = 5;
        System.out.println(show(length));

     }

}

Output:
Your OTP is : 35838

Thanks & Regards:
Trilochan Tarai
SILAN Technology & java8s.com
+91-9658317757

JAVA : How to generate password


//JAVA source code to generate random password
PasswordExample.java
package java8s;

import java.util.*;
public class PasswordExample {

     public static void main(String[] args) {
          
           int length = 10;
        System.out.println(silanPassword(length));
          

     }
    
     static char[] silanPassword(int len)
    {
        System.out.print("Your new password is : ");
 
       
        String uchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String lchars = "abcdefghijklmnopqrstuvwxyz";
        String numbers = "0123456789";
        String symbols = "!@#$%^&*_=+-/.?<>)";
 
 
        String values = uchars + lchars +
                        numbers + symbols;
 
        
        Random rd = new Random();
 
        char[] password = new char[len];
 
        for (int i = 0; i < len; i++)
        {
           
            password[i] =
              values.charAt(rd.nextInt(values.length()));
 
        }
        return password;
    }

}
Output:
Your new password is : mR!XPdfVmg

Note : Here we have used random() to generate the password. It may change every time due to use of random() method.

Thanks & Regards:'
Trilochan Tarai
SILAN Technology & java8s.com
0674-2361252

Monday, 17 December 2018

JAVA ArrayList Constructors

JAVA ArrayList Constructors


                 java.util.AbstractList
                          java.util.ArrayList

public class ArrayList
extends AbstractList
1.    ArrayList() : Create an empty ArrayList object with default initial capacity 10. Once ArrayList reaches its max capacity then new ArrayList will be created with a new capacity.
new capacity=(current capacity*3/2)+1
i.e (10*3/2)+1=16 
after this new ArrayList is created the previous one will be deleted and it will be treated as garbage.

2.    ArrayList(int initialcapacity) : Constructs an empty ArrayList with the specified initial capacity given by the user.
3.    ArrayList(Collection c) : For every Collection object, we can create an equivalent ArrayList.
ArrayListExample1.java
importjava.util.*;
classArrayListExample1
{
    public static void main(String[] args)
    {
            ArrayList al=new ArrayList();
            al.add("Sashi");
            al.add("Satyajit");
            al.add(20);
            al.add(null);
            System.out.println(al);          //[Sashi,Satyajit,20,null]
            al.remove(1);
           al.add(1,"Sasmita");
           al.add("Nilan");
           System.out.println(al);           //[Sashi,Sasmita,20,null,Nilan]
      }
}
Output
[Sashi,Satyajit,20,null]
[Sashi,Sasmita,20,null,Nilan]


Thanks & Regards:
Trilochan Tarai
SILAN Technology
java8s.com
0674-2361252