Monday, August 20, 2012

Collection interview Questions in Java 2


ArrayList vs LinkedList

Adding new elements is pretty fast for either type of list. For the ArrayList, doing  random lookup using "get" is fast, but for LinkedList, it's slow. It's slow because there's no efficient way to index into the middle of a linked list. When removing elements, using ArrayList is slow. This is because all remaining elements in the underlying array of Object instances must be shifted down for each remove operation. But here LinkedList is fast, because deletion can be done simply by changing a couple of links. So an ArrayList works best for cases where you're doing random access on the list, and a LinkedList works better if you're doing a lot of editing in the middle of the list.

Array vs ArrayList vs LinkedList vs Vector in java

Array vs ArrayList:
ArrayList is much better than Array, when the size need to be increased dynamically. Efficiency is possible with arrays. ArrayList permits null elements.
ArrayList has group of objects. Array it treated as an object.

LinkedList vs Vector:
A vector is a growable array which can store many objects of different classes.
A linked list is a linear list where each item has a link to the next item in the list. It can be used to implement queue or stack operations.

ArrayList vs LinkedList
Array list is better than linked list for accessing elements.Linked list is better than array list to perform insertion and deletion operations at arbitrary locations.

Vector Vs ArrayList
Vector is synchronized whereas ArrayList is not. Even though Vector class is synchronized, still when you want programs to run in multithreading environment using ArrayList with Collections.synchronizedList() is recommended over Vector.
ArrayList has no default size while vector has a default size of 10.

What is an enumeration?
An enumeration is an interface containing methods for accessing the underlying data structure from which the enumeration is obtained. It is a construct which collection classes return when you request a collection of all the objects stored in the collection. It allows sequential access to all the elements stored in the collection.

equals vs toString vs hashcod are methods in Object

Sort Diff b/w Arrays and Collections

Both use the same algorithm the only difference is type of input to them. Collections.sort() has a input as List so it does a translation of List to array. So this should be used when you are trying to sort a list. Arrays.sort is for arrays so the sorting is done directly on the array.

Collection Java Programming Interview Questions 1


Collection API ?
Java Collections framework API is a unified architecture for representing and manipulating collections. All collections frameworks contains interface, implementations and algorithms. Following are the benefits of collection framework.
Reduces programming efforts. - Increases program speed and quality.
Allows interoperability among unrelated APIs.
Reduces effort to learn and to use new APIs.
Reduces effort to design new APIs.
Encourages & Fosters software reuse.
Collection contains six interfaces.
Set, List and SortedSet: extends collection interface
Map and SortedMap : don’t extend collection interface.a

What is HashMap and Map?
Maps stores key Value pair, and Hashmap is class that implements that using hashing technique.

Diff  HashTable Vs HashMap Vs HashSet

Hashtable
Hashtable is basically a datastructure to retain values of key-value pair. Doesnot allow null for key and values
It is synchronized. So it comes with its cost. Only one thread can access in one time
Synchronized means only one thread can modify a hash table at one point of time. Any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.

Hashtable<Integer,String> rank= new Hashtable<Integer,String>();

rank.put(1,"A");
rank.put(1,"B");
rank.put(1,"C");

rank.put(null,"E"); NullPointerException at runtime

System.out.println(rank.get(1));
System.out.println(rank.get(2));


HashMap
Like Hashtable it also accepts key value pair.Allows null values
It is unsynchronized. So come up with better performance
By using following command Hashmap can be synchronized.
Map m = Collections.synchronizedMap(hashMap);

HashMap<Integer,String> Lang= new HashMap<Integer,String>();

Lang.put(1, "java");
Lang.put(2, null);

HashSet
HashSet does not allow duplicate values.
It can be used where you want to maintain a unique list. You also use its contain method to check whether the object is already available in HashSet.
It provides add method rather put method.

HashSet<String> str= new HashSet<String>();

str.add ("Apple");
str.add ("Boy");
str.add ("Cat");

if (str.contains("Apple"))

Iterator and implementation
An iterator is an object that enables a programmer to traverse a collection.
Iterator iter = list.iterator();
while (iter.hasNext()) {
    System.out.print(iter.next());
    if (iter.hasNext())
      System.out.print(", ");
}

Diff Iterator Vs ListIterator
Iterator : Enables you to traverse through a collection in the forward direction only, for obtaining or removing elements
ListIterator : extends Iterator, and allows bidirectional traversal of list and also allows the modification of elements.


Interface Implementation:


Implementations
InterfaceArrayBalanced TreeLinked ListHash table
ListArrayList LinkedList 
Map TreeMap HashMap
Set TreeSet HashSet
DequeArrayDeque LinkedList