Tuesday, February 19, 2013

Aggregation


Aggregation means HAS –A relationship

Eg: Employee has Address
ie-
class Employee{
int id;
String name,salary;
Address address;//Address is a class
}
employee -> name, id, salary also has Address -> which itself has different attribute like pincode, city, country etc.
When there is no IS-A relationship(ie: inheritance), best way of code reuse is aggregation.
Inheritance should be used only if the –IS-A relationship is maintained in the life time of the object, otherwise aggregation is the best choice.

Android Version and Name

Version
Name
1.5
Cupcake
1.6
Donut
2.1
Éclair
2.2
Froyo
2.3
Gingerbread
3.1,3.2
Honeycomb
4.0
Ice cream sandwitch
4.1
Jelly Bean

Thursday, February 7, 2013

ERROR 2006 (HY000): MySQL server has gone away

Issue:


mysql -u root -proot123 mydb < /root/db-2013-01-30.sql
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...

Analysis:-

Actually dump file having large INSERT queries, and also “max_allowed_packet” size was set to very low value.

So, when the mysql client or the mysqld server receives a packet,which is bigger than max_allowed_packet bytes, it will throw error and close the connection.

Solution:-

In /etc/my.cnf , I updated the entry
max_allowed_packet=1M
to
max_allowed_packet=64M

now run the query for restore again:-
mysql -u root -proot123 mydb < /root/db-2013-01-30.sql

It worked!!

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().