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

Monday, March 6, 2017

[Performance] java Map.containsKey() redundant when using map.get()


In most of our code, we generally use HashMap.

 

Most of the operation we do:

·         Prepare HashMap to process.

·         Process the HashMap.

·         Retrieve data from Map.

 

Following use-case needs to be avoided:

Most of the time to avoid NPE (NullPointerException) code is getting handled in below way:

 

 

 

          dataMap.contains(key) ? dataMap.get(key) : someObject;

 

 

Avoid using above as it may impact performance because both below APIs do same operation:

·         containsKey()

·         get(key)

 

SO optimized code for avoiding redundant Map processing:

 

 

  Object  mappedValue = dataMap.get(key);       // fetch only once

 (mappedValue == null) ? someObject: mappedValue;

 

 

 

This is definitely useful for dealing bigger DataSet.

 

Also don't forget to use collection.clear(); if data is no more required.

 

 

III

Best Regards,

Abhjeet.

 

Thursday, September 12, 2013

Injectable Singleton - APIDesign


Using Service Loader along with Singleton Design is really not possible.
Because Service Loader always going to return new object after loading.

To overcome this following is the useful approach to design such requirement.

This is about looking for :
  • Always getting Single Object 
  • Using Service Loader
Refer:
Injectable Singleton - APIDesign

Thursday, June 13, 2013

Eclipse Performance Enhancement!

Hi,

Most of the time we suffer from Eclipse responsiveness or performance.

Below is the link for changing Eclipse Memory settings in "eclipse.ini" file:

Saturday, February 18, 2012

Differentiate : ClassNotFoundException & NoClassDefFoundError

It is important to keep two different exceptions strait in our head in this case:
  1. java.lang.ClassNotFoundException This exception indicates that the class was not found on the classpath. This indicates that we were trying to load the class definition, and the class did not exist on the classpath.
  2. java.lang.NoClassDefFoundError This exception indicates that the JVM looked in its internal class definition data structure for the definition of a class and did not find it. This is different than saying that it could not be loaded from the classpath. Usually this indicates that we previously attempted to load a class from the classpath, but it failed for some reason - now we're trying again, but we're not even going to try to load it, because we failed loading it earlier. The earlier failure could be a ClassNotFoundException or an ExceptionInInitializerError (indicating a failure in the static initialization block) or any number of other problems. The point is, a NoClassDefFoundError is not necessarily a classpath problem.

Cheers!


Tuesday, July 14, 2009

How to call my own JavaScript functions on onLoad or onUnLoad event???

While working on the JSP or HTML pages, we are having some requirement to call our own JavaScript functions after execution of existing onLoad functions.

Also most of the time scenario is like - we are including different files & loading JavaScript files(may be 3rd party). Due to this we are not able to know JavaScript function is getting called on the onLoad or onUnload event for the browser window.

And QUESTION arises : “How to call our own JavaScript functions on onLoad or onUnLoad event?”

Here is the ANSWER for that:

Just add the following JavaScript functions to your own file(JSP/ASP/PHP/JS).

1.       addLoadEvent() : This function will append call of your function to existing queue for onLoad event of browser window.

2.       addUnLoadEvent() : This function will append call of your function to existing queue for onUnload event of browser window.

 

function addLoadEvent(func) {

  var oldonload = window.onload;

  if (typeof window.onload != 'function') {

    window.onload = func;

  } else {

    window.onload = function() {

      if (oldonload) {

        oldonload();

      }

      func();

    }

  }

}

 

function addUnLoadEvent(func){

      var oldOnUnLoad = window.onbeforeunload;

      if (typeof window.onbeforeunload != 'function') {

                window.onbeforeunload = func;

      } else {

      window.onbeforeunload = function() {

      if (oldOnUnLoad) {

            oldOnUnLoad();

            }

            func();

         }

      }

}

 

How to call these functions:

 

In your files make a call to these functions as –

 

addLoadEvent(function(){

                alert(‘Calling myFunction...’);

                myFunction();

                alert(‘ Hurray, myFunction gets called!’);

});

 

addOnLoadEvent(function(){

                alert(‘Calling myFunction...’);

                myFunction();

                alert(‘ Hurray, myFunction gets called!’);

});

 

You can copy & paste these functions to your files & use in the similar way.

Also you can modify the logic of function gets called.

 

Thursday, May 8, 2008

Java IDE selection :)

This article contains mostly the links of articles which are about the IDE selection & comparison of their features.

Java developers are generally used to with the IDEs : Eclipse, NetBeans, Intellij IDEA.

Most of the developers use Eclipse & Jetbrains' Intellij IDEA.

So lets view the following link to compare those.

http://www.ibm.com/developerworks/library/os-ecidea/?ca=dgr-lnxw06IDEA2Eclipse

http://trusoftware.blogspot.com/2006/11/eclipse-vs-intellij-idea.html

http://www.javafaq.nu/java-article1009.html

http://docs.codehaus.org/display/GRAILS/IDEA+Integration

http://memo.feedlr.com/?p=4

http://librenix.com/?inode=5533

http://blogs.jetbrains.com/idea/2006/11/eclipse-vs-intellij/

http://blog.tremend.ro/2006/08/10/intellij-idea-versus-eclipse-part-i/

Thursday, March 6, 2008

New version resin 3.1.3 pro is more stricter reagarding JSP/Servlet specification

I came to know about this while upgrading the resin server.
Resin is stricter regarding the specification of JSP & servlet as compared to other web-containers like Tomcat.
Most of the JSP pages were created using the custom tags, so those were throwing exceptions.
Resin is strict about the valid return values in Class file of Tags and the value which we mention in JSP.

Wednesday, February 6, 2008

How to run javac 1.5 (or beyond) compiler for JSP compilation in Tomcat 5.5 with generics enabled (and other Java 1.5 only features like autoboxing)?

Most of web applications are using Tomcat as a container & Java as these are open source & much more information available for their usage.
Apache's Tomcat & Sun's Java are coming with their newer versions so we are going for their new features.
Like in Java 1.5 has come with its newer features of Autoboxing & Generics which made programmers' life simpler but while upgrading our applications it becomes difficult. ;)

I came across this problem while upgrading my application with Java(1.5). I have used Tomcat 5.5.9 to run my web-application. This version of Tomcat is compiling JSP with Java(1.4) even if I have only JDK 1.5 installed on my machine.
Due to this my changes in JSP regarding Autoboxing won't getting compiled & tomcat started throwing exception for JSP which is having use of Java 1.5 features.

So to make them compile with Java 1.5 we have to follow the steps:
[Step 1] Copying tools.jar, ant.jar, ant-launcher.jar to <TOMCAT_HOME>\common\lib & removing jasper-compiler-jdt.jar
You will get the tools.jar from <JAVA_HOME>\lib & other two ant related jars from lib folder of ANT. Better to have latest updated Ant.

[Step 2] Modifying the web.xml located @ <TOMCAT_HOME>\conf\

compilerSourceVM - What JDK version are the source files compatible with? (Default JDK 1.4)
compilerTargetVM - What JDK version are the generated files compatible with? (Default JDK 1.4)
* You have to modify JDK version to 1.5 for compliation of JSP with JDK(1.5).

To more info about above parameters you have to visit Apache Tomcat 5.5 Documentation.
The above parameters are to be added as shown in following code:
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>compilerSourceVM</param-name>
<param-value>1.5</param-value>
</init-param>
<init-param>
<param-name>compilerTargetVM</param-name>
<param-value>1.5</param-value>
</init-param>

<load-on-startup>3</load-on-startup>
</servlet>