Saturday, 26 March 2016

Another simple way to get combination of array:

public class Combinations {

public static void main(String[] args) {
// TODO Auto-generated method stub
String arr[]={"a","b","c","d"};
int combCnt=getFactorial(arr.length);
int comb=Integer.parseInt(getBinaryString(arr.length,2);
//System.out.println(comb);
Vector<String>combVect= getBinary(comb,arr.length);
String temp="";
for(String test:combVect)
{
temp="";
for(int l=0;l<test.length();l++)
{
if(test.charAt(l)=='1')
{
temp=temp+arr[l];
}
}
System.out.println(temp);
}
}

public static Vector<String> getBinary(int comb,int length)
{
Vector<String> combVect=new Vector<>();
for(int l=1;l<=comb;l++)
{
combVect.add(String.format("%"+length+"s", Integer.toBinaryString(l)).replace(" ", "0"));
//System.out.println(String.format("%"+length+"s", Integer.toBinaryString(l)).replace(" ", "0"));
}
return combVect;
}


static int getFactorial(int num)
{
int number=1;
for(int k=num;k>0;k--)
{
number=number*k;
}
return number;
}
static String getBinaryString(int lenght)
{
StringBuilder binaryBuilder=new StringBuilder()
for(int k=0;k<lenght;k++)
{
binaryBuilder.append("1");
}
return binaryBuilder.toString();
}

}

Friday, 2 October 2015

Finding missing queue in Rabbitmq

Following is the code to compare two queues of rabbitmq . By using following code you can find out missing queue in other queue .

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Vector;

import org.apache.commons.codec.binary.Base64;
import org.eclipse.jetty.util.ajax.JSON;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.testng.annotations.Test;


public class GetQueuInfo {

public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String url1="http://queue1:55672/api/definitions";
                       //Following queue is compared against above queue.
String url2="http://queue1:55672/api/definition";


Vector<String> q1=new Vector<String>();
Vector<String> q2=new Vector<String>();


GetQueuInfo qInfo=new GetQueuInfo();
String response=qInfo.getResponse(url1);


JSONParser parser=new JSONParser();
Object obj=parser.parse(response);

JSONObject json=(JSONObject)obj;
JSONArray jArr=(JSONArray)json.get("queues");

for(int i=0;i<jArr.size();i++)
{
JSONObject jObj=(JSONObject)jArr.get(i);
String name=jObj.get("name").toString();
q1.add(name);
// System.out.println(name);
}

///
response=qInfo.getResponse(url2);
parser=new JSONParser();
obj=parser.parse(response);

json=(JSONObject)obj;
jArr=(JSONArray)json.get("queues");

for(int i=0;i<jArr.size();i++)
{
JSONObject jObj=(JSONObject)jArr.get(i);
String name=jObj.get("name").toString();
q2.add(name);
//System.out.println(name);
}

System.out.println("*****************Missing queues************************");
for(String str:q1)
{
if(!q2.contains(str))
{
System.out.println(str);
}
}



} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

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