Wednesday, December 8, 2010

Hashcode implementation and Hashing Concept

What is hash code ?
Hash code is integer number that is return from hash code method of object and will be used in hashing concept for collections to identify the object buckets.

Example :

public class HashcodeImplementation {
      private String name = null;

      public String getName() {
            return name;

      public void setName(String name) {
   = name;

      public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;

Implementing hash code:

1. Hash code method needs to overridden if equals method implemented in object and it is not vice-versa.

2. Same hash value can be returned for all objects. But it is discouraged as per concept of hashing.

3. If two objects are equal by Equals method then hash code needs to be same for those objects otherwise hashing will be failed and it is suggested that field those are used in Equals method needs to be used in hash code method implementation.

4. In Hash type collections, hash code will be used to maintain same value objects in same bucket and those will be separated and identified by equals’ method.

5. If only equals method is implemented then hash code will be different for same content objects so these object keys can’t retrieve the stored values form hash collections (HashSet, Hashtable, HashMap, etc.).

Concept of hashing:

Hashing is concept to store and retrieve the objects in hash collections.

And it uses the following methods for hashing.

1. Hash code to find the object bucket
2. Equals method to find the equivalent object.

As per this concept, different objects with same hash code will be stored in same bucket and object with same value will not found other bucket if it happens, values can’t be retrieved from hash collections using these object keys.

please see following class only with equals method.

package com.lenin.test;

import java.util.HashMap;
import java.util.Map;

public class HashcodeImplementation {
      private String name = null;

      public String getName() {
            return name;

      public HashcodeImplementation(String name) {
   = name;

      public String toString() {

      public boolean equals(Object obj) {
            if (this == obj) {
                  return true;
            if (obj == null) {
                  return false;
            if (!(obj instanceof HashcodeImplementation)) {
                  return false;
            HashcodeImplementation other = (HashcodeImplementation) obj;
            if (name == null) {
                  if ( != null) {
                        return false;
            } else if (!name.equals( {
                  return false;
            return true;

      public static void main(String[] args) {
            HashcodeImplementation t1 = new HashcodeImplementation("Lenin");
            HashcodeImplementation t2 = new HashcodeImplementation("Lenin");
            System.out.println(t1.hashCode() == t2.hashCode());
            Map map = new                HashMap();
            map.put(t1, t1);
            map.put(t2, t2);
            HashcodeImplementation t3 = new HashcodeImplementation("Lenin");
            HashcodeImplementation t4 = map.get(t3);

            if (t4 != null) {
                  System.out.println("No need to implement hashcode.");
            } else {
                  System.out.println("Hey it is null.");

Output :

Hey it is null.

Tuesday, November 16, 2010

Http Request Headers

To get client browser information :
String client = request.getHeader("User-Agent");

To get content length :
String content = request.getHeader("Content-Length");

To get router hops allowed for request :
String maxForwards = request.getHeader("Max-Forwards");

To get accepted encoding details :
String encoding = request.getHeader("Accept-Encoding");

To get language details :
String language = request.getHeader("Accept-Language");

To get content-type or mime type details :
String mime = request.getHeader("Accept");

To get character set details :
String characterSet = request.getHeader("Accept-Charset");

To get connection status :
String status = request.getHeader("Connection");

Monday, November 15, 2010

Can I map multiple URLs to single servlet?

Yes, We can map multiple urls to a single servlet.
Here i am providing the three ways :

First : By giving another servlet-mapping entry like:
Second : By giving two different name entries for single servlet
Three : By giving another url-pattern in servlet mapping but in not support in all servers


TCP (Transmission Control Protocol)

TCP is a 16-bit port number used to identify a specific software program on the server hardware.
Port numbers starts from 0 to 65535 and in this 0 to 1023 are reserved for well known services.

Some import port numbers:
  21 - FTP (File Transfer Protocol)
  23 - Telnet
  25 - SMTP (Simple Mail Transfer Protocol)
  37 - Time
  80 - HTTP (Hyper Text Transfer Protocol)
110 - POP2 (Post Office Protocol 2)
443 - HTTPS (Hypertext Transfer Protocol Secure)

Sunday, November 14, 2010

Java : Best way to declare a static list constant

public class MyListConstant {
      public static final List CONSTANT = Collections.unmodifiableList(getConstant());

      private static List getConstant() {
            List constant1 = new ArrayList();
            return constant1;

  1. It is better to use Array constants instead of list constants.
  2. List constant need to be created with unmodifiableList method otherwise it will modified with its set method.
CONSTANT.set(1, "replaced");

Friday, November 12, 2010

Java : Phone number validation using regular expression

package com.lnn.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PhoneNumberValidation {
      private static final Pattern PhoneNumberPattern = Pattern.compile("[0-9]{3}[-]{0,1}[0-9]{4}");

      public static boolean isValidPhoneNumber(String phone) {
            Matcher matcher = PhoneNumberPattern.matcher(phone);
            return matcher.matches();
      public static void main(String[] args) {

Java Script Regular Expression to restrict repetitive characters in a string

Following java script regular expression will restrict the numbers that contains 4 repetitive digits in a number.

function verifyAccessCode(){
    var val = document.forms[0].textbox.value;
    var exp1 = new RegExp("1111");
    var exp2 = new RegExp("2222");
    var exp3 = new RegExp("3333");
    var exp4 = new RegExp("4444");
    var exp5 = new RegExp("5555");
    var exp6 = new RegExp("6666");
    var exp7 = new RegExp("7777");
    var exp8 = new RegExp("8888");
    var exp9 = new RegExp("9999");
    var exp10 = new RegExp("0000");
    if(exp1.test(val) || exp2.test(val) || exp3.test(val) || exp4.test(val) || exp5.test(val)
        || exp6.test(val) || exp7.test(val) || exp8.test(val) || exp9.test(val) || exp10.test(val)){
        return false;
    return true;

Tuesday, October 5, 2010

Javac Recursive compilation

To compile all the files in a directory including subfolders.
Use the following command :
for /R %a in (*.java) do (javac "%a")

Example :
if you have files in folder C:\A with subfolder B and B cotains C folder.
C:\A>for /R %a in (*.java) do (javac "%a")

this will compile files in A, B and C folders.