Wednesday, 9 September 2015

Java- Differences between ArrayList and LinkedList

  


ArrayList
LinkedList
1) ArrayList internally uses dynamic array to store the elements.
 LinkedList internally uses doubly linked  list to store the elements.

2) Manipulation with ArrayList is slow because it internally uses array. If any element is removed from the array, all the bits are shifted in memory.
 Manipulation with LinkedList is faster than  ArrayList because it uses doubly linked list so  no bit shifting is required in memory.

3) ArrayList class can act as a list only because it implements List only.
 LinkedList class can act as a list and  queue both because it implements List and  Deque interfaces.

4) ArrayList is better for storing and accessingdata.
 LinkedList is better for manipulating data.

Java- Differences between Primary Key and Unique key


Primary key

Unique key
1)      Doesn't allow Null values.
Because of this we refer
PRIMARY KEY = UNIQUE KEY + Not Null CONSTRAINT
Allows Null value, but only one Null value.
2)      We can have only one Primary key in a table.

We can have more than one unique key in a table.

Java- Difference between StringBuffer and StringBuilder


StringBuffer
StringBuilder
1)      StringBuffer is synchronized i.e. thread safe. It means two threads can't call the methods of StringBuffer simultaneously.
StringBuilder is non-synchronized i.e. not thread safe. It means two threads can call the methods of StringBuilder simultaneously.

2)      StringBuffer is less efficient than StringBuilder.

StringBuilder is more efficient than StringBuffer.
3)      StringBuffer is slow
StringBuilder is fast



Java- differences between ArrayList and Vector




ArrayList
Vector
1) ArrayList is not synchronized.
Vector is synchronized.

2) ArrayListincrements 50% of current array size if number of element exceeds from its capacity.
Vector increments 100% means doubles the array size if total number of element exceeds than its capacity.


4) ArrayList is fast because it is non-synchronized.
Vector is slow because it is synchronized i.e. in multithreading environment, it will hold the other threads in runnable or non-runnable state until current thread releases the lock of object.

5) ArrayList uses Iterator interface to traverse the elements.
Vector uses Enumeration interface to traverse the elements. But it can use Iterator also.

Java- Differences between Array and ArrayList

Array
ArrayList
1) Stores primitive data types and also objects
Stores only objects . when we store primitive value , then with autoboxing features it automatically convert the value into object
2) Fixed size
Growable and resizable.
3) Stores similar data of one type  i.e , homogeneous
Can store heterogeneous data types
     4) Array is a fixed length data structure
ArrayList is a variable length Collection class

5) Elements accessible with index number
Accessing methods like get() etc.
6) can be Multidimensional
Single dimensional only .



Friday, 4 September 2015

Stored Procedure and their differences

Stored Procedure is a group of sql statements that has been created once and stored in database. It’s pre-compile objects which are compiled for first time and its compiled format is saved which executes (compiled code) whenever it is called. Stored procedures will accept input parameters so that single stored procedure can be used over network by multiple clients using different input data. Stored procedures will reduce network traffic and increase the performance.

Function is not pre-compiled object it will compile & execute every time whenever it was called.

Differences –

1)      Procedure can return zero or n values whereas function can return one value which is mandatory

2)      Procedures can have input, output parameters for it whereas functions can have only input parameters.

3)      Procedure allows select as well as DML(INSERT/UPDATE/DELETE) statements in it whereas function allows only select statement in it.

4)      Functions can be called from procedure whereas procedures cannot be called from function.

5)      Exception can be handled by try-catch block in a procedure whereas try-catch block cannot be used in a function .

6)      We can go for transaction management in procedure whereas we can't go in function.

7)      We can’t join Stored Procedure,But We can join functions.

Q) Why we can't execute stored procedure inside a function?
Answer:
1. Stored Procedure may contain DML statements.
2. Function can't contain DML statements.
            So executing Function inside stored procedure will never break rule 1.
    But executing stored procedure inside function may break rule no 2.

Java -Differences between HashSet and HashMap



HashSet
HashMap
1.          HashSet class implements  the Set interface
 HashMap class implements the Map interface
2.          In HashSet we store objects(elements or values) e.g. If we have a HashSet of string elements then it could depict a set of HashSet elements: {“Hello”, “Hi”, “Bye”, “Run”}
 HashMap is used for storing key & value pairs. In short it  maintains the mapping of key & value (The HashMap class is  roughly equivalent to Hashtable, except that it is unsynchronized  and permits nulls.) This is how you could represent HashMap  elements if it has integer key and value of String type: e.g. {1-  >”Hello”, 2->”Hi”, 3->”Bye”, 4->”Run”}
3.          HashSet does not allow duplicate elements that means you can not store duplicate values in HashSet.
 HashMap does not allow duplicate keys however it allows to have  duplicate values.
4.          HashSet permits to have a single null value.
 HashMap permits single null key and any number of null values.

Similarities:

1) Both HashMap and HashSet are not synchronized which means they are not suitable for thread-safe operations unitl unless synchronized explicitly. This is how you can synchronize them explicitly:
HashSet:
Set s = Collections.synchronizedSet(new HashSet(...));
HashMap:
 Map m = Collections.synchronizedMap(new HashMap(...));
3)      Both of these classes do not guarantee that the order of their elements will remain constant over time.