Thursday, August 1, 2013

How to implement Singleton design pattern in Java ?

Singleton design pattern is most commonly used design pattern across all  Java/J2EE applications. This design pattern is used for controlling the creation of number of instances for a specific class. Remember controlling instances is per 'Class Loader' and not for JVM. As you know JVM can have more than one class loader.

Version 1 :


public class DateTimeUtil{
// Declare static instance, that means one for a class (Not for instance )
public static DateTimeUtil singleton=null;
// Create a method , first time creates the instance, for every next subsequent calls, will not create a new instance.
public DateTimeUtil getInstance(){
// Create instance for very first time.
if(singleton==null){
singleton = new DateTimeUtil();
}
return singleton;
}
}

Versiont 2

public class DateTimeUtil{
// Declare and create  instance when class loaded itself.
public static final DateTimeUtil singleton=new DateTimeUtil();
public DateTimeUtil getInstance(){
return singleton;
}
}

Tuesday, July 30, 2013

How to retrieve distinct pair of values from the given list of integers where the sum of each pair equals to given input value.

How to retrieve distinct pair of values from the given list of integers where the sum of each pair equals to given input value.


Example

Input : 9,2,5,4,6,-9
Output : 9,2 & 5,6



How to declare and initialize ArrayList in One line using Java


// Declare & Initilize Integer ArrayList in java
List<Integer> arr = new ArrayList<Integer>(Arrays.asList(9,2,5,4,6,-9))

// Using Arrays
int[] arr2 = new int[]{9,2,5,4,6,-9};


Wednesday, July 24, 2013

Tuesday, July 23, 2013

SQL Queries on Joins


// Get All deparments where corresponding employee first name is blank

SELECT dep.department_name,
  emp.first_name
FROM DEPARTMENTS dep
LEFT OUTER JOIN EMPLOYEES emp
ON dep.department_id  =emp.department_id where emp.first_name is null


// To get first column associated value in Oracle. Ir rownum =2, then returns first two values of first_name
select first_name from employees where rownum <=1


// Connect to database using sql command prompt

conn hr/hr;

// Below query substring the city column values from 1 to 4 characters and returns using MID() function
SELECT MID(City,1,4) AS ShortCity
FROM Customers;

// Returns length of each first_name column using LEN() function
SELECT LEN(first_name) FROM employees;

// Returns current system date using NOW() function
SELECT first_name,  Now() AS PerDate

FROM employees;

Different SQL JOINs

Before we continue with examples, we will list the types the different SQL JOINs you can use:
  • INNER JOIN: Returns all rows when there is at least one match in BOTH tables
  • LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
  • RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
  • FULL JOIN: Return all rows when there is a match in ONE of the tables

How to Sort List programatically without using any in built functions in Java


   // Sort list of integers in descending order.
    private static List<Integer> sortList(List<Integer> pList){
        int temp;
        if(pList != null && pList.size() > 0){
            for(int i=0;i<pList.size();i++){
                for(int j=i+1;j<pList.size();j++){
                    if(pList.get(j) > pList.get(i)){
                        temp = pList.get(i);
                        pList.set(i,pList.get(j));
                        pList.set(j,temp);
                    }
                }
            }
        }
        return pList;
    }

Test

// Initialize and assign values
List<Integer> list1 =Arrays.asList(2,5,23,-1,-5,0,4);
// Invoke above sortList() method.


- How to merge two lists in a sorted order ?

// Initialize & assign two lists
List<Integer> list1 =Arrays.asList(2,5,23,-1,-5,0,4);
List<Integer> list2 =Arrays.asList(23,15,323,-51,-65,0,47);
 
// Add the second list to first list
list1.addAll(list2);
// Invoke above sortList() method by passing merged list i.e list1.

Monday, July 22, 2013

Left outer join example with three tables along with case





SELECT tab1.tb1_a,
       CASE WHEN tab3.tb3_a IS NOT NULL THEN tb3_b
            ELSE 'No city for him yet'
       END AS City,
tab2.*, tab3.*
FROM tab1
LEFT OUTER JOIN tab2 ON    tab1.tb1_a = tab2.tb2_a
LEFT OUTER JOIN tab3 ON    tab2.tb2_b = tab3.tb3_a
where tb2_b = (select max(tb2_b)
                 from tab2 t22
                where t22.tb2_a = tb1_a)
or    tb2_b is null
order by 1