Skip to main content

Posts

Showing posts from 2013

Using HTML5 SessionStorage in forms.

The two local storage mechanisms in HTML5, 'LocalStorage' and 'SessionStorage' are really nifty when you need to store the data temporarily but don't want to involve any server side interaction.

I wanted to create a small HTML-form page which would allow user to enter data in various fields and on submitting the form, just show the data submitted in a text-format (easy for copy-paste). And if needed allow the data to be edited by repopulating the data in the fields.

Something like this:

On submitting, the entered data shows up like this:

On reloading the page, once again we get the first page with data populated in the fields. Pretty cool eh?

I decided to use sessionStorage for this. Idea is very simple (and you will notice that implementation is as simple!).

Before starting with sessionStorage or JS code, let's take a look at the form HTML code:


<form id="bct" >
<label>Problem Description/Current Behavior:</label>
<textarea name=&q…

How does HashMap work in Java? (HashMap internals)

HashMaps are probably one of the most used and definitely one of the least understood Java classes. In this post let's look at the source code of HashMap.java to understand how is data inserted and retrieved from a HashMap.

We know that HashMaps work on the principle of 'hashing' and put(key, value) is used to store a key and corresponding value and get(key) is used to retrieve the value for given key.

Below image shows how HashMap stores the data. The blue squares represent the 'bucket' or indexes in the array, determined on the basis of hash value for each key. (We will see later, exactly how the hash value is calculated) At each index of the array, is stored a linked list which has nodes of the type 'Map.Entry'. Each node stores the key/value pair.


Why is the linked list needed? Because two unequal objects can have same hash value (read more about equals and hashcode here). Remember that a HashMap will store values as long as different keys are used so …

Java Design Patterns : Adapter Pattern

Whenever and wherever you will read about Adapter pattern you will come across example of electric adapters. Be it a 3-pin to 2-pin adapter, round pin to flat pin adapter or AC to DC adapter, its purpose is to act as an intermediary between electrical equipment and the socket.


Above is a representation of a 3-pin to 2-pin adapter which exposes a 3-pin socket which takes in a 3 pin plug and gives a 2 pin output. (These 2-pins can be plugged into any 2-pin socket.) 
Note that the adapter contains a 3-pin plug inside and behavior of its 3 pins is altered to suit the behavior of the adapter pins which will fit in the 2-pin socket.
Now if we interpolate this in terms of Java concepts, the 3-pin socket is an interface and every 3-pin plug implements this interface. Let's call this as 'adaptee' interface. Similarly every 2-pin socket is also an interface which is implemented by every 2-pin plug. Let's call the 2-pin socket (which our adapter is trying to be compatible with) …