Provides a well designed set of interfaces + classes. Support operations on a collections of objects\
Similar to Enumeration interface. Interface methods help traverse a collection from start to end. Helps safely remove elements from the underlying Collection. iterator() method used in query operations. Basic methods:iterator.remove();iterator.hasNext();iterator.next();
Allows you to iterate through all the elements of a collection. Similar to iterating through an Iterator. No removal support with Enumeration. Basic methods:boolean hasMoreElements();Object nextElement();
Enumeration and Iterator interface difference
Enumeration interface allows you to iterate through all the elements of a collection. Iterating through an Enumeration is similar to iterating through an Iterator. No removal support with Enumeration.
A set is just a group of unique items, no duplicates. Set interface extends the Collection interface. null is valid entry, but allowed only once.
HashSet Class implements java.util.Set interface. eliminate the duplicate entries + uses hashing for storage. TreeSet Class implements java.util.Set interface provides an ordered set, eliminates duplicate entries + uses tree for storage.
Mapping between a key value and a data item. Provides efficient searching.
Ordered collection of objects, supporting duplicate entries. Extends the Collection interface.
ArrayList Class implements java.util.List interface. Uses array for storage. Array storages are generally faster. Cannot insert and delete entries in middle of the list. Element access is random. LinkedList Class implements java.util.List interface. Uses linked list for storage. Allow elements to be added, removed at any location in the container by ordering the elements. Element access is done sequentially.
Collection used to implement a queue
Special kind of set with no duplicates. Key values used to lookup, or index the stored data. Not an extension of Collection interface. Does not allow duplicates. null is valid entry, but allowed only once.
HashMap Class implements java.util.Map interface. Uses hashing for storage. Map uses Set functionality, so no duplicates. TreeMap Class implements java.util.Map interface. Uses tree for storage. Provides the ordered map.
Functionality of Vector Class
Once array size is set you cannot change. Vector grows and shrink it's size automatically. Only objects allowed to be store, not primitives. To store primitives, convert primitives into objects using wrapper classes.
Vector vt = new Vector(3, 10);
vt is an instance of Vector class. Initial capacity of 3. Grows in increment of 10 in each relocation.
How to store a primitive data type in a Vector or other collections class
Wrap the primitive data type into the wrapper classes in java.lang package. E.g. Integer, Float, or Double. Integer in = new Integer(5); .
Difference between Vector and ArrayList
Represent a growable array. Vector is synchronized. ArrayList is not.
Both provide key-value access to data. The key differences are :a. Hashtable is synchronized but HasMap is not synchronised.b. HashMap permits null values but Hashtable doent allow null values.c. iterator in the HashMap is fail-safe while the enumerator for the Hashtable is not fail safe.
How to make an array larger
Make a new (larger) array and copy the original elements into it. Use System.arraycopy(). If done frequently, the Vector class does this automatically for you. Make sure arrays are not of primitive data types.
Which is faster, synchronizing a HashMap or using a Hashtable for thread-safe access?
Synchronized HashMap requires an extra method call, a Hashtable is faster for synchronized access.
In which package are the interfaces and classes defined in the Java Collection Framework?
Method in the System class to copy elements from one array to another