Saturday, 22 October 2016

Insert 100000 records in database with minimum time?

Using PreparedStatement batch

1. Create DB connection.
2. Create query for prepared statement.
3. setAutoCommit false.
4. Prepare stamen and add it to batch.
5. If batch size is multiple of 2000 then commit the database.
6. Repeat step 4 and 5 till the complete data is loaded.
7. close connection.

public static int uploadFilesData(List<CallDetailDTO> list) {
          
           connection = getConnection();
          
           String query = Constants.INSERT_PREFIX + Constants.TABLE_ROWS + Constants.PREPARED_STMT_VALUE;

           int count = 0;

           try {
                connection.setAutoCommit(false);
               
                PreparedStatement ps = connection.prepareStatement(query);

                for (CallDetailDTO dto: list) {
                     ps.setString (1,dto.getPartyNumberA());
                     ps.setString (2,dto.getPartyNumberB());
                     ps.setString (3,dto.getCallDate());
                     ps.setString (4,dto.getCallTime());
                     ps.setString (5,dto.getDuration());
                     ps.setString (6,dto.getCellId());
                     ps.setString (7,dto.getLastCellId());
                     ps.setString (8,dto.getCallType());
                     ps.setString (9,dto.getImei());
                     ps.setString (10,dto.getImsi());
                     ps.setString (11,dto.getPpPo());
                     ps.setString (12,dto.getSmsCentre());
                     ps.setString (13,dto.getRoamingNwCied());
                     ps.setString (14,dto.getSheetName());

                     ps.addBatch();

                     if(++count % Constants.BATCH_SIZE == 0) {
                           ps.executeBatch();
                           connection.commit();
                     }
                }
                ps.executeBatch();
                connection.commit();
                ps.close();
                connection.close();
           } catch (SQLException e) {
                System.out.println("SQLException:"+e.getMessage());
           }

           return count;
     }

Friday, 21 October 2016

Find a Pair Whose Sum is Closest to Zero in Array

This problem is also called minimum absolute sum pair.

You are given an array of integers, containing both +ve and -ve numbers. You need to find the two elements such that their sum is closest to zero.
import java.util.Arrays;

/**
 * Class to find the pair whose sum closer to zero.
 * @author rajesh.kumar
 */
public class SumClosestToZero {

     public static void main(String[] args) {
           int[] array = {10,12,14,16,-8,10,18,19,7,-6};

           getPairWithCloserToZeroSum(array);

     }

     /**
      * Method to print the pair.
      * @param array
      */
     private static void getPairWithCloserToZeroSum(int[] array) {
           Arrays.sort(array);
           int length = array.length;
          
           if(length==0 || length==1) {
                System.out.println("No pair exists !!");
           }
          
           int i = 0;
           int j = length -1;
           int minSum = array[i] + array[j];
           int minL = i; int minR= j;
           while (i  <  j) {
                int sum = array[i] + array[j] ;
                /* If sum of the elements at index i and j equals 0 */
                if (Math.abs(minSum)>Math.abs(sum)) {
                     minSum = sum;
                     minL = i;
                     minR = j;
                } else if(sum<0) {
                     i++;
                 } else {
                     j--;
                }
           }
           System.out.println("Pair is"
               +array[minL]+","+array[minR]+")");
     }
}

Find two elements in Array whose sum is Zero

SumZeroAmazon.com
import java.util.Arrays;

/**
 * Class to find the pair whose sum equal to zero.
 * @author rajesh.kumar
 */
public class SumZeroAmazon {

     public static void main(String[] args) {
           int[] array = {10,12,14,16,-8,10,18,19,6,-6};

           getPairWithZeroSum(array);

     }

     /**
      * Method to print the pair.
      * @param array
      */
     private static void getPairWithZeroSum(int[] array) {
           Arrays.sort(array);
           int length = array.length;
          
           if(length==0 || length==1) {
                System.out.println("No pair exists !!");
           }
          
           int i = 0;
           int j = length -1;
          
           while (i  <  j) {

                /* If sum of the elements at index i and j equals 0 */
                if (array[i] + array[j] == 0) {
                     System.out.println("Pair is ("+array[i]+","+array[j]+")");
                     return;
                } else if(Math.abs(array[i]) > Math.abs(array[j])) {
                     i++;
                } else {
                     j--;
                }
           }
           System.out.println("No pair exists !!");
     }
}

Saturday, 8 October 2016

Algorithm vs. Data structure

Algorithm: method for solving a problem.
Data structure: method to store information.

Algorithms + Data Structures = Programs.

Data structures such arrays, stacks, queues, trees and hash tables and their use cases. When to choose a linked list over an array? Should I go for a hash table or a balanced tree for my application? These are the kind of decisions you learn to take during the course.

Algorithms is typically more theoretical (lots of proofs!) and focuses on asymptotic time and space complexities of common algorithms. You also learn various approaches to tackle problems using strategies like Divide and Conquer, Greedy, Dynamic Programming, modelling your data as a graph and so on.
Related Posts Plugin for WordPress, Blogger...