Showing posts with label core java. Show all posts
Showing posts with label core java. Show all posts

Tuesday 14 July 2015

Swapping Columns of CSV Row

Swapping column values of CSV rows efficiently is not a simple piece of code, especially when it comes to processing of millions of records. Processing on String values consumes more CPU cycles when compared to char arrays. When char array representation of a String is easily available, let us try to make use of it for the swap purpose.

This sample code is written for a requirement where I had to swap two column values:
6,255,00aecd,00ebcb00,0,0,28356,0,417,1
had to be converted to
6,255,00ebcb00,00aecd,0,0,28356,0,417,1
i.e. to swap columns 2 and 3, if columns are numbered from 0.

Simple approach towards this is to split the line with delimiter, swap values in the respective array and join with delimiter again. This would be an expensive operation when you have millions of lines to process.

Other way of handling this operation is to get char array of the String, locate the starting and ending points of columns to be swapped and do array operation to move the characters.

Approach 1 (Simple and Expensive)


 private static String swapColumns(final String value,
   final String separator, final int swapColumn1, final int swapColumn2) {
  String[] splitValues = value.split(separator);
  String temp = splitValues[swapColumn1];
  splitValues[swapColumn1] = splitValues[swapColumn2];
  splitValues[swapColumn2] = temp;

  StringBuilder stringBuilder = new StringBuilder();

  boolean itemAdded = false;

  for (String splitValue : splitValues) {
   if (itemAdded) {
    stringBuilder.append(separator);
   }
   stringBuilder.append(splitValue);
   itemAdded = true;
  }

  return stringBuilder.toString();
 }
This code is available for download here.

Approach 2 (char array)


 private static String swapColumns(final String value, final char separator,
   final int swapColumn1, final int swapColumn2) {
  char[] charValues = value.toCharArray();

  int swapColumn1StartIndex = swapColumn1 == 0 ? 0 : -1;
  int swapColumn1EndIndex = -1;
  int swapColumn2StartIndex = -1;
  int swapColumn2EndIndex = -1;

  int i = 0;
  int separatorOccurance = 0;
  for (; i < charValues.length; i++) {
   if (charValues[i] == separator) {
    separatorOccurance++;
    if (swapColumn1StartIndex > -1) {
     swapColumn1EndIndex = i;
     break;
    } else if (swapColumn1 == separatorOccurance) {
     swapColumn1StartIndex = i + 1;
    }
   }
  }

  swapColumn2StartIndex = swapColumn2 == separatorOccurance
    ? i + 1
    : swapColumn2StartIndex;

  for (i++; i < charValues.length; i++) {
   if (charValues[i] == separator || i + 1 == charValues.length) {
    separatorOccurance++;
    if (swapColumn2StartIndex > -1) {
     swapColumn2EndIndex = i
       + ((i + 1 == charValues.length) ? 1 : 0);
     break;
    } else if (swapColumn2 == separatorOccurance) {
     swapColumn2StartIndex = i + 1;
    }
   }
  }

  if (swapColumn1EndIndex - swapColumn1StartIndex > swapColumn2EndIndex
    - swapColumn2StartIndex
    || swapColumn1EndIndex - swapColumn1StartIndex == swapColumn2EndIndex
      - swapColumn2StartIndex) {
   char[] tempArray = new char[swapColumn1EndIndex
     - swapColumn1StartIndex];
   System.arraycopy(charValues, swapColumn1StartIndex, tempArray, 0,
     tempArray.length);
   System.arraycopy(charValues, swapColumn2StartIndex, charValues,
     swapColumn1StartIndex, swapColumn2EndIndex
       - swapColumn2StartIndex);
   System.arraycopy(charValues, swapColumn1EndIndex, charValues,
     swapColumn1EndIndex - swapColumn1EndIndex
       + swapColumn1StartIndex + swapColumn2EndIndex
       - swapColumn2StartIndex, swapColumn2StartIndex
       - swapColumn1EndIndex);
   System.arraycopy(tempArray, 0, charValues, swapColumn2StartIndex
     - swapColumn1EndIndex + swapColumn1StartIndex
     + swapColumn2EndIndex - swapColumn2StartIndex,
     tempArray.length);
  } else {
   char[] tempArray = new char[swapColumn2EndIndex
     - swapColumn2StartIndex];
   System.arraycopy(charValues, swapColumn2StartIndex, tempArray, 0,
     tempArray.length);
   System.arraycopy(charValues, swapColumn1StartIndex, charValues,
     swapColumn2StartIndex + swapColumn2EndIndex
       - swapColumn2StartIndex - swapColumn1EndIndex
       + swapColumn1StartIndex, swapColumn1EndIndex
       - swapColumn1StartIndex);
   System.arraycopy(charValues, swapColumn1EndIndex, charValues,
     swapColumn1EndIndex + swapColumn2EndIndex
       - swapColumn2StartIndex - swapColumn1EndIndex
       + swapColumn1StartIndex, swapColumn2StartIndex
       - swapColumn1EndIndex);
   System.arraycopy(tempArray, 0, charValues, swapColumn1StartIndex,
     tempArray.length);
  }

  return new String(charValues);
 }
Approach 2 has more advantage over Approach 1. Have performed a test and found that Approach 2 has completed task in 2.776421 ms where Approach 1 has taken 19.690034 ms to complete.

You can download the Java source files here.

Saturday 19 January 2013

Java standalone application on Eclipse

Java standalone application would be the easiest one to start when learning the language. We will print "Hello World" text on the console as the first step.

Java standalone applications should have at least a class with main method, in the right signature. This is the first method that will take the handle of the application when called by Java Virtual Machine. Here we go.

  1. On your Eclipse IDE, click on File > New > Project.
  2. Choose Java > Java Project and click Next.
  3. Key in the project name and click Finish.
  4. From the Package Explorer, right click and create new Class.
  5. Name your new Class. If you need to place the new Class in a package, fill-in the package name. Since this is your Main class, you can check "public static void main(String[] args)" under "Which method stubs would you like to create?". I would not select this option now.
  6. You will find your Main class being opened in the editor. You can type the entire signature of the main method or just type main, then use Ctrl + Space to show the proposed templates and choose main method to generate the main method.
  7. Type the statement to print "Hello World" statement on the output stream. That is, System.out.println("Hello World");. The template way to type the statement is sysout and using Ctrl + Space.
  8. Format the code snippet using Source > Format or Ctrl + Shift + F and save the file.
  9. Right-click on the project or the class and use Run As > Java Application to run the file.
  10. See the console. If console could not be found, select to show it from Window menu.

Sunday 30 December 2012

Setting up Java Development Environment (JDK) on Linux


Java Development Kit (JDK) is essential for developing Java applications on your computer. JDK consists of many tools which can make your development easy and comfortable, especially when you do not have an Integrated Development Environment (IDE) such as Eclipse or NetBeans.

Here, I will mention about setting up JDK on CentOS 64 bit Operating System. However, the steps of installation are common on most of the Linux flavors.


Steps:

  1. Visit Oracle website to download the installation file.
  2. Click on the "Download" button at the JDK section to download the latest release of JDK. If you are looking for a previous version of JDK, you can visit the "Previous Releases" section on the same page. If you could not locate the section, you can search for "Previous" keyword on the page using the Ctrl + F key combination supported by most browsers.
  3. Accept the license agreement and choose the download. Determine your Operating System architecture bit length by executing getconf LONG_BIT or uname -m command on console. CentOS uses RPM based package management. Hence, you can download either .rpm or the zipped package. The steps of path configuration varies with the kind of package you choose.
  4. Follow the below steps according to the package you have downloaded:

RPM Installation:

  1. Switch to root user using su command.
  2. Copy the downloaded file to a location or navigate to the directory.
  3. Use rpm -ivh jdk<version>-<XXbit>.rpm to start the installation.

While installing JDK 7, you may find the errors being displayed as in the above image. These can be ignored. You can verify the installation using the java -version command and you are done.


Compressed Package Installation (tar.gz):

  1. Execute su command to switch to root user.
  2. Use tar <download-path>/<jdk<version>-<XXbit>.tar.gz -C /usr/java to extract the contents to the installation location. If /usr/java does not exist, you may create the directory using mkdir /usr/java.
  3. Once extracted the contents, you are done with the basic installation. You can follow the section to set the PATH environmental variable.

Binary File Installation:

Packages are available with .bin extension. They can be installed by executing them from the shell.
  1. Switch to root user with su.
  2. Copy the installation file to a location.
  3. Add execution permission to the file using chmod u+x jdk<version>-<XXbit>.bin command.
  4. Execute the file using shell. 
  5. Follow the prompts for installation.
  6. Move the installation folder to /usr/java. Create the folder if it does not exist.
  7. Follow the steps in the section to set the PATH variable.


 

Setting PATH variable:

While installing the package other than from rpm, you will need to set the PATH environmental variable to run the java development tools from console. Extra care should be taken while performing the steps below.


  1. Copy the path where you have java installed; remember to omit the '/' at the end. Usually on /usr/java/jdk<version>. This becomes the path for JAVA_HOME environmental variable.
  2. Edit /etc/profile file using an editor such as vi to set the PATH variable for all users, on their log in. Locate the section where the PATH variable is exported.
  3. Add line defining JAVA_HOME, append $JAVA_HOME/bin to PATH variable and include JAVA_HOME to the export command.
  4. Save the file and exit the editor.
  5. Source the file to reflect the changes in the current console window.
  6. Verify if java command works.