Wednesday 11 September 2013

Adding google analytics to jsp application

First register your application in google analytics.
Your application will be given a unique app id.
Then add the following code in your jsp.

<!-- Google Analytics module-->
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'app_id']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();


</script>

Note: It will take one or two days to track the number of users in your application.
The moment you deploy the code it will start tracking but it will take a day time to get updated in the Google Analytics site.

Thursday 27 June 2013

What is Race Condition in java? (or) How does Race condition occur in java?


  • Race condition in java is a type of concurrency issue caused in our program because of parallel execution of multiple threads.
  • A race condition occurs when two or more threads can access shared data and they try to change it at the same time i.e. both threads are "racing" to access/change the data.
  • Because the thread scheduling algorithm can swap between threads at any time, you don't know the order in which the threads will attempt to access the shared data. 
  • Since Java is a multi-threaded programming language hence risk of Race condition is higher in Java which demands clear understanding of what causes a race condition and how to avoid that.

Wednesday 26 June 2013

Code for Binary Search Tree

public class BinaryTree {

public int var;
public BinaryTree rightNode;
public BinaryTree leftNode;

public BinaryTree(){

}
public BinaryTree(int var){
this.var=var;
this.rightNode=null;
this.leftNode=null;
}

public void insert(BinaryTree tree, BinaryTree node){
int temp=tree.var;
int temp1=0;
if(node!=null){
temp1=node.var;
}
if(node == null){
//First Node Do nothing
}else if(temp < temp1 && tree.rightNode==null){
tree.rightNode = node;
}else if(temp > temp1 && tree.leftNode == null){
tree.leftNode = node;
}else if(temp < temp1 && tree.rightNode != null){
insert(tree.rightNode,node);
}else if(temp > temp1 && tree.leftNode != null){
insert(tree.leftNode,node);
}
}

public void display(BinaryTree tree){
if(tree == null){
System.out.println("No nodes to display");
}else if(tree.leftNode==null && tree.rightNode == null){
System.out.println(tree.var);
}else if(tree.leftNode==null && tree.rightNode != null){
System.out.println(tree.var);
display(tree.rightNode);
}else if(tree.leftNode != null && tree.rightNode == null){
display(tree.leftNode);
System.out.println(tree.var);
}else if(tree.leftNode != null && tree.rightNode != null){
display(tree.leftNode);
System.out.println(tree.var);
display(tree.rightNode);
}
}

//Main method can be written in your own way. I have used a unique object which i have not used. But this is not the correct approch.
public static void main(String args[]){
BinaryTree obj=new BinaryTree();
BinaryTree bt=new BinaryTree(8);
obj.insert(bt, null);
BinaryTree bt1=new BinaryTree(10);
obj.insert(bt, bt1);
BinaryTree bt2=new BinaryTree(5);
obj.insert(bt, bt2);
BinaryTree bt3=new BinaryTree(7);
obj.insert(bt, bt3);
BinaryTree bt4=new BinaryTree(4);
obj.insert(bt, bt4);
obj.display(bt);
obj.linkedListConversion(bt);
}
}

Algorithm for constructing a BinarySearchTree

Step 1: Creating Varibales
1.1: Create a integer variable to store the value
1.2: Create two references for the same class to point to leftNode and rightNode
Step 2: Initializing constructor
2.1: Create a constructor which sets the value for a node and make rightNode as null and leftNode as
null
Step 3: Mapping the node
3.1: If this the the first node then mapping is not required
3.2: If already node is available get value of the currentNode in the tree and compare with the value of
the new node
3.3: If the value is less than the currentNode check whether the leftNode is null or has any other node
3.3.1: If the leftNode has a value then perform 3.2 again
3.3.2: Else if the leftNode is null then insert the node here by pointing the null value to the
newNode
3.4: Else if the value is greater than the currentNode check whether the rightNode is null or has any
other node
3.4.1: If the rightNode has a value then perform 3.2 again
3.4.2: Else if the rightNode is null then insert the node here by pointing the null value to the
newNode

Wednesday 15 May 2013

Program to create a Linked List in Java


class Link {
    public int data1;
    public Link nextLink;

    //Link constructor
    public Link(int d1) {
   data1 = d1;
    }

    //Print Link data
    public void printLink() {
   System.out.print("{" + data1 + "} ");
    }
}

class LinkList {
    private Link first;

    //LinkList constructor
    public LinkList() {
   first = null;
    }

    //Returns true if list is empty
    public boolean isEmpty() {
   return first == null;
    }

    //Inserts a new Link at the first of the list
    public void insert(int d1) {
   Link link = new Link(d1);
   link.nextLink = first;
   first = link;
    }

    //Deletes the link at the first of the list
    public Link delete() {
   Link temp = first;
   first = first.nextLink;
   return temp;
    }

    //Prints list data
    public void printList() {
   Link currentLink = first;
   System.out.print("List: ");
   while(currentLink != null) {
   currentLink.printLink();
   currentLink = currentLink.nextLink;
   }
   System.out.println("");
    }
}  

class LinkListTest {
    public static void main(String[] args) {
   LinkList list = new LinkList();

   list.insert(1);
   list.insert(2);
   list.insert(3);
   list.insert(4);
   list.insert(5);

   list.printList();

   while(!list.isEmpty()) {
   Link deletedLink = list.delete();
   System.out.print("deleted: ");
   deletedLink.printLink();
   System.out.println("");
   }
   list.printList();
    }
}

Monday 6 May 2013

Difference between HashMap and Hashtable in Java


  • HashMap allows null values as key and value whereas Hashtable doesn't allow nulls 
  • HashMap is non synchronized whereas Hashtable is synchronized.  
  • Java 5 introduces ConcurrentHashMap which is an alternative of Hashtable and provides better scalability than Hashtable in Java. 
  • Iterator in the HashMap is  a fail-fast iterator  while the enumerator for the Hashtable is not. 
  • HashMap throw ConcurrentModificationException if any other Thread modifies the map structurally  by adding or removing any element except Iterator's own remove() method.  
  • But this is not a guaranteed behavior and will be done by JVM on best effort. 
  • One more notable difference between Hashtable and HashMap is that because of thread-safety and synchronization Hashtable is much slower than HashMap if used in Single threaded environment.  
  • HashMap does not guarantee that the order of the map will remain constant over time.

Difference between ConcurrentHashMap and Collections.synchronizedMap


  • ConcurrentHashMap is designed for concurrency and improve performance while HashMap which is non synchronized by nature can be synchronized by applying a wrapper using Collections.synchronizedMap 
  • ConcurrentHashMap do not allow null keys or null values while HashMap allows null keys. 
  • In case of multiple reader and Single writer ConcurrentHashMap is best choice.