Pages

Tuesday, September 20, 2011

Understand Hashmap Better


Many of us use Map in our code frequently. but very little of us know how the map is different than Array or linkedlist and works internally.
Well, we will explore how HashMap works. all of us know that map stores key value pairs, and we also know that it is better to use map when we need frequent search an item in collection. lets take an example that if we have a 1000 words put in collection and we want to find if a given particular word is present in collection or not, it would be inefficient if we tried to find sequentially looping through all 1000 words and find the matching word.map uses hashing technique which more efficiently narrow down the search and match the word.
.
what is hashing then?
.
Hashing uses a technique called as hashcode. It is nothing but using some algorithm to map object data to some representative integer value, that help to narrow down the search for finding object in collection. lets find how this mechanism works.
So to begin with we will take a simple example of small collection of strings like country name and its currency. assume that we have 5 countries and their currency need to be stored in collection/list. following is the data that we need to keep in map.

.
CountryCurrency
IndiaRupee
FranceEuro
CubaPeso
SwitzerlandSwiss Franc
DenmarkKrone

As I explained earlier that hascode uses a algorithm to find representative integer value of string, in our example we will take simple algorithm as count the length of string. so to put item in hashmap we will calculate it hashcode and put them in corresponding index.
Now assume that map uses two arrays to store key and value pairs respectively in each array as shown below. every item will be placed based on its calculated hashcode as an index item.


HashMap
(hashcode=keylength) IndexKeysArrayValueArrays
1
2
3
4CubaPeso
5
6FranceEuro
7DenmarkKrone
8
9
10BangladeshTaka
11SwitzerlandSwiss Franc

So if we want to find Denmark item in list we will simply count its length which is (7) and get item out from hash table from index 7. so simple isn't it. it also works much faster than comparing with other string. you will wonder how this will work if we have more than one key with same hashcode? i.e. if we have collision? If you noticed that we have wasted few space in the list. you will also have a question now what if we have a word more than has 100 characters long and rest are small? we will end up using lot of waste space.

.
Java uses the same structure as we have seen above, but instead of keeping items in a array it has array referenced to linklist, that means every item in a array points to a linklist called bucket. Now, as you know that two unequal object in Java very much can have equal hashcode, since hashcode is same, bucket location would be same and collision occurs in HashMap, and HashMap uses a linked list to store data objects, so they will be stored in next node of linked list, to explain same example above assume that if we have to find Denmark in map, it will first find its hashcode which is 7 and then it will traverse the linklist at index 7 for matching correct pair of key with its value with method key.equals().


.
Now to avoid empty space in the array, map tries to evenly distribute objects in the list. so it takes the hashcode of the key object and applies its own logic to identify bucket location for storing.please note that it stores whole key value pair object in the linklist, so that it can find it easily afterwords while retrieving. Its makes more clear now how HashMap stores Key value pairs objects, but one doubts arise how HashMap handles if array size grows? objects are stored in bucket based on the length of array to evenly distribute the load so that retrieval should be faster, so how it works if hashmap exceeds its given threshold load factor?
If the size of map exceeds given threshold defined by load factor i.e. if the load factor is .75%, it will try to re-size the map once it is filled by 75% java does this by creating new array of buckets by twice the size of previous map and then start putting all items to new map by calculating new bucket location, this method is called rehashing.
There are still more things to explore in map. Now it is much better that we know how HashMap stores objects internally and has logic of retrieval.

Lastly, If possible before closing, click on ad to support me.

Wednesday, July 20, 2011

Accessing parent window function from modal window

if you create a modal dialog window and try to access any function from parent window, well its not possible, any browser will not allow you to access parent window functions unless it is a normal window. so the alternate possible solution is as below
modal window can return a value to parent window's calling function, so based on its value you can either change parent window variables or call any function. It is only possible when your child modal window is closing.
here it is how.

// parent window JS
retval= window.showModalDialog( "mynewpage.jsp", myArguments, "dialogHeight: 100px; dialogWidth: 400px; edge: Raised; center: Yes; resizable: Yes; status:Yes ; scroll:Yes;");
resetTimer(retval);

// in child window JS i.e. mynewpage.jsp
function closeMe()
{
window.returnValue="60";
window.close();
}

Wednesday, February 16, 2011

ClassNotFoundException or NoClassDefFoundError.. what the hell is going on...

many a times we face "ClassNotFoundException" or  "NoClassDefFoundError"  and then we wonder what the hell is going on... not able to figure out what went wrong... some times we even see that even though class file or jar was exist in classpath, classloader unable to find it...
first try to understand that  "ClassNotFoundException" is thrown when specified class was not found in classpath by classloader, simply its a missing class in classpath or in a jar.
"NoClassDefFoundError" is also similar but with bit difference that it is a runtime error that is thrown by JVM when classloader did not find class definition while loading class caused by  "ClassNotFoundException",  it means that class was present at compile time but does not present at run time". normally both (error and exception) will print the causing class name that was not found by the classloader.

but if you come to notice that even though if class is present in classpath or in a jar and still classloader gives same error then try to look for dependent class imported inside the class that was thrown as "ClassNotFoundException"
Also check is that class has some static block/class variable defined from other package or jar, then make sure even those class/jars are present in classpath


in case of web applications or case where you use framworks like struts/spring or hibernate exceptions are wrapped and you may not come to know actual error that was causing the problem... some such scenarios could be

1. in web applications classloader has child and parent classloaders where classes loaded by parent are accessable to child classloaders but not child to parent. so in such case need to know which classloader is loading the class. for e.g. if you have some class (or class in jar)from shared lib of application referring to web-inf/lib or folder...

2. version compatibility. sometimes it happens that your application jdk is of lower version and jars created in higher version will create unsupported classversion error.. but thrown error could be "ClassNotFoundException"

3. recheck the fully qualified class name that may have typo error, e.g. class.forname or applicationcontext.xml in case of spring where beans are defined.

Wednesday, November 17, 2010

Google CEO interview in web2.0 summit in San Fransisco on the coming mobile revolution

On Monday at the Web 2.0 Summit in San Francisco, Google CEO Eric Schmidt did a wide-ranging interview  in which he shared new details about where Google sees the mobile revolution heading in the years ahead
In his interview Eric give presentation of his next version of Andriod which called as Gingerbread, He has also covered many other features that are coming in and how Googlesees the business opportunity in the mobile world. please have a look at this video.

Google TV

www.google.com
Google TV is a new experience that combines TV, the entire web, and apps -- as well as a way to search across them all. Take a tour, learn how it works, and find out how to get it.