Saturday, January 26, 2013

Interior Design of HashMap


HashMap works on principle of hashing.

We have put () and get () method for storing and retrieving data from hashMap.

When we pass an object to put () method to store it on hashMap, hashMap implementation calls hashcode() method hashMap key object and by applying that hashcode on its own hashing funtion it identifies a bucket location for storing value object.

HashMap stores both key+value in bucket.

Map internally used two data structures:
  1. Array
  2. LinkedList



  • Each index of array is a bucket
  • To identify the bucket for any <key,value>, Hash map use key.hashCode() and perform some operation:
  • It means, two keys with different hashCode can fall under same bucket.
  • If a bucket is empty then the Entry object is simply inserted at ith position
  • The latest entry resides on the top of the bucket.
  • If a key already exists in a map, then it just replace the value and return the old value
  • In case of new key,  map add the <key, value> (a new Entry object with key value) and return null
  • Entry.hash is not the hashCode of key, but HashMap use its own hash algorithm to create hash based on key.hashCode().


Collision  : If different objects have same hashcode ?

Since hashcode () is same, bucket location would be same and collision occurs in hashMap, 

Since HashMap use a linked list to store in bucket, value object will be stored in next node of linked list.

How to retrieve if two different objects have same hashcode?

We will call get() method and then HashMap uses keys hashcode to find out bucket location.

Since two objects are stored in same bucket, it will traverse through the linked list until find the value object.

After finding bucket location, we will call keys.equals() method to identify correct node in linked list and return associated value object for that key in Java Hash Map





ArrayList vs LinkedList


  • Both ArrayList and Vector use array to store the elements
  • ArrayList implements the RandomAccess interface, and LinkedList does not. The commonly used ArrayList implementation uses array for internal storage. Therefore an ArrayList is much faster than a LinkedList for random access, that is, when accessing arbitrary list elements using the get method.
  • Adding and deleting at the start and middle of the ArrayList is slow, because all the later elements have to be copied forward or backward using System.arrayCopy(). But Linked lists are faster for inserts and deletes anywhere in the list, since all you do is update a few next and previous pointers of a node.
  • When an element is inserted into the middle of the list the elements that follow the insertion point must be shifted to make room for the new element. The LinkedList is implemented using a doubly linked list, an insertion requires only the updating of the links at the point of insertion. So, the LinkedList allows for fast insertions and deletions.
    • Ie:- LinkedList is made up of a chain of nodes. Each node stores an element and the pointer to the next node. A singly linked list only has pointers to next. A doubly linked list has a pointer to the next and the previous element. This makes walking the list backward easier
  • Each element of a linked list (especially a doubly linked list) uses a bit more memory than its equivalent in array list, due to the need for next and previous pointers.

Friday, January 25, 2013

Why Map interface not extend Collection?


Answer from Sun's FAQ Page:-

This was by design. 

We feel that mappings are not collections and collections are not mappings. Thus, it makes little sense for Map to extend the Collection interface (or vice versa).

If a Map is a Collection, what are the elements?
The only reasonable answer is "Key-value pairs", but this provides a very limited (and not particularly useful) Map abstraction. You can't ask what value a given key maps to, nor can you delete the entry for a given key without knowing what value it maps to. Collection could be made to extend Map,

But this raises the question: what are the keys?
There's no really satisfactory answer, and forcing one leads to an unnatural interface. Maps can be viewed as Collections (of keys, values, or pairs), and this fact is reflected in the three "Collection view operations" on Maps (keySet, entrySet, and values). While it is, in principle, possible to view a List as a Map mapping indices to elements, this has the nasty property that deleting an element from the List changes the Key associated with every element before the deleted element.

So, That's why we don't have a map view operation on Lists.

Hashcode and equals



Java.lang.Object has two methods:-
-          Public Boolean equals(Object o)
-          Public int hashCode()

  • These are heavily used with Collections.
  • Whenever it is invoked on the same object more than once during an execution, the hashCode() method must consistently return the same integer. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals() method, then calling the hashCode() method on each of the two objects should return the same integer.
  • Also It is not mandatory that if two objects are not equal according to the equals(java.lang.Object) method, then calling the hashCode() method on each of the two objects return distinct integer results.
  • It is necessary to override the hashCode() method whenever equals() method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
  • Sets use equals() to enforce non-duplicates, and HashSet uses hashCode() as a first-cut test for equality. Technically hashCode() isn't necessary then since equals() will always be used in the end, but providing a meaningful hashCode() will improve performance for very large sets or objects that take a long time to compare using equals().



HashTable V/s HashMap


HashTable -> synchronized – is a legacy class , Enumeration in the Hash table is not fail-fast
-            
HashMap -> not synchronized, Itrator in the hashmap is fail-fast
                        We can make the hashmap synchronized when ever required using Collections.SynchronizedMap(map)
                        Does not garentee the order of the keyvalue pair in the entered order, but the subclass of hashmap, which is LinkedHashMap we can achieve this.

Synchronized means only one thread can modify a hash table at one point of time. Basically, it means that any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.

Fail-fast
A fail-fast system is designed to immediately report any failure or condition that is likely to lead to failure. Fail-fast systems are usually designed to stop normal operation rather than attempt to continue a possibly-flawed process. When a problem occurs, a fail-fast system fails immediately and visibly. It will sounds like making your software more fragile, but it actually makes it more robust. Bugs are easier to find and fix, so fewer go into production

When to use what?
Whenever there is a possibility of multiple threads accessing the same instance, we should use Hashtable. While if not multiple threads are going to access the same instance then use HashMap. Non synchronized data structure will give better performance than the synchronized one.
Again , may be later point of time - there can be a scenario when you may require to retain the order of objects in the Collection with key-value pair then HashMap can be a good choice. As one of HashMap's subclasses is LinkedHashMap, so in the event that you'd want predictable iteration order (which is insertion order by default), you can easily swap out the HashMap for a LinkedHashMap. This wouldn't be as easy if you were using Hashtable.
Also if you have multiple thread accessing you HashMap then Collections.synchronizedMap() method can be leveraged.

Verdict:-Overall HashMap is better in all aspects.

HashMap Sample Program:-

class HashMap
{
public static void main(String args[]) {
// Create a hash map
HashMap hm = new HashMap();

// Put elements to the map
hm.put("Sujith", new Double(71.5));
hm.put("Manjusha", new Double(53.6));
hm.put("Sukhesh", new Double(78.5));
hm.put("Ranjith", new Double(81.1));

// Get a set of the entries
Set set = hm.entrySet();

// Get an iterator
Iterator i = set.iterator();

// Display elements
while(i.hasNext())
{
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
}
}


mysqlbinlog : Enable binary logs in mysql and extract from bin file

MySQL Server generate binary log files for every db transaction, provided administrator does not disable. The binary log files are written in binary format. It will be stored in /var/lib/mysql directory. It cannot be read directly, as it is in binary format. So we need to use mysqlbinlog command to read in text file.

Check enabled?


SELECT * from information_schema.GLOBAL_VARIABLES WHERE VARIABLE_NAME = 'LOG_BIN';
 Or
SELECT @@log_bin;
Or
SHOW VARIABLES LIKE 'log_bin';
If not enabled:-enable binary logs in mysql

Add this to /etc/my.cnf:

log-bin=mysql-bin

[root@rhel6 ~]# /etc/init.d/mysqld restart --log-bin
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[root@rhel6 ~]# mysql -u root -p
Enter password:

mysql> show binary logs;
ERROR 1381 (HY000): You are not using binary logging
[root@rhel6 ~]# updatedb
[root@rhel6 ~]# locate mysql-bin

Mysql - To extract from bin file:

mysqlbinlog  is a tool to analyze and view the binlogs  from mysql, which are stored in binary format. This will converts them to plaintext, so that  it can be readable.

--start-datetime and --stop-datetime , both will accept DATETIME or TIMESTAMP entries, and which together set the start/stop of what kind of information we’re interested in.
Ex:-
mysqlbinlog --start-datetime="2012-09-05 00:00:00" --stop-datetime="2012-12-31 14:00:00" mysql-bin.000019 --result-file=05Sept_31Dec.txt

mysqlbinlog --start-datetime="2012-12-31 01:00:00" mysql-bin.000019 --result-file=31dec.txt

Monday, January 14, 2013

REST and SOAP Web Services


REST Web Service

  • REST => Representational State Transfer,  means  each unique URL is a representing some object. You can get the contents of that object using an HTTP GET, also you can use a POST, PUT, or DELETE to modify the object.
  • It is Light weight ,  not a lot of extra xml markup, Human Readable Results,  Easy to build
  • REST has no WSDL interface definition
  • REST is over HTTP, REST is stateless
  • REST can be  plain text, JSON, HTML etc which can transfer over HTTP
  • REST services are easily cacheable.
  • In REST, For transport security we can use https and for authentication, basic auth
  • REST is much simpler and easy to interop in many languages.
  • REST is a better choice for integration between websites, with public API, on the TOP of layer (VIEW, ie, javascripts taking calls to URIs).

SOAP Web Service:

  • SOAP is a protocol.
  • SOAP was designed to give access to Objects as in Object Oriented Programming Objects; i.e., data plus methods
  • SOAP can be over any transport protocols such HTTP, FTP, STMP, JMS
  • SOAP is using soap envelope
  • From any given WSDL, we can generate
  • The problem with SOAP is its complexity when the other WS-* specifications come in and there are countless interop issues if you stray into the wrong parts of WSDL, XSDs, SOAP, WS-Addressing etc.
  • SOAP is a XML-based protocol that tunnels inside an HTTP request/response, so even if you use SOAP, you are using REST too
  • SOAP is a better choice for integration between legacy/critical systems and a web/web-service system, on the foundation layer, where WS-* make sense (security, policy, etc.).