Thursday 27 August 2015

Java Memory Management

Memory Management - PART - 1


Memory Management in java is responsibility of garbage collector. Garbage Collection is not the only form of Memory Management in Java. Real-time Specification for Java (RTSJ) is also being used for Memory Management. These efforts were mainly dedicated to real-time and embedded programming in Java for which GC was not suitable - due to performance overhead.

Understanding JVM Memory Model is very important if we want to know the working process of Java Garbage Collection.

Types of Garbage Collector : -

  • Do nothing : - It might just decide never to run and never to do anything, no memory gets free but it do stills gurantee to not collecting live objects.

  • Reference Counting garbage collector : - COM programming environment is the best example of Reference Counting Garbage collector. COM application may call 2 functions. First is Add Ref and second is Release. Add Ref increments the count of the object and Release decrease the count. When count goes to zero then ref is no longer been used or we can say that A reference count is maintained for each object on the heap. When an object is first created and a reference to it is assigned to a variable, the object's reference count is set to one. When any other variable is assigned a reference to that object, the object's count is incremented. When a reference to an object goes out of scope or is assigned a new value, the object's count is decremented. Any object with a reference count of zero can be garbage collected. When an object is garbage collected, any objects that it refers to have their reference counts decremented. In this way the garbage collection of one object may lead to the subsequent garbage collection of other objects.

  • Mark and Sweep : - To determine which objects are no longer in use, the JVM intermittently runs mark-and-sweep algorithm. Garbage collector runs in 2 phases, In mark phase it is marking that memory is still alive or this algorithm traverses all object references, starting with the GC roots, and marks every object found as alive and in sweep phase all of the heap memory that is not occupied by marked objects is reclaimed. It is simply marked as free, essentially swept free of unused objects.

  • Copying - Copying garbage collectors move all live objects to a new area and the old area is known to be all free space. This is not following any separate Mark and Sweep phases Objects are copied(these objects are discovered by the traversal from the root nodes) to the new area on the fly and forwarding ponters are left in their old locations and these pointers allows the garbage collector to detect references to objects that have already been moved. The garbage collector can then assign the value of the forwarding pointer to the references so they point to the object's new location.

  • Generational - Copying collectors spend much of their time for copying the same long-lived objects again and again. In order to address this inefficiency Generational collectors work with grouping objects by age and garbage collecting younger objects more often than older objects. This approach works by dividing the heap into two or more sub-heaps, each of which serves one "generation" of objects. The youngest generation is garbage collected most often. As most objects are short-lived, only a small percentage of young objects are likely to survive their first collection. Once an object has survived a few garbage collections as a member of the youngest generation, the object is promoted to the next generation: it is moved to another sub-heap.

  • Incremental - Rather than attempting to find and discard all unreachable objects at each invocation an incremental garbage collector just attempts to find and discard a portion of the unreachable objects. Because only a portion of the heap is garbage collected at each invocation, each invocation should in theory run in less time. A garbage collector that can perform incremental collections, each of which is guaranteed to require less than a certain maximum amount of time, can help make a Java virtual machine suitable for real-time environments.  


Reference taken from other sources

Tuesday 4 August 2015

How to Compare two Arraylists which contain objects of the same class?

Compare two Arraylists which contain objects of the same class

I have two lists of different sizes which belong to the same class Student which is having attributes like:

  • Integer id;
  • String name;
  • Integer age;


We will compare these two lists and remove the elements which are common
in both of them. Below is the example available to compare two lists:-

Student.java

public class Student {

/**
  * @author Gaurav
  *
  */
private Integer id;
private String name;
private Integer age;


public Student(Integer id, String name, Integer age){
    this.id = id;
    this.name = name;
    this.age = age;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}


public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

@Override
public String toString() {
    return "Id : " + this.id +" Name : "+this.name+ " age : "+this.age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
Student student = (Student) obj;
return id == student.id
&& (name == student.name || (name != null && name
.equals(student.getName())))
&& (age == student.age || (age != null && age
.equals(student.getAge())));
}
}

CompareLists.java

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

import org.apache.commons.collections.CollectionUtils;

/**
 * @author Gaurav
 *
 */
public class CompareLists {
public static void main( String[] args )
{
  List<Student> l1 = new ArrayList<Student>();
  l1.add(new Student(1,"Kumar",33));
  l1.add(new Student(2,"Hiten",35));
  l1.add(new Student(3,"Varun",46));
  l1.add(new Student(4,"Gaurav",35));
  l1.add(new Student(5,"Vishal",40));


  List<Student> l2 = new ArrayList<Student>();
  l2.add(new Student(4,"Gaurav",35));
  l2.add(new Student(5,"Vishal",40));
  
 
  //This is the first way to compare two list containing objects of same classes and removing the common item.
  
  List<Student> firstList = new ArrayList<Student>(l1);
  System.out.println("First list before removing common elements");
  System.out.println(firstList);
  firstList.removeAll(l2);
  System.out.println("\nFirst list after removing common elements");
  System.out.println(firstList);
  
 //This is the Second way to compare two list containing objects of same classes  and removing the common item.
  List<Student> secondList = new ArrayList<Student>();
  System.out.println("\nSecond list before removing common elements");
  System.out.println(l1);
  for (int i = 0; i < l1.size(); i++){

       if (!l2.contains(l1.get(i))){

           secondList.add(l1.get(i));
       }
   }
  System.out.println("\nSecond list after removing common elements");
  System.out.println(secondList);
  
  //This is the Third way to compare two list containing objects of same classes  and removing the common item.
  List<Student> thirdList = new ArrayList<Student>(l1);
  System.out.println("\nThird list before removing common elements");
  System.out.println(thirdList);
  for(Student apv : l1){
for(Student pv : l2){
if(apv.getId() == pv.getId() && apv.getName().equalsIgnoreCase(pv.getName())){
thirdList.remove(pv);
}
}
}
  System.out.println("\nThird list after removing common elements");
  System.out.println(thirdList);
   
  
  //This is the Fourth way to compare two list containing objects of same classes  and removing the common item.
 
 /**org.apache.commons.collections.CollectionUtils - with the help of this class below call is possible and for this explicitly, we need to add the jar named commons-collections-3.2.1.jar*/
  
  System.out.println("\nFourth list after removing common elements");
  @SuppressWarnings("unchecked")
  Collection<Student> fourthList= CollectionUtils.subtract(l1, l2);
  System.out.println(fourthList);
 
}
}

Result:-

First list before removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46, Id : 4 Name : Gaurav age : 35, Id : 5 Name : Vishal age : 40]

First list after removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46]

Second list before removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46, Id : 4 Name : Gaurav age : 35, Id : 5 Name : Vishal age : 40]

Second list after removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46]

Third list before removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46, Id : 4 Name : Gaurav age : 35, Id : 5 Name : Vishal age : 40]

Third list after removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46]

Fourth list after removing common elements
[Id : 1 Name : Kumar age : 33, Id : 2 Name : Hiten age : 35, Id : 3 Name : Varun age : 46]

Comparing and Sorting list objects based on their multiple attributes

Comparing and Sorting list objects 


There are multiple ways to sort objects available in list collection based on their multiple attributes which we can treat as keys. We will see the example below where we will sort list of objects. These objects are available as pojo(plain old java objects) forms.

Employee.java

package com.gaurav.compare.lists;
/**
 * @author Gaurav
 *
 */
public class Employee {
    private String empName;
    private String designation;
    private int age;
    private int salary;

    public Employee(String name, String jobTitle, int age, int salary) {
        this.empName = name;
        this.designation = jobTitle;
        this.age = age;
        this.salary = salary;
    }

    public String toString() {
        return String.format("%s\t%s\t%d\t%d", empName, designation, age, salary); 
    }

public String getEmpName() {
return empName;
}

public void setEmpName(String empName) {
this.empName = empName;
}

public String getDesignation() {
return designation;
}

public void setDesignation(String designation) {
this.designation = designation;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}
}


ComparatorChain.java

package com.gaurav.compare.lists;
/**
 * @author Gaurav
 *
 */
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class ComparatorChain implements Comparator<Employee>{
private List<Comparator<Employee>> lstComparators;
 
    @SafeVarargs
public ComparatorChain(Comparator<Employee>... comparators) {
        this.lstComparators = Arrays.asList(comparators);
    }

    @Override
    public int compare(Employee employee1, Employee employee2) {
        for (Comparator<Employee> comparator : lstComparators) {
            int finalResult = comparator.compare(employee1, employee2);
            if (finalResult != 0) {
                return finalResult;
            }
        }
        return 0;
    }
}

DesignationComparator.java

package com.gaurav.compare.lists;
/**
 * @author Gaurav
 *
 */
import java.util.Comparator;

public class DesignationComparator implements Comparator<Employee>{
 @Override
   public int compare(Employee employee1, Employee employee2) {
       return employee1.getDesignation().compareTo(employee2.getDesignation());
   }
}

AgeComparator.java

package com.gaurav.compare.lists;
/**
 * @author Gaurav
 *
 */
import java.util.Comparator;

public class AgeComparator implements Comparator<Employee>{
 @Override
   public int compare(Employee employee1, Employee employee2) {
       return employee1.getAge() - employee2.getAge();
   }
}

SalaryComparator.java

package com.gaurav.compare.lists;
/**
 * @author Gaurav
 *
 */
import java.util.Comparator;

public class SalaryComparator implements Comparator<Employee>{
@Override
    public int compare(Employee employee1, Employee employee2) {
        return employee1.getSalary() - employee2.getSalary();
    }
}

ListObjectsComparisonAndSortingExample.java

package com.gaurav.compare.lists;
/**
 * @author Gaurav
 *
 */
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ListObjectsComparisonAndSortingExample {
public static void main(String[] args) {
 
       System.out.println("# COMPARING AND SORTING LIST OBJECTS BASED ON MULTIPLE ATTRIBUTES #\n");
 
       List<Employee> employeesList = new ArrayList<Employee>();
 
       employeesList.add(new Employee("Tusaar", "Developer", 44, 82000));
       employeesList.add(new Employee("Suresh", "Designer", 32, 77500));
       employeesList.add(new Employee("Bimlesh", "Designer", 44, 141900));
       employeesList.add(new Employee("Piyush", "Programmer", 26, 65700));
       employeesList.add(new Employee("Tiya", "Designer", 44, 131300));
       employeesList.add(new Employee("Culinan", "Programmer", 31, 55000));
       employeesList.add(new Employee("Amit", "Programmer", 27, 58000));
       employeesList.add(new Employee("Aaditya", "Designer", 31, 155000));
       employeesList.add(new Employee("Bimal", "Programmer", 24, 39000));
       employeesList.add(new Employee("Smith", "Developer", 29, 89000));
       employeesList.add(new Employee("Jimmy", "Developer", 36, 69000));
       employeesList.add(new Employee("Peter", "Developer", 38, 152000));
       employeesList.add(new Employee("Ali", "Programmer", 37, 85000));
       employeesList.add(new Employee("Dravid", "Developer", 33, 10000));
       employeesList.add(new Employee("Jatin", "Designer", 34, 83250));
 
       System.out.println("*** Displaying the employee list objects before sorting:\n");
 
       for (Employee employee : employeesList) {
           System.out.println(employee);
       }
 
       Collections.sort(employeesList, new ComparatorChain(
               new DesignationComparator(),
               new AgeComparator(),
               new SalaryComparator())
       );
 
       System.out.println("\n*** Displaying the employee list objects after sorting:\n");
 
       for (Employee employee : employeesList) {
           System.out.println(employee);
       }
 
   }
}


Result:-

# COMPARING AND SORTING LIST OBJECTS BASED ON MULTIPLE ATTRIBUTES #

*** Displaying the employee list objects before sorting:

Tusaar Developer 44 82000
Suresh Designer 32 77500
Bimlesh Designer 44 141900
Piyush Programmer 26 65700
Tiya Designer 44 131300
Culinan Programmer 31 55000
Amit Programmer 27 58000
Aaditya Designer 31 155000
Bimal Programmer 24 39000
Smith Developer 29 89000
Jimmy Developer 36 69000
Peter Developer 38 152000
Ali Programmer 37 85000
Dravid Developer 33 10000
Jatin Designer 34 83250

*** Displaying the employee list objects after sorting:

Aaditya Designer 31 155000
Suresh Designer 32 77500
Jatin Designer 34 83250
Tiya Designer 44 131300
Bimlesh Designer 44 141900
Smith Developer 29 89000
Dravid Developer 33 10000
Jimmy Developer 36 69000
Peter Developer 38 152000
Tusaar Developer 44 82000
Bimal Programmer 24 39000
Piyush Programmer 26 65700
Amit Programmer 27 58000
Culinan Programmer 31 55000
Ali Programmer 37 85000




Monday 3 August 2015

Visitor Design Pattern Implementation

Visitor Design Pattern


Question : - What is Visitor Design Pattern?
Answer : - This design pattern also falls under Behavioral design pattern. Using this design pattern, we can move the operational logic from the objects to another class. This pattern is providing a way of separating an algorithm from an object structure on which it operates. This visitor pattern is used to simplify operations on groupings of related objects.

As of GOF:-Allows for one or more operation to be applied to a set of objects at runtime, decoupling the operations from the object structure.

Benefits : -

Adding new operatios are easy because if the logic of operation changes, then we need to make change only in the visitor implementation rather than doing it in all the item classes.
Visitors can gather state as they visit each element in the object structure. Without a visitor, this state would have to be passed as an extra arguments to the operations that perform the traversal.

Disadvantage : -
During design period of application, we should know the return type of visit() methods otherwise
we will have to change the interface and all of its implementations.
If there are many implementations of visitor interface then extending that class is not a easy task.

For more information on Visitor design pattern please refer to the below link

In the below example, we will see the implementation of the Visitor design pattern

BodyPart.java

package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public interface BodyPart {
public void accept(BodyPartVisitor bodyPartVisitor);
}


Body.java

package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public class Body implements BodyPart{
private BodyPart[] bodyParts;
public Body(){
bodyParts = new BodyPart[] {new Eye(), new Heart(), new Kidney()};
}
@Override
  public void accept(BodyPartVisitor bodyPartVisitor) {
     for (int i = 0; i < bodyParts.length; i++) {
     bodyParts[i].accept(bodyPartVisitor);
     }
     bodyPartVisitor.visit(this);
  }

}

BodyPartVisitor.java

package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public interface BodyPartVisitor {
public void visit(Body body);
public void visit(Eye eye);
public void visit(Heart heart);
public void visit(Kidney kidney);
}

Eye.java

package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public class Eye implements BodyPart{

@Override
public void accept(BodyPartVisitor bodyPartVisitor) {
bodyPartVisitor.visit(this);
}

}


Heart.java


package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public class Heart implements BodyPart{

@Override
public void accept(BodyPartVisitor bodyPartVisitor) {
bodyPartVisitor.visit(this);
}

}

Kidney.java


package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public class Kidney implements BodyPart {

@Override
public void accept(BodyPartVisitor bodyPartVisitor) {
bodyPartVisitor.visit(this);
}

}

FullBodyScanner.java

package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public class FullBodyScanner implements BodyPartVisitor{

@Override
public void visit(Body body) {
System.out.println("Scanning Body");
}
@Override
public void visit(Eye eye) {
System.out.println("Scanning Eye");
}

@Override
public void visit(Heart heart) {
System.out.println("Scanning Heart");
}

@Override
public void visit(Kidney kidney) {
System.out.println("Scanning Heart");
}

}

VisitorDesignPatternDemo.java

package com.gaurav.designpattern.visitor;
/**
 * @author Gaurav
 *
 */
public class VisitorDesignPatternDemo {
public static void main(String args[]) {
BodyPart body = new Body();
body.accept(new FullBodyScanner());
}
}

Result:-

Scanning Eye
Scanning Heart
Scanning Kidney
Scanning Body

JMS                                                            JSON                                                       JDBC