Wednesday, November 2, 2011

Collection Interview Question Answer


 Explain Java Collections Framework?
     Java Collections Framework provides a well designed set of interfaces and classes that support operations on collections of objects.
Three Interface: Set, List, Map
Implementation: HasSet, TreeSet

Difference between Collection and Collections?
Collections is a Framework set of interfaces and classes interface like set, list, map while Collection is a class

Explain Iterator Interface.
The Iterator interface is used to step through the elements of a Collection.

     An Iterator is similar to the Enumeration interface. With the Iterator interface methods, you can traverse a collection from start to end and safely remove elements from the underlying Collection. The iterator() method generally used in query operations.
Basic methods:
  • iterator.remove();
  • iterator.hasNext();
  • iterator.next();
Example
Iterator ite = ListArray.Iterator();
While(ite.hasNext())
{
SOP(ite.next());
}
·         ListIterator
            Is like a  Iterator. ListIterator is bi-directional.ListIterator only update
·         enumerator
            Enumerator is same as Iterator. enumerator is read only interface.

Iterator
List Iterator
Enumerator
Forward only
Bi-directionally
Forward only
Only add & delete
Only update
Read Only
Has remove() method
Doest not have remove() method
Doest not have remove() method


SET Interface
The Set interface provides methods for accessing the elements of a finite mathematical set
Set Interface
  • Extends the Collection interface.
  • A set is just a group of unique items.
·        Sets do not allow duplicate elements
  • null is valid entry but allowed only once

The Set implementations available in the Collections Framework
·      HashSet ,TreeSet ,LinkedHashSet ,EnumSet


DIFFERENCE
HasSet
TreeSet
to eliminate the duplicate entries
eliminates duplicate entries
it  does not guarantee for either sorted order or sequence order.
it provides elements in a sorted  order (acceding order).
We can add any type of elements to hash set.
We can add only similar types
of elements to tree set.



LIST Interface
The List interface provides support for ordered collections of objects.

List is a ordered and non duplicated collection of objects. The List interface extends the Collection interface.
Two types of List implementations available in the Collections Framework
  • ArrayList, Vector and LinkedList

Difference
Array list
Link list
Array List are faster
Link list are slower then Array list
cannot insert and delete entries in middle of the list
allow elements to be added, removed from the collection at any location
you can access the elements randomly
you can only access the elements in sequentially



Difference
Vector
array list
synchronized
Not synchronized
Used Iterator and enumeration to access element
Use only Iterator to access element
Default size is 10
No default size
Increase the size by 50%
Increase the size by double







MAP Interface

·      A map is an object that stores associations between keys and values (key/value pairs).
·      Given a key, you can find its value. Both keys and values are objects.
·      The keys must be unique, but the values may be duplicated.
·      Some maps can accept a null key and null values, others cannot.
     A map is a special kind of set with no duplicates. The key values are used to lookup, or index the stored data. The Map interface is not an extension of Collection interface. It has its own hierarchy. Map does not allow duplicates in the collection. In Map implementations null is valid entry, but allowed only once.

·      HashMap ,HashTable ,TreeMap ,EnumMap

HasTable
HasMap
Hashtable does not store null value
HashMap store null value
Hashtable is synchronized
HashMap is not synchronized
Enumerator in HasTable is not fail-safe
Iterator in HasMap is fail-safe




Comparable interface

  • The Comparable interface is used to sort collections and arrays of objects using the Collections.sort() and java.utils.Arrays.sort() methods respectively.
  • The objects of the class implementing the Comparable interface can be ordered.

The Comparable interface in the generic form is written as follows:
        interface Comparable<T>
where T is the name of the type parameter.

All classes implementing the Comparable interface must implement the compareTo() method that has the return type as an integer. The signature of the compareTo() method is as follows:
      int i = object1.compareTo(object2)
·      If object1 < object2: The value of i returned will be negative.
·      If object1 > object2: The value of i returned will be positive.
·      If object1 = object2: The value of i returned will be zero.

What are the differences between the Comparable and Comparator interfaces ?
Comparable
Comparato
It uses the compareTo() method.
int objectOne.compareTo(objectTwo).
it uses the compare() method.

int compare(ObjOne, ObjTwo)
It is necessary to modify the class whose instance is going to be sorted.
A separate class can be created in order to sort the instances.
Only one sort sequence can be created.
Many sort sequences can be created.
It is frequently used by the API classes.
It used by third-party classes to sort instances.


How do I make an array larger?
     You cannot directly make an array larger. You must make a new (larger) array and copy the original elements into it, usually with System.arraycopy(). If you find yourself frequently doing this, the Vector class does this automatically for you, as long as your arrays are not of primitive data types.

Which is faster, synchronizing a HashMap or using a Hashtable for thread-safe access?
     Because a synchronized HashMap requires an extra method call, a Hashtable is faster for synchronized access.

What method in the System class allows you to copy elements from one array to another?
     System. arraycopy()

When using the System.arraycopy() method,What exception is thrown if the destination array is smaller than the souce array?
     ArrayIndexOutofBoundsException

What is the use of Locale class?
     The Locale class is used to tailor program output to the conventions of a particular geographic, political, or cultural region

No comments:

Post a Comment