Posts

Showing posts from 2011

Using EnumSet by Example

In previous example I have explained using enum using color constants. Java util collection has provides EnumSet , it is a specialized Set implementation for use with enum types. In this post I have explained usage of EnumSet using an example. A text can have multiple attributes like bold, italic, underlined etc. Given a text can have these properties, defined a sample Text class with bit fields as text properties public class Text { //defining Text properties using int public static final int BOLD = 1<<0; public static final int ITALIC = 1<<1; public static final int UNDERLINED = 1<<2; private int style; public Text( int style){ this .style = style; } public boolean isBold(){ return (BOLD&style)==BOLD; } public boolean isItalic(){ return (ITALIC&style)==ITALIC; } public boolean isUnderlined(){ return (UNDE

Java Enum in details

Java 5 has brought linguistic support for enumerated types. An enum type is a type whose fields consist of a fixed set of constants. The enum provides following features Typesafe Namespace Printable constants values Typical and often enum is being used to replace int constants. For example constants for color codes public static final int RED = 0; public static final int GREEN = 1; public static final int BLUE = 3; public static final int WHITE =4; public static final int BLACK = 6;   This code can be replaced by following enum public enum StandardColor { RED,GREEN,BLUE,WHITE,BLACK } Given the above enum declared, we can use the same to communicate the color preferences. For example we have Window class and it supports setting background color using setBackgroundColor(StandardColor color) . One can call setBackgroundColor method on Window's instance to set background color, as shown in code below public class Wind

Type Inference in Java

As part of Generics , Java language supports type safe programming. It allows a type or method to operate on objects of various types while providing compile-time type safety. The most common use case is while using Java Collection framework. For example if you define a list of string and trying to add integer value to the list, compiler throws error. List list = new ArrayList(); //Compilation Error : he method add(int, String) in the type List is not applicable for the arguments (int) list.add(10); As a developer you may always notice redundancy in defining parameter type information in both the side of declaration. As off Java 1.6, language doesn’t provide any feature to overcome it. Using Static Factory : One possible way to overcome this problem by using static factory methods. If you frequently use typed list or map in application then you can define generic static factory methods which returns you required typed map or list. public static HashMap getMapInstance(){

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