Pages

Wednesday 29 June 2016

Oracle queries for the rownum, rank to find the nth highest salary

Find the 2nd highest data in SQL:
SELECT MAX(balance) FROM mtx_wallet_balances WHERE balance NOT IN (SELECT MAX(balance) FROM mtx_wallet_balances )

SELECT MAX(balance) FROM mtx_wallet_balances WHERE balance <> (SELECT MAX(balance) FROM mtx_wallet_balances)


Find the nth highest data in Oracle using rownum:
select * from ( select mwb.*, row_number() over (order by balance DESC) rownumb from mtx_wallet_balances mwb ) where rownumb = 105;

Find the nth highest data in Oracle using RANK:
SELECT * FROM (SELECT balance, RANK () OVER (ORDER BY balance DESC) ranking FROM mtx_wallet_balances) WHERE ranking = 105;



What are the states of object in hibernate?

States of object in hibernate



States of object (instance) in hibernate

Transient:The object is in transient state if it is just created but has no primary key (identifier) and not associated with session i.e. doesn’t represent any row of the database.

Persistent: It represent one row of the database and always associated with some unique hibernate session.

The object is in persistent state if session is open, and you just saved the instance in the database or retrieved the instance from the database.

Changes to persistent objects are tracked by hibernate and are saved into database when commit call happen.

Detached: Detached objects are those who were once persistent in past (object is in detached state if session is closed) and now they are no longer persistent.

After detached state, object comes to persistent state if we call lock() or update() or saveOrUpdate() method (reattach object to hibernate session).


If we want to move an object from persistent to detached state, we need to do either closing that session or need to clear the cache of the session.

If we want to move an object from persistent state into transient state then we need to delete that object permanently from the database.


Sunday 26 June 2016

Calculate the power in log(n) complexity in Java

Using divide and conquer:


public classPowerUsingDnC {
     public static voidmain(String[] args) {
           int x = 5;
           int n = 7;

           /** To calculate the power of x^n. */
           int result = power(x,n);
          
           System.out.println(result);
     }

     private static int power(int x, int n) {
           int temp = 1;

           if(n==0) {
                return 1;
           }

           int result = 1;

           /** divide using original power function. */
           temp = power(x, n/2);

           /** conquer using temp value. */
           if(n%2==0) {
                result = temp*temp;
           } else {
                result = temp*temp*x;
           }
           return result;
     }
}

Difference between Singleton Pattern vs Static Class in Java

The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes), so you can pass around the singleton as if it were "just another" implementation.

Static class:
Static class can not be a top level class and can not implement the interface.
It provides better performance than Singleton pattern, because static methods are bonded on compile time.
Static class always provides the eagerly loaded however Singleton classes can be lazy loaded if it’s a heavy object.

Singleton objects:
It stored on heap while static class stored in stack.
It can have constructor while static class cannot.
It can dispose but not static class.
It can clone but not with static class (logically wrong to clone the Singleton objects).

Difference between Singleton Pattern vs Static Class in Java

The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes), so you can pass around the singleton as if it were "just another" implementation.

Static class:
Static class provides better performance than Singleton pattern, because static methods are bonded on compile time.

Static class always provides the eagerly loaded however Singleton classes can be lazy loaded if it’s a heavy object.

Singleton Objects:
Singleton Objects stored on heap while static class stored in stack.
Singleton Objects can have constructor while Static Class cannot.
Singleton Objects can dispose but not static class.
Singleton Objects can clone but not with static class (logically wrong to clone the Singleton objects).

Wednesday 8 June 2016

Print Even or Odd without using conditional statement in Java



public classEvenOddWoCondition {

     /**
      * Return even odd result.
      * @param number
      * @return even or odd.
      */
     private static String getEvenOddRest(int number) {
           String[] array  = {"even","odd"};

           int index = number%2;

           returnarray[index];
     }

     public static voidmain(String[] args) {
           int number = 10;
           String result = getEvenOddRest(number);
           System.out.println(result);
     }
}

Calling run method directly of thread (created by implementing Runnable interface) in Java

It will works as ordinary java method because the thread class run() method implementation.

voidjava.lang.Thread.run()

If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns. Subclasses of Thread should override this method.

@Override
public void run() {
    if (target != null) {
        target.run();
    }
}

Here target is our runnable class which will set while calling Thread class constructor while creating thread.

Example:

class MyThread implements Runnable {
     @Override
     public void run() {
           System.out.println("This is my thread !!");
     }
}

public class ThreadTest {
     public static voidmain(String[] args) {
           MyThread  myThread = new MyThread();
          
           Thread thread = new Thread(myThread);
          
           thread.run();
     }
}

Monday 6 June 2016

Reverse merge the half linked list

Write the production level code to arrange the linked list as given example:

1->2->3->4->5->6, make the following changes 1->6->2->5->3->4.

class Node {
     int value; Node next;
     public Node(intvalue) {
           this.value = value;
     }
}

public class ArrangeSuffledList {
     private static void getReversedSuffledList(Node head) {
           if(head==null|| head.next==null) {
                printList(head);
                return;
           }
          
           Node end = head.next;
           int length = 1;
           while(end != null) {
                end = end.next;
                length++;
           }

           Node mid = head.next;
           for (inti = 0; i < length/2-1; i++) {
                mid = mid.next;
           }

           //printList(mid);
           Node head2 = new Node(mid.value);
           Node next = mid.next;

           for (inti = 0; i < length-length/2-1; i++) {
                Node tNext = next.next;
                next = new Node(next.value);
                Node temp = next;
                temp.next = head2;
                head2 = temp;
                next = tNext;       

           }

           printList(mergeList(head, head2, length));

     }

     private static Node mergeList(Node head1, Node head2, int length) {
           Node head = new Node(head1.value);
           head.next = new Node(head2.value);
           Node next = head.next;

           int count = 2;

           head1 = head1.next;
           head2 = head2.next;

           while(head2!=null) {
                Node temp = newNode(head1.value);
                next.next = temp;
                next = next.next;
                count++;
                if(count<length) {
                     temp = newNode(head2.value);
                     next.next = temp;
                     next = next.next;
                     count++;
                }

                head1 = head1.next;
                head2 = head2.next;
           }
           return head;
     }

     public static void main(String[] args) {
           Node head = new Node(1);
           head.next = new Node(2);
           head.next.next = new Node(3);
           head.next.next.next = new Node(4);
           head.next.next.next.next = new Node(5);
           head.next.next.next.next.next = new Node(6);
           //head.next.next.next.next.next.next = new Node(7);

           /* 1->2->3->4->5->6 :: 1->6->2->5->3->4 * */
           getReversedSuffledList(head);
           //printList(head);
     }

     private static void printList(Node head) {
           System.out.println("");
           Node next = null;
           if(head!=null) {
                System.out.print(head.value+" ");
                next = head.next;
           }

           while(next!=null) {
                System.out.print(next.value+" ");
                next = next.next;
           }
     }
}