Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, June 25, 2015

Arraylist v/s Vector

ArrayList v/s Vector


Expect this question in a junior level (0-2 yrs) interview. One of the most done to death and still kicking question. This questions lays the foundation of advanced level question.

Let's see the differences one by one.

S No.
Arraylist
Vector
1
Non Synchronized and hence not thread safe
Synchronized and Thread safe
2
Faster performance as non-synchronized
Slower performance as only one thread access at a time.
3
Array increase the size by 50% when full (Starting Size 10 by default)
Vector doubles the size when full.
4
Uses Iterator for iteration
Uses Enumerator for iteration

Wednesday, June 24, 2015

HashMap v/s ConcurrentHashMap

Concurrence


Going a step further interviewer now delves into the multi-threading regime. One of the first questions that is asked at mid-level (2-4 yrs) is difference between HashMap and ConcurrentHashMap. 

This is check of knowledge of basic usage of the Candidate, since if you have worked on any Java application, you would have come across HashMap/ConcurrentHashMap for sure.


S No.
HashMap
ConcurrentHashMap
1
Not Thread safe
Thread safe.
2
synchronized(map) makes HashMap synchronized
Already thread safe
3
synchronized(map) locks entire map. Reduces Performance
Takes lock only on a portion of the Map and not the whole Object. Faster Performance
4
One null key is allowed
Null key is not allowed
5
Better performance in Single threaded Environment
Not Got performance in Single threaded Environment

Monday, June 22, 2015

Iterator v/s Enumator

Iterator v/s Enumerator

Often asked during interviews from Collections. They are both used to iterate over a Collection of Data. Below is the difference in tabular format and sufficient for interviews.


Differences
S No.
Iterator
Enumerator
1
Methods
hasNext()
next()
remove()
Methods
hasMoreElement()
nextElement()
2
Secure as throws ConcurrentModificationException if other thread tries to modify the Collection object while one thread is iterating over it
Not secure as no concept of ConcurrentModificationException
3
remove() method makes it read/modify interface
remove() method is absent, hence it is Read-only interface




Similarities
S No.
Iterator
Enumerator
1
Interface
Interface
2
java.util package
java.util package
3
Used for iteration
Used for iteration

Sunday, June 21, 2015

Java HashMap v/s HashTable

Map v/s Table
HashMap always brings innumerable interview questions along with itself. One of the common interview questions is the difference and similarities between Java HashMap and HashTable. You might not get the direct question like this but something on similar lines. 

We have tried to list the difference and similarity. Have a go!

Differences
S No.
HashMap
HashTable
1
Not synchronized. Not thread safe
Synchronized. Thread safe
2
Allows one Null Key and multiple null values
Null keys and Null values not allowed
3
Iterated by using Iterator
Iterated by using Enumerator
4
Iterator in HashMap is fail-fast
Enumerator in HashTable is not fail fast
5
Faster and uses less memory as unsynchronized
Slower and uses more memory as synchronized


Similarities
S No.
HashMap
HashTable
1
Does not guarantee order of Map
Does not guarantee order of Map
2
Constant Time for put/get
Constant Time for put/get
3
Works on Hashing Principle
Works on Hashing Principle
4
Implements Map Interface
Implements Map Interface

Friday, June 19, 2015

Concurrency Utilities Fork/Join Framework

Forks Join!

With the advent of multiple processors, the code also need to evolve. We need to write code which can spawn Threads in such a manner so as to take complete use of multiple processors. The idea is to use all available processing power which leads to enhanced performance. No Thread should remain free.

Fork/Join framework is an implementation of ExecutorService. We have a ThreadPool. Tasks are allotted to the worker threads. The difference here is if some thread is free, it will steal the work from a busy thread, thereby maximizing performance.
ForkJoinPool is the implementation of ExecutorService which can run ForkJoinTasks. It is different from other ExecutorServices as the threads in the pool tries to steal the subtasks created by other Tasks (being processed by other threads). It’s like a workaholic employee who ends up doing other people’s work also. In effect it improves firms overall performance.
1
2
ForkJoinPool pool = new ForkJoinPool();
pool.invoke(new SumArray(array, 0, array.length));
SumArray extends ForkJoinTask (RecursiveTask/RecursiveAction). See here the analogy with a simple ThreadPoolExecutor, which requires a Runnable/Callable task.
The SumArray needs to have function compute() (analogous to run()) which will be called post pool.invoke() (analogous to execute()). Also a function invokeAll() is available to the SumArray, which is just like invoke, but takes multiple ForkJoinTasks and assign them to the ForkJoinPool.
1
2
3
4
5
6
7
8
9
10
11
protected void compute() {
        if(length < 1000 ) { //No need to Split the Task
            sumDirectly();
            return;
        }
        //Need to split the Task
        invokeAll(new SumArray(array, 0, length/2),
                new SumArray(array, length/2, length));
}