Posts

Showing posts from November, 2011

Pattern matching using java.util.regex

Image
java.util.regex library is available since Java 1.4. It can be used for matching character sequences against pattern specified by regular expression. It has two main classes Pattern and Matcher . An instance of the Pattern class represents a regular expression that is specified in string form in a syntax. Instances of the Matcher class are used to match character sequences against a given pattern. Input is provided to matchers via the CharSequence interface in order to support matching against characters from a wide variety of input sources. Here is the simple example, If you have to extract part of string based on the fixed pattern defined. For example the simple greeting string “Hello Yogesh, Welcome to Hyderabad.” In this sentence, we have to extract two info like name and location ( Yogesh & Hyderabad). First of all we need to define pattern to extract the same information. Java support regular expression for pattern matching, go through Java Pattern documentation to

WeakHashMap by example

Usually we read following explanation about java.util.WeakHashMap “A hashtable-based Map implementation with weak keys. An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use” Unlike java.util.HashMap, WeakHashMap maintain weak reference for the all keys objects and strong reference of value objects. As per features of weak reference, they are more prone to be collection by GC. WeakHashMap runs some kind of unknown services to remove all the entries from collection whose keys are already garbage collected. It means entries in weak hash map may changes over the time. Whenever there  is a need of more memory to your application, GC cycle will execute and its job is to collect all un-referenced objects or weak references. In some cases GC may collected keys of weak hash map as those are a weak references. Once keys are garbage collected , WeakHashMap’s implementation job is to clear all such entries.   See the following program, I have tri

Eclipse Plug-in Development:Working with Project Facet

Image
Every project may have one or more natures. Each nature brings set of properties to the project.For example a Java project has “src” directory and “bin” as build directory or you may want to associate a set of libraries to your custom project. Faceted Project Framework provides a powerful mechanism for extending the capabilities of the Web Tools Platform. Project facets are typically used as a way of adding functionality to a project. When a facet is added to the project it can perform any necessary setup actions such as copying resources, installing builders, adding natures, etc. Facets can also be used as markers for enabling user interface elements. This post explains defining a facet and using it. Prerequisites : Install WST eclipse plug-in Add dependencies to MANIFEST.MF Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Sample Bundle-SymbolicName: com.facet.sample; singleton:=true Bundle-Version: 1.0.0 .qualifier Bundle-Activator: com .facet .sample .Activato

StringBuffer vs StringBuilder : Performance improvement figures

IN previous post StringBuffer vs StringBuilder , I have explained benefits of using StringBuilder over StringBuffer. Here are some performance figure using both for concatenating string. package com.test;   public class StringConcatenateTest { public static void main(String[] args) { System.out.println( "Using StringBuffer" ); long bufferTime = usingStringBuffer(); System.out.println( "Using String Builder" ); long builderTime = usingStringBuilder(); double perc = (( double )(bufferTime-builderTime)/bufferTime)* 100; System.out.println( "Improved in performance = " + perc + "%" ); } public static long usingStringBuffer(){ long startTime = System.currentTimeMillis(); System.out.println( "\tStart time = " + startTime); StringBuffer sb = new StringBuffer(); for ( int i=0;i<1000000;i++){ sb.append( "\tSample