Saturday, 25 August 2018

Queue implementation in java


Queue --> Dequeue --> LinkedList

Queue : Only add() from tail and remove() from head (FIFO obviously)







Dequeue: add() and remove() from both the sides methods like addFirst(10),addLast(10),removeFirst(),removeLast().


Notes on LinkedList

1. it maintained all node as 

Node<E> Object attributes{
E item;
Node next;
Node previous;
}

So if you want to implement you own queue/LinkedList,  you should have similars fields

2. remove and add, increase the size field of
Class LinkedList {

int size =0;
Node first/head;
Node last/tail;

}

add(item) {
added to tail, but if head is also null, then head and tail both should point to same.


Sunday, 19 August 2018

Why to override hasCode() and equals() together ?


Lets say your class:

Public Employee {

int empId;

Override only equals()

public boolean equals(Employee other) {

if(this.empId == other.empId) { return true}
else { return false;}

}

You will not face any issues, while using your class , till you start storing your class's object in to any hash implemented collection like HashMap,HashSet etc.


Lets say you want to store you Employee objects in to HashMap.

First lets understand, how HasMap stores the data in it.



Its a Linked List of Linked List
All indexes 0-15 are like linked buckets, then in side each bucket there is another linked list of Map.Entry, for all collision Object with same Bucket Id.


Quick logic of HashMap.put(Key,value)

1.  It first invokes the Key.HashCode() --> say 2001
2. Then tries to find the BucketId/IndexId where it has to place this object --> say divide by n  = 2
3. If next put also result in same bucketId, then that new node will be added to next node of that bucket, line above picture, key4 and key5


Coming back to question again.

Lets your class has only overridden the equals, then each time "new Employee(10)" will produce new Hashcode(Object native memory address , integer representation).

and when you do hashMap.get(new Employee(10), you will never find your object in the map, even though you had added it.

Hence you should provide you unique was of generating hashcode for your class,


Question -2

if you override only HashCode() but not equals

Then suppose your --> new Employee(10) and new Employee(11) goes in to same bucket, then, when you do hasMap.get(new Employee(10)), it will find the bucket, but in the bucket how would find exact object,

becaue inside the bucket each node's "searchedKey.equals(BucketNextkey)" will be invoked, and default implementation of Object's class equal is ==, it check if reference is same,

hence you will never find your Object inside map,even if you has put it.




Monday, 13 August 2018

Threads Creation and Synchronization

Brief

1. A thread can be created either by implementing "Runnable" interface or extending "Thread" class

Inside difference

Thread class internally Implements Runnable interface

and overrides "run" method.

when you call

Thread t = new thread(Runnnable r); --> sets global variable Runnable target = r;

t.start() --> this thread method in Thread class, internally calls local run() and that run method called
target.run().


So if runnable is passed as target, then the class which implements this Runnable.run() will be invoked.

If its thread class is extended , then this target is set as extending class's instance, hence it becames target in run() of thread class.


On Synchronization









Abstract class vs Interface : Use case scenarios


                Abstract class                                                                  Interface 


Most of the properties of these are same, except 

1. Its a class, use "Extends" to inherit it                                         1. Its an interface, use "implements" to inherit it

2. You can have some implement/unimplemented function          2. all functions are bydefault public abstract

3. You can have constructor                                                           3. No constructor allowed


4. Fields/attributes allowed                                                             4. Only constant allowed, Not fields/variable 



Use case scenarios 


                                                                     Interface

1.  To provide some special(HAS-A) capabilities to its all may  be unrelated children, like Employee can be "Serialization"  and Animal can be "Serialization" , after implementing "Serializable" interface both HAS-A property/capability of                 being serialized implementer, 
  
 2. as well as your base has no meaning on it's own
3. You want to define some sort contract/set some fixed behavior/capabilities for all your implementer , walk-able, runnable,flyable etc


                                                                     Abstract

1. IS-A type relation, Dog is Animal, Nano is a Car
2. When you want to reuse the existing code, make inherit properties of you parent and have some of your own.