Wednesday, 31 October 2018

Why is Hibernate Optimistic Locking Failure Exception thrown when saving a new entity?

org.springframework.orm.ObjectOptimisticLockingFailureException: Object of class [com.payumoney.paymentUtil.model.EntityTDR] with identifier [389853]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.payumoney.paymentUtil.model.EntityTDR#389853]

Solution :
In the Entity or Model if we are using version column and if we are not mentioned below annotation then we will get this issue

@Generated(GenerationTime.ALWAYS)

Key points:
  1. The relationship between the parent and child is bi-directional one-to-many.
  2. We use optimistic locking with the version column being a timestamp created by MySQL either during insert or during update. On the version field we specify Generated(GenerationTime.ALWAYS) to ensure that the version details are obtained from the database automatically (avoid the time precision issue between Java and MySQL)
  3. During saving a new entity (id = 0), I can see the logs that the entity is being inserted into the database, I can also see the child entities being inserted in the database (via the Hibernate logs). During this process, I can also see the a select is done to get the version details from the database.
  4. Soon after the entities are inserted and the session is being flushed, there is a dirty checking is done on the collection and I see a message in the log that the collection is (). Straight after this, I see an update statement on the parent entity’s table and this is where the problem occurs as the version value used in the update statement is different to what is in the database, the exception is thrown.

Tuesday, 27 March 2018

How to remove the middle of the commit from git local

Rebase or revert are the options. Rebase will actually remove the commit from the history so it will look like that second commit never existed. This will be a problem if you've pushed the master branch out to any other repos. If you try to push after a rebase in this case, git will give you a reject non fast-forward merges error.

Revert is the correct solution when the branch has been shared with other repos. git revert af5c7bf16 will make a new commit that simply reverses the changes that af5c7bf16 introduced. This way the history is not rewritten, you maintain a clear record of the mistake, and other repos will accept the push.

Here's a good way to erase: git rebase -i <commit head>^ That takes you to the commit just before the one you want to remove. The interactive editor will show you a list of all the commits back to that point. You can pick, squash, etc. In this case remove the line for the commit you want to erase and save the file. Rebase will finish its work.

Monday, 13 November 2017

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated ???

If you are getting javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated when you are calling any API in server to server call.

Step 1:
Download the bouncy castle jars according to your Java version using below URL
https://www.bouncycastle.org/latest_releases.html

Step 2: Copy downloaded jars (bcpkix-jdk15on-1.50.jar, bcprov-jdk15on-1.50.jar) into your JRE ext directory.
Example - If you are installed java in C drive
C:\Java-JDK-1.6.0.45\Java\jdk1.6.0_45\jre\lib\ext\

Step 3: Open java.security file available in below path if you are installed java in C drive

C:\pavan\Java-JDK-1.6.0.45\Java\jdk1.6.0_45\jre\lib\security\java.security

add the below command end of the file java.security.
security.provider.2=org.bouncycastle.jce.provider.BouncyCastleProvider

Step 4: 
Use below method to overcome the same while creating the HttpClient object.

Create the HttpClient object using below code and call the API you will get result 100%!!!

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
......
private static HttpClient getHttpClient() {

    try {
        SSLContext sslContext = SSLContext.getInstance("SSL");

        sslContext.init(null,
                new TrustManager[]{new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {

                        return null;
                    }

                    public void checkClientTrusted(
                            X509Certificate[] certs, String authType) {

                    }

                    public void checkServerTrusted(
                            X509Certificate[] certs, String authType) {

                    }
                }}, new SecureRandom());

        SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext,SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);



        HttpClient httpClient = HttpClientBuilder.create().setSSLSocketFactory(socketFactory).build();

        return httpClient;

    } catch (Exception e) {
        e.printStackTrace();
        return HttpClientBuilder.create().build();
    }
}

Tuesday, 10 October 2017

how to remove untracked files from git local

  1. To remove directories, run git clean -f -d or git clean -fd.
  2. To remove ignored files, run git clean -f -X or git clean -fX.
  3. To remove ignored and non-ignored files, run git clean -f -x or git clean -fx.

Wednesday, 28 December 2016

How to change the alias name of JKS file!!!!

Hi All

How to change the alias name of JKS file once we generated?

Step1 : To view what alias is available present in JKS file, use the below command

Open the command prompt and got the location where the jks file is available and execute below command
keytool -list -v -keystore <jks file name>

Example:
keytool -list -v -keystore test.jks

Step2 : once you executed the above command it will ask you the jks password, enter the same then it will display all the details like what all are certificated available in jks, expiry of certificates, alias name..etc..

Step3 : Execute the below command to change the alias name

keytool -keyclone -alias "old alias name" -dest "new alias name" -keystore <jks file>

Example :
keytool -keyclone -alias "pavan" -dest "kumar" -keystore test.jks

once you have execute above command it will ask you the password of jks, enter the same.
Done..!!!!enjoy..


How to change the certificate password

Hi All, after a long time posting again

How to change the certificate password using keytool command

Step1: open to the command prompt and go to the location where the certificate exists.
Step2:  execute the below command

keytool -storepasswd -keystore <jks file name>

Example
keytool -storepasswd -keystore test.jks

Step3: it will ask you the current password enter the same.
Step4: it will ask you the new password enter the new password.
Step5: it will ask you the password again for confirmation enter the same password which is entered  
           in step5.
Step6: thats it!!! your jks file got changed with new password....enjoy!!!!

Tuesday, 2 February 2016

How to use equals( ) and equalsIgnoreCase( ) in Java ?

To compare two strings for equality, use equals( ). It has this general form:
boolean equals(Object str)
Here, str is the String object being compared with the invoking String object. It returns true if the strings contain the same characters in the same order, and false otherwise.
The comparison is case-sensitive. To perform a comparison that ignores case differences, call equalsIgnoreCase( ). When it compares two strings, it considers A-Z to be the same as a-z. It has this general form:
boolean equalsIgnoreCase(String str)

Here, str is the String object being compared with the invoking String object. It, too, returns true if the strings contain the same characters in the same order, and false otherwise. Here is an example that demonstrates equals( ) and equalsIgnoreCase( ):

In one word we can say equals() is case-sensitive, equalsIgnoreCase() is not a case-sensitive.

Below is the example
// Demonstrate equals() and equalsIgnoreCase().
class equalsDemo {
        public static void main(String args[]) {
               String s1 = "Hello";
               String s2 = "Hello";
               String s3 = "Good-bye";
               String s4 = "HELLO";
               System.out.println(s1 + " equals " + s2 + " -> " +
               s1.equals(s2));
               System.out.println(s1 + " equals " + s3 + " -> " +
               s1.equals(s3));
               System.out.println(s1 + " equals " + s4 + " -> " +
               s1.equals(s4));
               System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " +
               s1.equalsIgnoreCase(s4));
       }
}

The output from the program is shown here:
Hello equals Hello -> true
Hello equals Good-bye -> false
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true

Saturday, 30 January 2016

What is GIT Hub and how to use ?

What Is Git?

Git is version control software, which means it manages changes to a project without overwriting any part of that project.

Why use something like Git? Say you and a coworker are both updating pages on the same website. You make your changes, save them, and upload them back to the website. So far, so good. The problem comes when your coworker is working on the same page as you at the same time. One of you is about to have your work overwritten and erased.

Command Line: 
                         The computer program we use to input Git commands. On a Mac, it’s called Terminal. On a PC, it’s a non-native program that you download when you download Git for the first time (we’ll do that in the next section). In both cases, you type text-based commands, known as prompts, into the screen, instead of using a mouse.

  
Repository: 
                  A directory or storage space where your projects can live. Sometimes GitHub users shorten this to “repo.” It can be local to a folder on your computer, or it can be a storage space on GitHub or another online host. You can keep code files, text files, image files, you name it, inside a repository.

Version Control: 
                          Basically, the purpose Git was designed to serve. When you have a Microsoft Word file, you either overwrite every saved file with a new save, or you save multiple versions. With Git, you don’t have to. It keeps “snapshots” of every point in time in the project’s history, so you can never lose or overwrite it.

Commit: 
              This is the command that gives Git its power. When you commit, you are taking a “snapshot” of your repository at that point in time, giving you a checkpoint to which you can reevaluate or restore your project to any previous state.

Branch: 
            How do multiple people work on a project at the same time without Git getting them confused? Usually, they “branch off” of the main project with their own versions full of changes they themselves have made. After they’re done, it’s time to “merge” that branch back with the “master,” the main directory of the project.

Git-Specific Command
                        Since Git was designed with a big project like Linux in mind, there are a lot of Git commands. However, to use the basics of Git, you’ll only need to know a few terms. They all begin the same way, with the word “git.”

1. git config :-
                  Short for “configure,” this is most useful when you’re setting up Git for the first time.
2. git help :-
                   Forgot a command? Type this into the command line to bring up the 21 most common git commands. You can also be more specific and type “git help init” or another term to figure out how to use and configure a specific git command. 
3. git clone  <url>
                   Clones a repository into a newly created directory, creates remote-tracking branches for each branch in the cloned repository (visible using git branch -r), and creates and checks out an initial branch that is forked from the cloned repository’s currently active branch.
                   After the clone, a plain git fetch without arguments will update all the remote-tracking branches, and a git pull without arguments will in addition merge the remote master branch into the current master branch, if any (this is untrue when "--single-branch" is given; see below).

4. git status :-
                   Check the status of your repository. See which files are inside it, which changes still need to be committed, and which branch of the repository you’re currently working on.
5. git add :-
               This does not add new files to your repository. Instead, it brings new files to Git’s attention. After you add files, they’re included in Git’s “snapshots” of the repository.
 i) git add <file name>
              This command is used to add a single file name into repository, that file will be available to commit, if you are not add file to repository you can't able to commit.
ii) git add -u
               This command is used to add all modified files to repository.
iii) git add .
               This command is used to add all modified and newly added files to repository.
5. git commit -m "commit message"
                        Git’s most important command. After you make any sort of change, you input this in order to take a “snapshot” of the repository. Usually it goes.
6. git push origin <branch name>
                        If you’re working on your local computer, and want your commits to be visible online on GitHub as well, you “push” the changes up to GitHub with this command.                      
7. git branch :-
                   Working with multiple collaborators and want to make changes on your own? This command will let you build a new branch, or timeline of commits, of changes and file additions that are completely your own. Your title goes after the command. If you wanted a new branch called “cats,” you’d type git branch cats.
8. git checkout <branch name> :-

                   Switch branches or restore working tree files.
     Ex: If you are current working directory master, if you want to switch from master to sandbox.
              git checkout sandbox
   If you want to restore of current modified files to previous stage use the below command

              git checkout <file name> 
   To revert all modified file to its previous stage use the below command

             git checkout .

9. git pull origin <branch name>
                If you’re working on your local computer and want the most up-to-date version of your repository to work with, you “pull” the changes down from GitHub with this command.
10. git merge <branch name>
               
When you’re done working on a branch, you can merge your changes back to the master branch, which is visible to all collaborators.
  Ex: you are currently working on feature branch i.e your branch xyz name.
would take all the changes you made to the “xyz” branch and add them to the master.
                 git merge xyz
11.  How to delete a branch from local repository ?
         git branch -D <branch name>
12. How to take particular commit from one branch to another branch ?

         git cherry-pick <commit-sha>
      commit-sha - take from original branch which commit you want.
    
  Below is the image where to take commit sha
       (
deff1f5b3a86bea8128e8eb18f689bc74ba0e213)
13. How to save your local changes temporarily in your local repository ?
      git stash
     
     When ever you want to pull out your changes we can use the below command
      git stash apply     
     Above command will pullout your last stashed files.
      git stash list     
      Above command will show all the local stashed commits.
      If you want to pullout specific local commit we can use below command
      git stash apply stash@{0}

      stash@{0}
- this is the latest local stashed commit.
      
{0} - here we can also pass specific stash number to pop out previous changes like                  below.       Ex :- git stash apply stash@{1} 

14. If you want to fix up your latest commit, you can undo the commit, and   
      unstage the files in it, by doing:
      git reset HEAD~1
      Below is the command we can use to reset N number of last commits.
      git reset HEAD~N
      If you want to get rid of your latest commit, and do not want to keep the code
      changes, you can do a "hard" reset.
      git reset --hard HEAD~1

15. To remove untracked files from git use below command
         git clean -fd

Tuesday, 8 December 2015

WebService Client Generation Error with JDK8

java.lang.AssertionError: org.xml.sax.SAXParseException:

Create a file named jaxp.properties (if it doesn't exist) under /path/to/jdk1.8.0/jre/lib and then write this line in it:

javax.xml.accessExternalSchema = all

That's all. Enjoy JDK 8.

Thursday, 20 August 2015

Just 3 Steps to Setting Up a Tomcat Server Log file in Eclipse ?

  • In the servers tab, double-click on the Tomcat Server. You will get a screen called Overview.
  • Click on "Open launch configuration". Click on the "Common" tab.
  • Towards the bottom of the screen you can check the "File" checkbox and then specify a file that can be used to log your console (catalina.out) output.
  • Finally, restart the Tomcat server.

Monday, 3 August 2015

How to add or remove or list certificates from keystore or trustStore in Java ?

                    How to add certificates on keystore in Java is primary questions when you start working on SSL connection and simple answer is keytool utility in Java is used to add or list Certificates into keystore. SSL is industry standard for secure communication between two parties e.g. client and server. SSL offers two benefits, it encrypts data transferred between client and server to make it hard for someone to access and understand in between and SSL also verify identity of two parties in communication and certificates are used for that purpose. SSL Setup in Java comes during various process e.g. Setting up SSL on tomcat, configuring messaging over SSL or JDBC over SSL are some examples of task where you need to deal with keyStore, certificates and trustStores. for those who are not aware of what is a keystore in Java and what is certificates, we will see brief introduction in next section.
Basics of SSL Certificates and Keystore in Java : -
                        When we access a secure site which uses SSL for providing identity and encryption, it provides a certificates which was verified by a trusted third party sites like verisign, godaddy or thwate. by using certificates browser or java clients knows that they talking to the correct site (who it claims to be) and not on redirected proxy site. this step is pretty transparent if you access websites using browser because if certificate is not on browser's trusted store it will ask you to add that certificate and it will be subsequently added, But when you access a secure site using Java program, this step of certificate hand shaking is not transparent to user and certificates are verified form JRE's trustStore. This trustStore is located on JDK Installation directory referred by JAVA_HOME  e.g. JAVA_HOME/jre/lib/security and commonly named as "cacerts".If certificate provided by secure site is present on JRE's trustStore SSL connection would be established but if certificate is not there than Java will throw exception and to solve that you need to add that certiificate into trustStore. keyStore and trustStore is often used interchangeably and same file can act as keystore as well as trustStore it just matter of pointing javax.net.ssl.keyStore and javax.net.ssl.trustStore properties to that file but there is slightly difference between keystore and trustStore. keyStore is used to store individual identity or certificate while trustStore is used to store other parties certificates signed by CA.
How to add ,remove and list certiifcates from Java keystore :-
  • In this article we will see how to add ,remove and list certiifcates from Java keystore using keytool utility.
  • keytool is binary located inside JAVA_HOME/jre/lib/security folder and used for adding, removing and listing certificates. 
 here is step by step example of adding certificates in Java:
Example of listing certificates form Java Keystore :-
                 Before adding new certificates in keystore or truststore its good to see, count and verify already installed certificates. run following keytool command in commonprompt to get a list of certififcates from keystore:
Step 1 :- Open the command prompt, go to the path where the java installed in your machine.
Step 2 :- Run the below command to get the list of certificates available in your cacerts file.
               keytool -list -keystore cacerts
               You see currently keystore "cacerts" holds 77 certificates.



Example of adding Certificate on Java KeyStore :-
Now let's see example of adding certificates into keytstore in Java:

1. Get Certificate: easier way is point your browser to that url and when certificate is presented save it on your local folder or directory say in C:/certificates/test.cer
2. Now go to Security folder of your JRE installation directory. id you have JDK installed than it would be something like C:/Program Files/Java//jdk1.6.0_20/jre/lib/security
3 Execute following keytool command to insert certificate into keystore


keytool -import -keystore cacerts -file test.cer

Now this will print details about certificate and ask you for confirmation of adding certificates:

Trust this certificate? [no]:  y
Certificate was added to keystore
if you approve it by typing "y" certificate will be added into keystore.
Trust this certificate? [no]:  n
Certificate was not added to keystore


password : changeit  - by default.

if you decline it by typing "n" certificate will not be added into keystore.


Important point about SSL, KeyStore and keyTool in Java :- 
1. Certificates are required to access secure sites using SSL protocol or making secure connection from client to server.
2. JRE stores certificates inside keystore named as "cacerts" in folder C:/Program Files/Java/jdk1.6.0_20/jre/lib/security.
3. Common password of keystore is "Changeit"
4. Keytool is used to access keystore in Java and by using keytool you can list, add certificates from keystore.
5. if you are implementing SSL connection on Server side say Tomcat you need both keyStore and trustStore, both can be same file though. keyStore will be used to store server certificate which server will present to client on SSL connection.

That’s all on how to add and list certificates from keyStore or trustStore in java. Keytool utility which comes with JDK installation will help you to create alias, list certificates etc.

Sunday, 2 August 2015

How to print values of an object in Java when you do not have the source code for the class?

                 You can get all fields by Class#getDeclaredFields(). Each returns a Field object of which you in turn can use the get() method to obtain the value. To get the values for non-public fields, you only need to set Field#setAccessible() to true.

Example :-
-------------------------------------------------------------------------------------------------------------------
package com.test;

public class TestBean {
   
    String name;
    String job;
    String company;
    String sal;
   
    //Initialising the properties.
    TestBean() {
        this.name = "java";
        this.job = "developer";
        this.company = "XYZ";
        this.sal = "10000";
    }
   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public String getCompany() {
        return company;
    }
    public void setCompany(String company) {
        this.company = company;
    }
    public String getSal() {
        return sal;
    }
    public void setSal(String sal) {
        this.sal = sal;
    }
}

------------------------------------------------------------------------------------------------------------------ 
package com.test;

import java.lang.reflect.Field;

public class PrintPropertiesFromObject {

    public static void main(String[] args) throws RuntimeException, IllegalAccessException {
       
        TestBean testBeanObject = new TestBean();
       
        for (Field field : testBeanObject.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            String name = field.getName();
            Object value = field.get(testBeanObject);
            System.out.printf("Field name: %s, Field value: %s%n", name, value);
        }
    }
}

 -----------------------------------------------------------------------------------------------------------------
Output :-
Field name: name, Field value: java
Field name: job, Field value: developer
Field name: company, Field value: XYZ
Field name: sal, Field value: 10000

Thursday, 23 July 2015

Optimization techniques in Sets

              Set is a collection of unique objects, it doesn't allow duplicate objects and modification of existing objects. Set types also allow basic operations like adding objects, removing objects, accessing objects, iterating objects but do not allow modification. There are two implementations of the Set interface they are HashSet and TreeSet.

            HashSet gives better performance than TreeSet because , TreeSet is an ordered collection of objects and the objects are sorted while they are inserted in the TreeSet where as in case of HashSet objects are added in an adhoc manner. It is expensive to do all basic operations in TreeSet because it has to compare and sort every object. We can get better performance by using a HashSet and converting  it to a TreeSet later on.

             HashSet and TreeSet are backed by HashMap and TreeMap respectively. Whenever we use a HashSet we can specify an initial capacity and load factor using constructors. The default size for a HashSet is 11 and it's load factor is 0.75. Load factor determines at which capacity HashSet has to be resized. It's internal structure will become double in size when it reaches it's maximum capacity based on load factor. HashSet scales well when it is initialized with proper size and default load factor. When you know the the number of objects to be added, it is better to initialize with that capacity and put load factor as 1.0f. The objects in HashSet are stored and retrieved through hash code which provides constant look up time.  I did not give any bench marks for these two Sets because We don't have many options here to compare and evaluate. We have two Sets to choose. Use TreeSet if you want sorted collection otherwise use HashSet.

The constructors for the HashSet to initialize with proper size are:

HashSet(int initialcapacity)

HashSet(int initialcapacity, float loadfactor)
Key Points :
  • Use HashSet for maintaining unique objects if you don't want thread safe for the collection for all basic(add/remove/access) operations otherwise use synchronized HashSet for thread safe.
  • Use TreeSet for ordered and sorted set of unique objects for non-thread safe collection otherwise use synchronized TreeSet for thread safe.

Tuesday, 21 July 2015

Optimization techniques in Lists

            List types represent an ordered collection of objects. ArrayList, Vector, Stack and LinkedList are the List implementation classes. All  List types support basic operations - adding objects, removing objects, accessing objects and iterating through the list. So which one to choose since all the list implementations support these basic operations? Performance is different for each class based on specific operations. So your choice is driven by the performance and the requirement options. Your requirement could be

1. Thread safe collection
2. Size of collection (large or small collection)
3. Type of operation ( adding, removing, accessing or iterating )


              If you want your collection to be thread safe then Vector or Stack must be used because both have synchronized methods. While ArrayList and LinkedList are not thread safe. Stack is meant for specific LIFO (last in - first out) operations, this can be filtered down based on this specific requirement. If you don't want your collection to be thread safe then you have can choose between ArrayList or LinkedList. General concept from performance point of view is that ArrayList gives better performance when accessing and iterating objects whereas LinkedList gives better performance when adding and removing objects. Although true in most cases, sometimes there is an exception.
conclusion is :
Type of operation ArrayList with out initialization ArrayList with initialization Vector with out initialization Vector with initialization LinkedList
Adding objects at end fast (but slower than initialization) fast fast (but sligtly slower than initialization and slower than ArrayList because of synchronization) fast(but sligtly slower than ArrayList because of synchronization) fast ( but slightly slower than ArrayList and Vector)
Adding objects at middle slow ( slower than when adding objects at last) slow ( slower than when adding objects at last) slow ( slower than when adding objects at last) slow ( slower than when adding objects at last) worse( worse than every operation)
Adding objects at first slow ( slower than when adding objects at last and middle) slow ( slower than when adding objects at last and middle) slow ( slower than when adding objects at last and middle) slow ( slower than when adding objects at last and middle) slow ( slower than when adding objects at last and middle)

                       The initial size for ArrayList and Vector is 10. ArrayList increases its capacity by half approximately whenever its capacity reaches maximum (10) but Vector increases its capacity by double whenever its capacity reaches maximum. That is the reason why ArrayList takes more time than Vector if it is not initialized with proper size though ArrayList is not synchronized. As soon as it reaches its maximum capacity when adding objects, it creates one more bigger array ( with 15 capacity for ArrayList approximately and 20 capacity for Vector) and copies the previous and new objects into new array. Obviously it is expensive to create new array and copy objects. So best approach is to initialize the ArrayList and Vector with proper size using constructors or using ensureCapacity(int capacity) which gives good performance.  If you initialize with proper size then the ArrayList gives better performance than Vector.
                        ArrayList with initialization gives better performance than others because its methods are non-synchronized. Synchronized methods are bit expensive because JVM has to lock the objects whenever it finds synchronized methods.
                        Vector takes slightly more time than ArrayList when you use JDK1.3 Hotspot JVM ,if you are not sure that whether your collection needs to be thread safe or not then it is better to use Vector to have higher safety.
                       You can convert an ArrayList as thread safe collection using Collections.synchronizedList(ArrayList object) but it is more expensive than using a Vector.
                      ArrayList and Vector maintain internal Object array ( Object[]) to store objects. So whenever you add an object, they add it to the end of the array which is  fine as long as it doesn't reach its maximum capacity. If you want to add an object at any other position, it creates a new object array and recopies all the objects which is expensive. That is the reason why adding objects at middle and beginning of collection takes a long time than when it is adding at the end
                     LinkedList gives good performance when adding elements at the end and beginning but it is worse when adding objects at middle because it needs to scan the node whenever it needs to add an object. LinkedList cannot be initialized.

The constructors for ArrayList and Vector to initialize with proper size are
ArrayList( int initialcapacity)
Vector( int initialcapacity)
Vector( int initialcapacity, int capacityIncrement)
You can give incremental capacity in Vector to change the default increment in capacity.
Here is the ListAddTest.java source code.
 ===================================================================
package com.test;

import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Vector;

public class ListAddTest {

    private static final int NUM = 50000;
    private static String[] objs = null;

    public void addLast(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < NUM; i++) {            list.add(objs[i]);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for adding Objects at End: " + (endTime - startTime));
    }
   public void addFirst(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < NUM; i++) {
            list.add(0, objs[i]);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for adding Objects at First : " + (endTime - startTime));
    }
    public void addMiddle(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < NUM; i++) {
            list.add(i / 2, objs[i]);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for adding Objects at Middle : " + (endTime - startTime));
    }
    public void doTest(List list) {
        addLast(list);
        clear(list);
       
        addMiddle(list);
        clear(list);
       
        addFirst(list);
        clear(list);
    }
    public void clear(List col) {
        if (!col.isEmpty())
            col.clear();
    }
    public static void main(String[] args) {
        objs = new String[NUM];
        for (int i = 0; i < NUM; i++) {
            objs[i] = "Object" + i;
        }
        ListAddTest col = new ListAddTest();
        ArrayList collection1 = new ArrayList();
        col.doTest(collection1);

        ArrayList collection1A = new ArrayList(NUM);
        col.doTest(collection1A);

        Vector collection2 = new Vector();
        col.doTest(collection2);

        Vector collection2A = new Vector(NUM);
        col.doTest(collection2A);

        LinkedList collection4 = new LinkedList();
        col.doTest(collection4);
    }
}
 ====================================================================
Here is the Output :
Time taken for adding Objects at End: 0
Time taken for adding Objects at Middle : 101
Time taken for adding Objects at First : 180
Time taken for adding Objects at End: 0
Time taken for adding Objects at Middle : 90
Time taken for adding Objects at First : 190
Time taken for adding Objects at End: 0
Time taken for adding Objects at Middle : 100
Time taken for adding Objects at First : 180
Time taken for adding Objects at End: 10
Time taken for adding Objects at Middle : 90
Time taken for adding Objects at First : 180
Time taken for adding Objects at End: 10
Time taken for adding Objects at Middle : 1091
Time taken for adding Objects at First : 0

====================================================================== 
Removing objects :
  1. All classes take approximately same time when removing objects from end
  2. ArrayList and Vector give similar performance with slight difference because of JDK1.3 Hotspot JVM.
  3. LinkedList  gives worst performance when removing objects from middle (similar to adding objects at middle).
  4. LinkedList gives better performance when removing objects from the beginning.
  5. Only LinkedList gives better performance when removing objects from the beginning.
======================================================================
Here is the ListRemoveTest.java source code
package com.test;

import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Vector;
import java.util.Arrays;

public class ListRemoveTest {

    private static final int NUM = 20000;
    private static Object[] objs = null;

    public void removeLast(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = NUM; i > 0; i--) {
            list.remove(i - 1);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for removing Objects at End: " + (endTime - startTime));
    }
    public void removeFirst(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < NUM; i++) {
            list.remove(0);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for removing Objects at First : " + (endTime - startTime));
    }
    public void removeMiddle(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < NUM; i++) {
            list.remove((NUM - i) / 2);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for removing Objects at Middle : " + (endTime - startTime));
    }
    public void doTest(List collection) {
        collection.addAll(getList());
        removeLast(collection);
        clear(collection);
 
        collection.addAll(getList());
        removeMiddle(collection);
        clear(collection);

        collection.addAll(getList());
        removeFirst(collection);
        clear(collection);
    }
    public void clear(List col) {
        if (!col.isEmpty())
            col.clear();
    }
    public List getList() {
        objs = new Object[NUM];
        for (int i = 0; i < NUM; i++) {
            objs[i] = new Object();
        }
        return Arrays.asList(objs);
    }
    public static void main(String[] args) {
        ListRemoveTest col = new ListRemoveTest();
      
        ArrayList collection1 = new ArrayList();
        col.doTest(collection1);

        Vector collection2 = new Vector();
        col.doTest(collection2);

        LinkedList collection4 = new LinkedList();
        col.doTest(collection4);
    }
}
=======================================================================
Here is the Output :
Time taken for removing Objects at End: 10
Time taken for removing Objects at Middle : 20
Time taken for removing Objects at First : 20
Time taken for removing Objects at End: 0
Time taken for removing Objects at Middle : 20
Time taken for removing Objects at First : 30
Time taken for removing Objects at End: 10
Time taken for removing Objects at Middle : 150
Time taken for removing Objects at First : 0
=======================================================================
The conclusion is :
  • ArrayList and Vector give best performance because they access objects using index. Vector takes slightly more time but it is negligible.
  • LinkedList gives worst performance  when accessing objects at end and middle because it has to scan nodes to access objects.
=========================================================================
Here is the ListAccessTest.java source code
package com.test;

import java.util.List;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Vector;
import java.util.Arrays;

public class ListAccessTest {

    private static final int NUM = 25000;
    private static Object[] objs = null;

    public void getFromLast(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = NUM; i > 0; i--) {
            list.get(i - 1);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for getting Objects at Last: " + (endTime - startTime));
    }
    public void getFromFirst(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < NUM; i++) {
            list.get(0);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for getting Objects at First : " + (endTime - startTime));
    }
    public void getFromMiddle(List list) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < NUM; i++) {
            list.get(NUM / 2);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Time taken for getting Objects at Middle : " + (endTime - startTime));
    }
    public void doTest(List list) {
        list.addAll(getList());
        getFromLast(list);
        getFromMiddle(list);
        getFromFirst(list);
    }
    public void clear(List col) {
        if (!col.isEmpty())
            col.clear();
    }
    public static List getList() {
        objs = new Object[NUM];
        for (int i = 0; i < NUM; i++) {
            objs[i] = new Object();
        }
        return Arrays.asList(objs);
    }
    public static void main(String[] args) {
        ListAccess col = new ListAccess();

        ArrayList collection1 = new ArrayList();
        col.doTest(collection1);

        Vector collection2 = new Vector();
        col.doTest(collection2);

        LinkedList collection4 = new LinkedList();
        col.doTest(collection4);
    }
}
 =======================================================================
Here is the Output :
Time taken for getting Objects at Last: 0
Time taken for getting Objects at Middle : 0
Time taken for getting Objects at First : 10
Time taken for getting Objects at Last: 0
Time taken for getting Objects at Middle : 0
Time taken for getting Objects at First : 0
Time taken for getting Objects at Last: 222
Time taken for getting Objects at Middle : 420
Time taken for getting Objects at First : 0
=======================================================================
Iterating collection:
                  Iterating collection using all three types of classes ,ArrayList ,Vector and LinkedList gives similar performance because they need not do any extra work  they simply iterate one by one. So I did not give any benchmark results. You can use any Iterator . But using ListIterator gives more flexibility than Iterator and Enumeration. You can traverse both sides.
 Key Points :
  • Use ArrayList with proper initialization if you don't want thread safe for the collection whenever you  add/remove/access objects at end and middle of collection.
  • Use Vector with proper initialization if you want thread safe for the collection whenever you  add/remove/access objects at end and middle of collection.
  • Use LinkedList if you don't want thread safe for the collection whenever you  add/remove/access objects at beginning of collection.
  • Use synchronized LinkedList if you want thread safe for the collection whenever you add/remove/access objects at beginning of collection.
  • Use ListIterator than Iterator and Enumeration for List types

Monday, 20 July 2015

Optimization techniques in Maps

          Map is a collection of key and value object associations. You can do all basic operations as in Lists and Sets such as adding , removing ,and accessing key-value pairs. There are four types of Map implementations they are HashMap, Hashtable, WeakHashMap and TreeMap.

         HashMap, Hashtable and WeakHashMap have similar implementations. TreeMap is meant for sorted collection, this can be filtered down based on the requirement. Then we have other three types to choose from. The choice  again depends upon your requirement. Your requirement could be

1.Thread safe
2.Type of operation ( basic operations )

HashMap and  WeakHashMap are not synchronized whereas Hashtable is synchronized.

          WeakHashMap is a special purpose map which uses an internal hashtable. When there are no more references to key object except weak reference maintained by WeakHashMap, the garbage collector reclaims the key object and mapping between key object and value object is also reclaimed, if the value object does not have any other references then the value object is also reclaimed by the garbage collector.

          If you want your Map type collection to be thread safe, then you need to use Hashtable otherwise use HashMap. HashMap gives better performance than Hashtable because of it's non-synchronized methods. The reason I did not give any bench marks for Map types is that it is pretty straight forward to choose Map type depending on requirement .

        You can improve performance by using proper initial size and load factor in the constructor for all types of Map types except TreeMap.

The constructors are

HashMap(int initialcapacity)
HashMap(int initialcapacity, float loadfactor)
Hashtable(int initialcapacity)
Hashtable(int initialcapacity, float loadfactor)
WeakHashMap(int initialcapacity)
WeakHashMap(int initialcapacity, float loadfactor)

            When the number of objects exceed loadfactor capacity, then the capacity of the class increases to (2*capacity + 1). The default load factor is 0.75.  

            All these classes work in accordance with hash values which are used to identify the value objects. The key objects are converted to integer called hash code by using hashing algorithms which is used as an index for value objects. Hash code must be same for two equal objects, i.e. when tested with the equals() method,it must return true.  The hash code is determined by using hashCode() method. The hash code of a collection is determined by cumulating all the hash codes of the associated objects. 
Key Points :
  • Use HashMap for non-thread safe map collection otherwise use Hashtable for thread safe collection. 
  • Use TreeMap for non-thread safe ordered map collection otherwise use synchronized TreeMap for thread safe.

Sunday, 19 July 2015

Performance improvement techniques in Object creation

 This topic illustrates the performance improvement techniques in object creation with the following sections:
  •     Overview of Objects
  •     Optimization techniques in Object creation
  •     Key Points
Overview of Object creation :
         Object contain data and methods to manipulate the data.  Whenever we create an object there is an overhead involved. Now let us look at an example to understand the overall process :

class ObjectOne{
    int a;

    ObjectOne(){
        a=999;
    }
    int square(){
        return a*a;
    }

    String string (String str){
        return "hello"+str;
    }

}

class ObjectTwo extends ObjectOne{
    String name ;

    ObjectTwo(){
        name ="rr";
    }
  
    public static void main(String args[]){
        ObjectTwo t2 = new ObjectTwo();
    }
}

So now when object t2 is created the following steps are involved :
    Memory is allocated to all the variables
    All super class variables are also allocated memory
    All sub class variables, super class variables are initialized .
    The constructor is invoked.

So whenever we create an object the above steps are repeated which take considerable resources so it is very important to decide whether creating a new object is required or not.

And now let us look at where the objects are placed in memory :
                All objects are placed on heap, their address on the heap is stored in the stack. All class  variables are stored in the method area. All primitive data types are stored on the stack.

Note: This section assumes that reader has some basic knowledge of Java.

Optimization techniques in Object creation :
    Avoid creating objects in a loop.
    Always try to use String literals instead of String objects.

Eg . String str1 = "Hello I am here ";   //String literal
        String str2= "Hello I am here ";   //String literal
        String str3 = new ("Hello I am here ");  //String Object

When we create a String without the new operator and if the content is already existing it uses a single instance of the literal instead of creating a new object every time.
  • Never create objects just for accessing a method.
  • Whenever you are done with an object make that reference null so that it is eligible for garbage collection.
  • Never keep inheriting chains long since it  involves calling all the parent constructors all along the chain until the constructor for java.lang.Object is reached.
  • Use primitive data types rather than using wrapper classes.
  • Whenever possible avoid using class variables, use local  variables since accessing local  variables is faster than accessing class variables.
  • Use techniques such as lazy evaluation. Lazy evaluation refers to the technique of avoiding certain computations until they are absolutely necessary. This way we put off certain computations that may never need to be done at all.
  • Another technique is Lazy object creation : i.e. delaying the memory allocation to an object till it is  not being put into use. This way a lot of memory is saved till the object is actually put in to use.
Key Points
  •     Avoid creating objects in a loop.
  •     Use String literals instead of String objects (created using the 'new' keyword) if the content    is same.
  •     Make used objects eligible for garbage collection.
  •     Do not keep inheritance chains long.
  •     Accessing local variables is faster than accessing class variables
  •     Use lazy evaluation, lazy object creation whenever possible.

Thursday, 16 July 2015

Performance improvement techniques in Serialization

This topic illustrates the performance improvement techniques in Serialization with the following sections:
  •     Overview of Serialization
  •     Optimization with 'transient'
  •     Key Points
Overview of Serialization
                      Serialization is the process of writing complete state of java object into output stream, that stream can be file or byte array or stream associated with TCP/IP socket.

Deserialization is the process of reading back that serialized java object stream from input stream.

A java object is serializeable and deserializeable if that class follows the following rules

A) The java class must implement java.io.Serializable interface or java.io.Externalizable interface or inherit that implementation from any one of it's super class implementation.

B) All instance variables of that class must implement Serializable interface or Externalizable interface or inherit from one of it's super class.

All primitive data types and some of standard java API classes are serializable. You need not explicitly implement Serializable or Externalizable interfaces for those classes. Serialization process ignores class (static) variables.

Externalizable interface allow to do your own custom implementation of serialization. In this section,  focus is only on Serializable interface.

We will talk initially about Serializable interface. This is a marker interface and does not have any methods. All major java technologies like RMI, EJB are based on serialization process to pass the objects through network. These technologies implicitly do all the serialization work for you. You need to simply implement the java.io.Serialzable interface, but If you want to do your own serialization, that is reading from or writing to streams, ObjectInputStream and ObjectOutputStream  can be used.

These methods help to write into stream and read from stream

ObjectInputStream.readObject();                    // to read object

ObjectInputStream.writeObject(Object obj);  // to write object

Initially, We need to understand the default mechanism of serialization process in order to improve performance

The default mechanism:
                     When you write or read an object to a file or network or other stream using serialization process, It writes/reads the complete object state that means it writes the object, it's instance variables, and  super class instance variables except transient variables and class (static) variables. Look at this object hierarchy.



                               In this class hierarchy, when I write CorporateEmployee object into file and and read from that file, Initially Address is called, second HomeAddress is called, third Employee is called and finally CorporateEmployee is called. So Total object hierarchy will be written into file except transient and class (static) variables. Initially super class will be called and so on till end of heirarchy. You need to keep an eye on this mechanism and act up on that, otherwise you will end up with writing everything. The next section explains how to avoid unnecessary data in to streams and improve performance.

Note: This section assumes that reader has some basic knowledge of Java.

                        Variables that have access modifier  'transient'  will not be read from or written into streams. It gives facility to avoid writing unnecessary data into streams. In other words, it boosts the performance by avoiding writing unnecessary data into streams.
Here is the code snippet to show the Serialization process with transient and non transient variation bench marks
 ------------------------------------------------------------------------------------------------------------
package test;

import java.util.Vector;
import java.io.*;

public class SerializationTest {

    static long start, end;
    OutputStream out = null;
    InputStream in = null;
    OutputStream outBuffer = null;
    InputStream inBuffer = null;
    ObjectOutputStream objectOut = null;
    ObjectInputStream objectIn = null;

    public Person getObject() {

        Person p = new Person("SID", "austin");
        Vector v = new Vector();

        for (int i = 0; i < 7000; i++) {
            v.addElement("StringObject" + i);
        }
        p.setData(v);
        return p;
    }

    public static void main(String[] args) {

        SerializationTest st = new SerializationTest();
        start = System.currentTimeMillis();
        st.writeObject();
        st.readObject();
        end = System.currentTimeMillis();
        System.out.println("Time taken for writing and reading :"
                + (end - start) + "milli seconds");
    }
    public void readObject() {
        try {
            in = new FileInputStream("c:/temp/test.txt");
            inBuffer = new BufferedInputStream(in);
            objectIn = new ObjectInputStream(inBuffer);
            objectIn.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (objectIn != null)
                try {
                    objectIn.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
    public void writeObject() {
        try {
            out = new FileOutputStream("c:/temp/test.txt");
            outBuffer = new BufferedOutputStream(out);
            objectOut = new ObjectOutputStream(outBuffer);
            objectOut.writeObject(getObject());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (objectOut != null)
                try {
                    objectOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}

class Person implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private String name;
    private Vector data;
    private String address;

    public Person(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public String getAddress() {
        return address;
    }

    public Vector getData() {
        return data;
    }

    public String getName() {
        return name;
    }

    public void setData(Vector data) {
        this.data = data;
    }
}
-------------------------------------------------------------------------------------------------------------
It writes the Person Object into file and reads from that file.

The output is :
Time taken for writing and reading : 390 milli seconds
If I use 'transient' modifier for the Vector in the Person Object, then the output is
Time taken for writing and reading : 110 milli seconds

It almost increases the speed more than 3 times.
You need to use 'transient' keyword for unnecessary variables to increase performance.

Key Points
  • Use 'transient' key word for unnecessary variables that need not be read from/written into streams.
  • When you write RMI, EJB or any other technologies that uses built in Serialization to pass objects through network, use 'transient' key word for unnescessary variables.
  • Class (static) variables ignores by Serialization process like 'transient' variables.