Wednesday, 9 July 2014

Find out all combinations of an array.

This is the java code to find out combinations of an array.

class Combinations
{
String DIM_ARR[]={"a","b","c"};
  public static void main (String args[])
        {
            Combinations combobj= new Combinations();
            combobj.combine(0);
        }
       
       
        private void combine(int start){
            for( int i = start;i< DIM_ARR.length; i++ ){
                tcVect.add(DIM_ARR[i]+",");
                    System.out.println(tcVect.toString());
                if ( i < DIM_ARR.length )
                combine( i + 1);
                tcVect.remove(tcVect.size()-1);
            }
        }
}

Tuesday, 8 July 2014

HashMap Sort by Value

Following is the sample code to sort hashmap by its value using custom comparator.


package code_snippet;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class SortByValueHashMap {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        HashMap<String, Integer> hMap=new HashMap<String, Integer>();
        hMap.put("a", 10);
        hMap.put("b", 1);
        hMap.put("c", 100);
        System.out.println("Without Sorting="+hMap);
       

Tuesday, 27 May 2014

Example of deadlock

Following is the simplest example of how deadlock occurs?. Following code will run forever since they are waiting for each other to release the lock.

public static void main(String[] args) {
        // TODO Auto-generated method stub
        final LinkedList<String> ll_1=new LinkedList<String>();
        final LinkedList<String> ll_2=new LinkedList<String>();
       

Usage of wait,notify and notifyAll.

Following code demonstrates the the wait,notifyAll working.I have created one linkedlist object.
  Working of code is as below:
  1.First thread is created and set priority to 5 and called wait method.It will be in waiting state until it receives notify .
  2.Created second thread and set priority to 6 and called wait method.It will be in waiting state until it receives notify .
  3.Create third thread and added one element to list and called notifyAll method. In this ,notifyAll method will notify to second thread because priority is higher than first thread.It executes second thread then first thread. Always wait,notify,notifyAll methods get called from synchronized blocks or methods.
=======================code starts==================================
public static void main(String[] args) {
        // TODO Auto-generated method stub
        final LinkedList<String> ll=new LinkedList<String>();
        new Thread(new Runnable() {

Friday, 7 February 2014

Keyword driven automation framework tool

Hello guys, i have developed one tool for automation using java swing in front end and on top of webdriver.

Please download and use it, and let me know if you face any issue.

ReadMe
Executable

Monday, 7 October 2013

Static method and variables

This is one of the very important topics of core Java. In this post i am going to explain you about static in Java. First and very important point is you can access static methods and variables without creating an object of class. You can access static variables and methods by using classname.static_member.

Ex.
  class StatEx
  {
    static int i;
    void fun()
   {
      System.out.println(StatEx.i);
   }
  }

In above example you can notice that i have accessed i variable by using className (StatEx.i). And same you can do with the static member function.

Some of the important features of static ;
  • Static members can be accessed without creating an object of class.
  • Static function can access only static methods/variables.
  • You can initialize static members in static block .It gets executed only once when class loads.
  • Static variables also called as class variable.
  • Outer class or main class can not be static but you can declare inner class as static.
  • Static method can not be overridden.
  • Binding happens at compile time.
  • In java we have one public static void main method which is also static, an entry point for execution.




Sunday, 6 October 2013

Why main method is static?

This is one of the very popular interview question for beginner. So main method is static because without creating an object you can access this method and start the execution. JVM calls this method by using  its class name dot main method(MyClass.main(params). Now think we don't have main method ,so to do the access we have to create an object,to create an object we have to execute certain required line of code.So without static method entry of an execution is not possible that's why main method is static.