Tuesday, October 11, 2011

Java Serialization(overview)


It is a technique to save the state of an object, so that it can be used at a later time or distributed(eg : RMI)


By default, objects are not serializable. The class has to implement the serializable interface or externalizable interface for its objects to become serializable. There are other ways to serialize an object
but the one mentioned above are the most commonly used
  1. Since serialization operation is for objects and not classes, static member fields are not serialized.
  2. Sometimes we dont want to serialize certain non-static fields. This can be achieved by making them transient. Eg: password
  3. Implementing  externalizable interface gives you more control on how an object can be serialized.
One should be careful while serializing/de-serializing a singleton object as it could lead to multiple
instances of the singleton object. If a serialized object is de-serialized multiple times, you end up with multiple copes of the object. One way to prevent is as follows


public class Singleton implements Serializable {
  
  // This method returns the singleton instance.
  protected Object readResolve() {
   return getInstance();
  }
}
Also if you are using a static field to store the reference to the singleton instance, then you have to override the writeObject() method to serialize it as by default static fields are not serialized.

Sunday, October 9, 2011

Duplicate characters in a string

The function assumes that all characters are same case.
1)countChars() : list the characters and their count
2) checkIfDupChars : checks if String has duplicate characters


 public void countChars(String s){   
      if(s == null || s.length() == 0){   
        return;   
      }   
      final Map<Character, Integer> charCountMap = new HashMap<Character, Integer>();   
      for(int i = 0; i < s.length() ; i++){   
        Character c = s.charAt(i);   
        if(!charCountMap.containsKey(c)){   
          charCountMap.put(c, new Integer(1));   
        }else{   
          final int charCount = charCountMap.get(c).intValue();   
          charCountMap.put(c, new Integer(charCount + 1));   
        }   
      }   
      final Set<Character> keySet = charCountMap.keySet();   
      for(final Character c : keySet){   
        System.out.println("The character "+c+" appears "+charCountMap.get(c)+" times.");      }   
    }   


  public boolean checkIfDupChars(String s){  
         if(s == null || s.length() == 0){  
             return false;  
         }  
         List<Character> charList = new ArrayList<Character>();  
         for(int i = 0; i < s.length() ; i++){  
             Character c = s.charAt(i);  
             if(!charList.contains(c)){  
                 charList.add(c);  
             }else{  
                 return false;  
             }  
         }  
         return true;  
     }  

Saturday, October 8, 2011

Trie data structure, reverse an integer in place

1) http://www.technicalypto.com/2010/04/trie-in-java.html

2) Reverse an integer (java)

The time taken is proportional to the number of digits in the integer.


 public int revNum(int num){  
    if(num == 0) return 0;  
    boolean isNeg = false;  
    if(num < 0){  
      isNeg = true;  
      num = num * -1; //make it + ve  
    }  
    int newNum = 0;  
    while(num != 0){  
       newNum = newNum * 10 + num % 10;  
       num = num / 10;  
     }  
     if(isNeg){  
       newNum = newNum * -1;  
     }  
     return newNum;  
   }