Thursday, January 22, 2009

HashMap and null keys

most of us know a hashMap allows null keys and also null values , but what if we add two values corresponding to same null key

      map.put(null,null);
map.put(null,"D");
map.put(null,"E");

What will be the output of
System.out.println(map)

will for the null key it will print the last key value pair , i.e. as per my understanding the null key is unique and on adding more value elements to the same null key will override the old values

Convert HashMap to HashTable

How will you convert a HashMap having null values and/or keys to a hash table ?


import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashTableHashMap
{

public static void main(String[] args) {
Map map = new HashMap();
map.put("a",null);
map.put("b","B");
map.put(null,"D");
System.out.println(map);
Hashtable ht = new Hashtable();
Set set = map.entrySet();
Iterator itr = set.iterator();
while(itr.hasNext()){
Map.Entry entry = (Map.Entry)itr.next();
Object key = entry.getKey();
Object val = entry.getValue();
if(key==null){
key = ""+null; // Or whatever you want
}
if(val==null){
val = ""+null; // Or whatever you want
}
ht.put(key,val);
}
System.out.println(ht);
}
}

Monday, January 19, 2009

iPhone apps using java

hi
this video tutorial at google provides good insight
http://developerlife.com/theblog/?p=842

Tuesday, January 6, 2009

JSF page refresh

To refresh a JSF page the following piece of code is useful

FacesContext context = FacesContext.getCurrentInstance();
String viewId = context.getViewRoot().getViewId();
ViewHandler handler = context.getApplication().getViewHandler();
UIViewRoot root = handler.createView(context, viewId);
root.setViewId(viewId);
context.setViewRoot(root);


If your page is not getting refreshed due to browser caching

 FacesContext facesContext = event.getFacesContext();
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
response.addHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "no-cache");
response.setHeader("Cache-Control", "no-store");
response.addHeader("Cache-Control", "must-revalidate");

Monday, January 5, 2009

Data Structures

Few good links for Data Structures
  • C API : http://code.google.com/p/c-generic-library/
  • google collections : http://code.google.com/p/google-collections/
  • java listenable collections : http://code.google.com/p/jalico/
  • JRDF : http://code.google.com/p/jrdf/
  • J5Collections : http://code.google.com/p/j5collections/
  • Stanford video tutorial : http://in.youtube.com/view_play_list?p=84A56BC7F4A1F852
  • Book : http://www.brpreiss.com/books/opus5/html/page257.html
More links

Thursday, December 11, 2008

How to empty a StringBuffer

We know that StringBuffer is reusable , We may want to empty a String Buffer ( lets say at end of a loop )
so how do you do it ?

The easiest way i came across use the method setLength()
public void setLength(int newLength)
You may have the case like
StringBuffer sb = new StringBuffer("HelloWorld")
// after many iterations and manipulations
sb.setlength(0);
// reuse sb

Please note you cant say
StringBuffer sb = "";
You will get a type mismatch error saying that "Cannot assign a value of type String to StringBuffer"
Makes sense as StringBuffer is not String , and they both inherit from java.lang.Object