Showing posts with label Interview questions on collections framework. Show all posts
Showing posts with label Interview questions on collections framework. Show all posts

what is the difference between ArrayList and DoubleLinkedList?


ArrayList
LinkedList
ArrayList is implemented as a resizable array
LinkedList is implemented as a Double LinkedList
As more elements added to ArrayList, its size is increased dylnamically
LinkedList performance on add & remove is better than ArrayList
ArrayList elements can be accessed directly by calling get() and set() methods
LinkedList worse on get() and set()methods

When to use LinkedLIst:
  1. Your application can live with out random access because if you need nth element in LinkedList, you need to traverse upto nth element and then you can get data from that node.
  2.  If your application have more insertion and deletion operations more than retrieval. 
  3.  LinkedList is much faster than ArrayList since removal does not involve resize.

Which is the best one from the following i.e List l =new ArrayList() and ArrayList al=new ArrayList()

The First one List l = new ArrayList() 
allows you to use the List1 to refer to any object that is a sub class of list 


The second one ArrayList al = new ArrayList() 
will allow you to refer to only object that is an ArrayList Object

Logically the second way should be faster. But, since the binding happens 
at runtime, it shouldnt matter really. Its actually more to do with 
polymorphism. When you know you are going to be referencing object that may 
be sub classes and want to reuse the object ref.

It's simple, List is a Interface and "Arraylist, LinkedList, Vector" are your implementation. Each one with its own benefit, when you use the List any moment you can change your implementation, your application becomes more flexible. 

Example: 
List list = new ArrayLista(); 
for(String s : list){ 
... 


list = new LinkedList(); 
for(String s : list){ 
... 

What is the difference between HashMap and HashTable?

Hash Map
Hash Table
Hash Map is not synchronized
Hash Table is synchronized
In case of single thread, Hash Map is faster than Hash Table
In case of multiple threads Hash Table is advisable, with single thread model it becomes slow
Hash Map allows null keys and null values
Hash table does not allow null keys or null values
Iterator in Hash Map is fail-fast means it will produce exception if concurrent updates made to Hash Map
Enumeration for Hash table is not fail-fast means even if concurrent updates are done to Hash Table, Enum will not produce any incorrect results