Monday, August 20, 2012

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 


No comments:

Post a Comment

Thanks for your valuable comments