10 | 03 | 2010
Main Menu
Affiliates
Login Form



Alexa
JoomlaWatch Stats 1.2.6 by Matej Koval
Java Collections Interview Questions
Written by Java King   
Sunday, 18 May 2008 01:44
Article Index
Java Collections Interview Questions
Sets
Maps
All Pages

Rating 3.1/5 (12 votes)

 

Q: Can u explain the collections hierarchy?

A:

Collection Interfaces Hiererchy

 

 

 

 

 

 

 

 

 

 

 

Interface

Implementation

Historical

Set

HashSet


TreeSet



List


ArrayList


LinkedList

Vector
Stack

Map

HashMap


TreeMap


Hashtable
Properties

 

 

Q: What is the List Interface?

List represents an ordered collection of objects, In a List user can specify the position into which an object must be inserted, and Users can access elements by their index and can also search for objects in the List.

 

Q: What are the different implementations of List interface?

A: ArrayList, LinkedList and Vector.

 

Q: What is the difference between ArrayList and Vector?

A: The underlying implementation of ArrayList and vector is same, the only difference is that all the methods in Vector is synchronized and hence vector is thread Safe. Moreover, In an Vector we can specify the amount by which the size of an Vector should increase when it becomes full.

 

Q: How do you decide when to use ArrayList and When to use LinkedList?

A: In an ArrayList direct access to any element based on its index is very fast ,where as in linked list the access to elements by index is comparatively slow. In array list Insertion and deletion are very slow where as in LinkedList it is very fast. So I would use ArrayList where I need fast retrieval by index and I would use LinkedList when I need to do many insertion and deletion.

 

Q: Why is insersion and deletion in ArrayList slow compared to LinkedList?

A: ArrayList internally uses and array to store the elements, when that array gets filled by inserting elements a new array of roughly 1.5 times the size of the original array is created and all the data of old array is copied to new array. During deletion, all elements present in the array after the deleted elements have to be moved one step back to fill the space created by deletion. In linked list data is stored in nodes that have reference to the previous node and the next node so adding element is simple as creating the node an updating the next pointer on the last node and the previous pointer on the new node. Deletion in linked list is fast because it involves only updating the next pointer in the node before the deleted node and updating the previous pointer in the node after the deleted node.

 

Q: What is an Iterator?

A: Iterator is an Interface whose implementations are returned by the iterator() method in the collection classes, Iterators are used for iterating through the elements in a collection, It also allows us to remove elements during the iteration.

 

Q: How do you traverse through a collection using its Iterator?

A: Iterators have methods hasNext() and next(). Using hasNext() we can find whether the iterator has more elements and by calling next() we can get the next element.

 

Q: How do you remove elements during Iteration?

A: Iterator also has a method remove() when remove is called, the current element in the iteration is deleted.

 

Q: Why are Iterators returned by ArrayList called Fail Fast?

A: Because, if list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

 



Comments
Search
Pannaga   |203.200.31.xxx |2009-06-25 04:21:48
Only registered users can write comments!

3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Sunday, 12 April 2009 09:10 )