Collection classes

1)Can we sort an Hashtable?

Yes. Here is an example http://www.javagalaxy.com:8080/CoreJava/View.jsp?slno=90&tbl=0

2)What is the difference between ArrayList and Vector

Methods in Vectors are synchronized

3)How to get values from a Vector

Vector v = new Vector(10,2) v.add(3); v.add(4); int i= v.get(1);

4)What is the Collections API?

The Collections API is a set of classes and interfaces that support operations on

collections of objects

5)What is the List interface?

The List interface provides support for ordered collections of objects.

6)What is the Vector class?

The Vector class provides the capability to implement a growable array of objects

7)What is an Iterator interface?

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

8)What is the difference between ArrayList and Vector?

a.Internally, both the ArrayList and Vector hold onto their contents using an Array.

A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.

b.ArrayList doesn't have a constructor for specifying the incremental

capacity, where as Vector has a constructor to specify the initial capacity and incremental capacity.

c.Vector is synchronized where as ArrayList is not synchronized

9)What is the difference between Iterator and Enumeration?

Iterator takes the place of Enumeration in the Java collections framework. Iterators differ from enumerations in two ways: Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. Method names have been improved.

10) What is a cloneable interface and how many methods does it contain?

It is not having any method because it is a TAGGED or MARKER interface.

11) What is the difference between Array and vector?

Array is a set of related data type and static whereas vector is a growable array of objects and dynamic.

12) What are Vector, Hashtable, LinkedList and Enumeration?

Vector : The Vector class provides the capability to implement a growable array of objects.

Hashtable : The Hashtable class implements a Hashtable data structure. A Hashtable indexes and stores objects in a dictionary using hash codes as the object's keys. Hash codes are integer values that identify objects.

LinkedList: Removing or inserting elements in the middle of an array can be done using

linkedList. A LinkedList stores each object in a separate link whereas an array stores object references in consecutive locations.

Enumeration: An object that implements the Enumeration interface generates a series of

elements, one at a time. It has two methods, namely hasMoreElements( ) and nextElement( ). HasMoreElemnts( ) tests if this enumeration has more elements and nextElement method returns successive elements of the series.

13)What is the difference between set and list?

Set stores elements in an unordered way but does not contain duplicate elements, whereas list stores elements in an ordered way but may contain duplicate elements.

14)What is the Collection interface?

The Collection interface provides support for the implementation of a mathematical bag - an unordered collection of objects that may contain duplicates.

15) What is the Dictionary class?

The Dictionary class provides the capability to store key-value pairs.

16) Which java.util classes and interfaces support event handling?

The EventObject class and the EventListener interface support event processing.

17) What is the Map interface?

The Map interface replaces the JDK 1.1 Dictionary class and is used associate keys with values.

Source Ravi