Wednesday 8 November 2023

Java Stream Max

1) Find Maximum number in Array list

import java.util.ArrayList;
import java.util.List;

public class javaStreamExamples {
    List<Integer> list1=Arrays.asList(10,20,35,20,67,10)

    public void testStream(){
        Int max1=list1.stream().max(Integer:: compare).get();

System.out.println(max1);

    }

}

O/p
67


≠=================================

1) Find Minimum number in Array list

import java.util.ArrayList;
import java.util.List;

public class javaStreamExamples {
    List<Integer> list1=Arrays.asList(10,20,35,20,67,10)

    public void testStream(){
        Int min1=list1.stream().max(Comparator.reverseOrder()).get();

System.out.println(min1);

    }

}

O/p
67


≠=================================


Wednesday 13 May 2020

Lambda Expression in JAVA



-> The Lambda expression is used to provide the implementation of an interface which has functional interface.

->  It saves a lot of code.

-> In case of lambda expression, we don't need to define the method again for providing the implementation. Here, we just write the implementation code.

-> Java lambda expression is treated as a function, so compiler does not create .class file.



Functional Interface

-> Lambda expression provides implementation of functional interface.

-> An interface which has only one abstract method is called functional interface.

-> Java provides an anotation @FunctionalInterface, which is used to declare an interface as functional interface.


Syntax

(argument-list) -> {body}


Java lambda expression is consisted of three components.

1) Argument-list: It can be empty or non-empty as well.

2) Arrow-token: It is used to link arguments-list and body of expression.

3) Body: It contains expressions and statements for lambda expression.



Example :  Addition of two numbers using lamda expression

@FunctionalInterface
interface Calc{

   int add(int n1,int n2);

}




public class Calculation{
 
 
    public static void main(String[] args) { 

     
       Calc addition=(n1,n2)->{

          return n1+n2;
       };
   
 
       System.out.println("Addition "+addition.add(10,20)); 


       
 }


}


Output

Addition 30

Comparator Interface in java


-> Comparator used to sorting the object element by using multiple sorting sequence.

-> We can sort the elements on the basis of any data member, for example, rollno, name, age or anything else.

-> The comparator interface provide two methods like compare(Object obj1,Object obj2) and equals(Object obj).



1) compare(Object obj1,Object obj2) function

Its compares first object(obj1) and second object(obj2) and return int value

Syntax : public int compare(Object obj1,Object obj2)


positive integer = if the current object is greater than the specified object.
negative integer = if the current object is less than the specified object.
zero = if the current object is equal to the specified object.



2) equals(Object obj1) function

Its used to compare current object with specified object

Syntax : public boolean equals(Object obj1)




Example : Sorting by name and Emp id by using comparator

import java.util.*; 
import java.io.*;

// User - Defined Class

class Employee { 
int empid; 
String empname; 
int empage; 
Employee(int empid,String empname,int empage){ 
this.empid=empid; 
this.empname=empname; 
this.empage=empage; 

}


class sortByAge implements Comparator{
                         
public int compare(Object o1,Object o2){ 
Employee s1=(Employee)o1; 
Employee s2=(Employee)o2; 

if(s1.empage==s2.empage) 
return 0; 
else if(s1.empage>s2.empage) 
return 1; 
else 
return -1; 

   
}
 

class sortByName implements Comperator{
   
public int compare(Object o1,Object o2){ 
Employee s1=(Employee)o1; 
Employee s2=(Employee)o2; 

return s1.empname.compareTo(s2.empname);
   
}   
}


// Main Class



public class TestSort1{ 
public static void main(String args[]){ 
ArrayList<Employee> al=new ArrayList<Employee>(); 
al.add(new Employee(1,"Ajith",23)); 
al.add(new Employee(6,"Vijay",27)); 
al.add(new Employee(4,"Surya",21)); 
 
 
System.out.println("Sorting By Age"); 
Collections.sort(al, new sortByAge()); 
for(Employee st:al){ 
System.out.println(st.empid+" "+st.empname+" "+st.empage); 
}


System.out.println("Sorting By Name");
Collections.sort(al, new sortByName()); 
for(Employee st:al){ 
System.out.println(st.empid+" "+st.empname+" "+st.empage); 






Output

Sorting By Age

4 Surya 21
1 Ajith 23
6 Vijay 27


Sorting By Name

1 Ajith 23
4 Surya 21
6 Vijay 27

Comparable interface in java



-> Comparable is an interface of comparing the objects with other objects os the same type. This is also called "natural ordering"

-> by using compare interface, we can sort the elements based on single data member only.

-> It provides single method name as compareTo(object) and provides single sorting sequence only.

-> The interface found in java.lang.package


compareTo(Object obj) method

The method used to compare the current object with specified object. Please find the below return values

positive integer = if the current object is greater than the specified object.
negative integer = if the current object is less than the specified object.
zero = if the current object is equal to the specified object.



Example 1: Sort by Integer Value (Ascending Order) by using Comparable

import java.util.*; 

// User - Defined Class

class Employee implements Comparable<Employee>{ 
int empid; 
String empname; 
int empage; 
Employee(int empid,String empname,int empage){ 
this.empid=empid; 
this.empname=empname; 
this.empage=empage; 

 
public int compareTo(Employee st){ 
if(empage==st.empage) 
return 0; 
else if(empage>st.empage) 
return 1; 
else 
return -1; 

}



// Main Class



public class TestSort1{ 
public static void main(String args[]){ 
ArrayList<Employee> al=new ArrayList<Employee>(); 
al.add(new Employee(1,"Ajith",23)); 
al.add(new Employee(6,"Vijay",27)); 
al.add(new Employee(4,"Surya",21)); 
 
Collections.sort(al); 
for(Employee st:al){ 
System.out.println(st.empid+" "+st.empname+" "+st.empage); 







Output

4 Surya 21
1 Ajith 23
6 Vijay 27


********************************************************


Example 2: Sort by Integer Value (Decending Order) by using Comparable


import java.util.*; 

// User - Defined Class

class Employee implements Comparable<Employee>{ 
int empid; 
String empname; 
int empage; 
Employee(int empid,String empname,int empage){ 
this.empid=empid; 
this.empname=empname; 
this.empage=empage; 

 
public int compareTo(Employee st){ 
if(empage==st.empage) 
return 0; 
else if(empage<st.empage) 
return 1; 
else 
return -1; 

}



// Main Class



public class TestSort1{ 
public static void main(String args[]){ 
ArrayList<Employee> al=new ArrayList<Employee>(); 
al.add(new Employee(1,"Ajith",23)); 
al.add(new Employee(6,"Vijay",27)); 
al.add(new Employee(4,"Surya",21)); 
 
Collections.sort(al); 
for(Employee st:al){ 
System.out.println(st.empid+" "+st.empname+" "+st.empage); 







Output


6 Vijay 27
1 Ajith 23
4 Surya 21


Monday 30 March 2020

Difference between Web driver, Remote Web Driver and Chrome Driver


Webdriver

Web driver is interface.
Global for all browsers
The interface provide common methods signature for all browser.
Ex : findElement(), get(),switchTo()

Remote Webdriver

Remote Webdriver is concrete class  implements Webdriver.
The implementation of webdriver method is available  in Remote Webdriver Class.
The Class provide additional  method  which is not available in webdriver. Provide method to run selenium test in remote machine.
ex : getSessionID()


Chrome driver

Chrome Driver is class extends Remote Webdriver.
Only specific to Chrome browser.
The class have implementation for only chrome browser. 

Difference between String, String Builder and String Buffer

Immutable : Cannot change value
Mutable : Can change the Value


String :

 String is Immutable.
 If we update any existing String then it will create String object. It won't override existing value.

Ex : 
String S1="Testing"
print (S1)

String concat(String s1){
S1 = S1+"Java"
}

O/P

S1: Testing


String Builder

String Builder is mutable.
we can change value. if any thing update then it will override existing object itself.

Ex : 
StringBuilder S1="Testing"
print (S1)

String concat(String s1){
S1 = S1.append("Java")
}

O/P

S1: Testing Java


String Buffer

Both String Buffer and String builder same. Except one concept.
String Buffer is thread safe. we can refer the String buffer object via multi thread.

String Buffer is mutable. we can change value. if any thing update then it will override existing object itself.

Ex : 
StringBuffer S1="Testing"
print (S1)

String concat(String s1){
S1 = S1.append("Java")
}

O/P

S1 : Testing Java





When to use

String : if String object value contant entire program.
String Builder : If String object value need to change at any time.
String Buffer : If String object value need to change at any time. It can be access same value via multithread.

Saturday 4 March 2017

How to implement Log in Selenium (Using Log4j)



-> During the Automation Execution users need some information about the Execution steps in console.

-> We need information which helps the users to understand the testcase or any failure during the testcase execution.

-> With the help of Log4j its possible to enable logging in selenium testcase.


Follow the below steps to implement logs


1) Log4j Download


Download Log4j by using this link -> https://logging.apache.org/log4j/1.2/download.html

2) Create Log4j.properties file

-> Log4j.properties file for display log message only in console

# Root logger option
log4j.rootLogger=INFO,stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


->Log4j.propertiesfiles for display log message only in file

# Root logger option
log4j.rootLogger=INFO,file

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./Log/log_${current.date}.log
log4j.appender.fileout.Append=false
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.Append=false
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


->Log4j.propertiesfiles for display log message in both console and File

# Root logger option
log4j.rootLogger=INFO,stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./Log/log_${current.date}.html
log4j.appender.fileout.Append=false
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.Append=false
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


-> Load properties file in Main()


Initial Environment variable (Current.date) which is used in Log4j.properties file and load Log4j.properties file.

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh_mm_ss");
        System.setProperty("current.date", dateFormat.format(new Date()));
       
           PropertyConfigurator.configure("C:/Selenium/log4j.properties");
          


-> create log for required class files



public class Login {
   
    final static Logger logger = Logger.getLogger(Login.class);

public static boolean LoginGmail(){

logger.info("Information Message");
logger.debug("Warning Message");
logger.error("Error Message");
}

}


-> Best practise for use logger

logger.info = Instead of Sysout we can use looger.info
logger.debug = use inside of conditional Statments and looping statments.
logger.error =  use inside catch block.


-> LoGGER Level

See the link for logger level ->  http://arunrajvdm.blogspot.in/2016/12/log4j-how-to-disable-all-log-details.html