Q) What is difference between ArrayList and vector?

1) Synchronization – ArrayList is not thread-safe whereas Vector is thread-safe. In Vector class each method like add(), get(int i) is surrounded with a synchronized block and thus making Vector class thread-safe.

2) Data growth – Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.

Q)What is difference between HashMap and HashTable?

Both collections implements Map. Both collections store value as key-value pairs. The key differences between the two are

1. Access to the Hashtable is synchronized on the table while access to the HashMap isn’t. You can add it, but it isn’t there by default.

2. Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn’t. If you change the map while iterating, you’ll know. • Fail-safe – “if the Hashtable is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove method, the iterator will throw a ConcurrentModificationException”

3. HashMap permits null values and only one null key, while Hashtable doesn’t allow key or value as null.

Introduction to Spring Part II – Bean, BeanFactory, ApplicationContext, IOC

In this technoticle I will be explaining some of the basic concepts of Spring :

Bean – It is the basic element of the framework. It is equivalent to an object created in java. In spring bean can be created using following methods:

1)      Setter injection

2)      Constructor injection

3)      Interface injection

4)      Static factory method

5)      Instance method

The detailed explanation is Dependency Injection

BeanFactory – As the name suggest the beanfactory instantiate, configure and manages all beans defined. These beans typically collaborate with one another, and thus have dependencies between themselves. The ways by which beanFactory can be created: 

  • InputStream is = new FileInputStream(“beans.xml
    XmlBeanFactory factory = new XmlBeanFactory(is);

  • ClassPathResource res = new ClassPathResource(“beans.xml”); 
    XmlBeanFactory factory = new XmlBeanFactory(res);

  • ClassPathXmlApplicationContext appContext =  new ClassPathXmlApplicationContext (new String[] { “applicationContext.xml”,”applicationContext-part2.xml”}
    BeanFactory factory = (BeanFactory) appContext;

When any of the above lines are executed all beans present in the “configuration” file(s) are instantiated and stored in the bean factory.Once the bean factory is loaded the bean can be retrieved:


   BeanClassName beanObj = (BeanClassName) factory.getBean(“beanName”);


ApplicationContext: ApplicationContext extends BeanFactory and thus provide extra functionality than BeanFactory.

The following are the additional functionalities:

·         MessageSource, providing access to messages in, i18n-style

·         Access to resources, such as URLs and files

·         Event propagation to beans implementing the ApplicationListener interface

·         Loading of multiple (hierarchical) contexts, allowing each to be focused on one particular layer, for example the web layer of an application

Invresion of Control – Earlier the control of interface was with the application program. For example it a typical user input application a prompt will pop up saying ‘Enter Name’ or at some place the property has to be set explicitly using the setter methods. But with the advent of IOC, now it’s the responsibility of the container in which beans are loaded to find the correct value of a property with the help of spring configuration file and now not much responsibility is left to the application program to set or get the values.IOC is too generic term, so spring generally designates it as Dependency Injection (DI). 

 A typical IOC container would look like:

IOC Container
IOC Container


  • An IOC can have multiple ApplicationContexts.
  • ApplicationContext can also refer other application context.
  • Applicationcontext can contain another applicationcontext.
  • Multiple beanfactories can be inserted in an applicationcontext.

What is volatile keyword?

Ans) In general each thread has its own copy of variable, such that one thread is not concerned with the value of same variable in the other thread. But sometime this may not be the case. Consider a scenario in which the count variable is holding the number of times a method is called for a given class irrespective of any thread calling, in this case irrespective of thread access the count has to be increased. In this case the count variable is declared as volatile. The copy of volatile variable is stored in the main memory, so every time a thread access the variable even for reading purpose the local copy is updated each time from the main memory. The volatile variable also have performance issues.

Program to find height of a binary tree

The algorithm works as follows:

  • Each node starting from the parent node is push into an stack. Then the element is popped from the stack and a check is made to find whether the node has left and right child. If any one or both are present the elements are pushed into stack. At the same time a corresponding entry is made into a map with key as ‘node’ and value as the ‘height’ .The value of height is one more than the height of parent and intial height of parent is set to 1.

The code flow is explained below.

  • The program contains two classes : Node and BinaryTree class. The Node class is a datastructure used to build a tree and BinaryTree is a class to find the height.
  • The class Node is a data structure which can have left child (Node), a right child (Node) and can contain any value.

package com.src;

public class Node {


private Node left;

private Node right;

private Object value;


public Node getLeft() {

return left;



public Node getRight() {

return right;



public Object getValue() {

return value;



public Node(Node left, Node right, Object value) {


this.left = left;

this.right = right;

this.value = value;




The BinaryTree class contains the logic of finding the height of the tree.

Consider the following example:


  • The createTree() method is used to create the node for each element in tree. Create node starting from the leaf node and moving upward. For above tree:

Node node21 = new Node(null,null,21);

Node node1 = new Node(null,node21,1);

  • For the given example the createTree() method will have code as:

private void createTree(){

Node node21 = new Node(null,null,21);


Node node23 = new Node(null,null,23);

Node node56 = new Node(null,null,56);

Node node11 = new Node(null,null,11);

Node node1 = new Node(null,node21,1);

Node node45 = new Node(node23,node1,45);

Node node73 = new Node(null,null,73);

Node node12 = new Node(node56,node11,12);


Node node34 = new Node(node45,node73,34);

Node node87 = new Node(node12,null,87);


Node node77 = new Node(node34,node87,77);

parent = node77;



The logic for finding height is put in getHeight() method:

private Node parent = null;

     private Stack<Node> stack = new Stack<Node>();

// the map will store the node and its current height

     private Map<Node,Integer> map = new



private void getHeight(){


int highest_height =1;

int current_height = 1;

int parent_height=0;

Node temp_node = null;

stack.push(parent); // initially only the parent is present in stack

map.put(parent, current_height);

while(stack.size() != 0){

 temp_node = stack.pop();  // pop element at top from stack.

parent_height = map.get(temp_node);  // get the height

if(temp_node.getLeft() != null){  // if current node has left child

 current_height = parent_height+1; // increase the height by 1

stack.push(temp_node.getLeft()); // push the element to the stack

//put into map – put(current node, current height)


 highest_height= (highest_height < current_height) ? current_height : highest_height;



if(temp_node.getRight() != null){  // same as left child

current_height =parent_height+1;



 highest_height= (highest_height < current_height) ? current_height : highest_height;




System.out.println(“\n Elements currently in stack \n”);


for(int i =0; i<stack.size();i++){

System.out.print(” “ +((Node)stack.get(i)).getValue());  





System.out.println(“The height is “ + highest_height);



public static void main(String[] args) {

           BinaryTree bt = new BinaryTree();








The logs will show:


Elements currently in stack 34 87

Elements currently in stack 34 12

Elements currently in stack 34 56 11

Elements currently in stack 34 56

Elements currently in stack 34

Elements currently in stack 45 73

Elements currently in stack 45

Elements currently in stack 23 1

Elements currently in stack 23 21

Elements currently in stack 23

Elements currently in stack

The height is 5




  • When the parent is pop from the stack a check is made to find whether it has a left child or not, if a left child is present push the left child to stack, also put the same element in a map with key as the current node and value is height which is incremented by 1 of its parent height.
  • Now check if the current height is greater than the highest height or not. If it is set the highest_height to current_height.
  • Similarly it works for the right child.

For given example when a check is made for parent, then 34 and 87 are push into stack.


Elements currently in stack 34 87 

  • Now 87 is picked up and a check is made for it. Since it contains only left child as 12 , the current stack will look like

 Elements currently in stack 34 12

  • The while loop iterates till the stack becomes empty.
  • The main overhead in this program is to create a tree like structure by declaring the nodes explicitly.