Java Collections Interview Questions

dibuxazu's version from 2016-10-02 21:11

Section 1

Question Answer
Java Collections FrameworkProvides a well designed set of interfaces + classes. Support operations on a collections of objects\
Iterator InterfaceSimilar 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();;
Enumeration InterfaceAllows 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 differenceEnumeration 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.
Set InterfaceA set is just a group of unique items, no duplicates. Set interface extends the Collection interface. null is valid entry, but allowed only once.

Section 2

Question Answer
Two types of Set implementationsHashSet and TreeSet.
Difference between HashSet and TreeSetHashSet 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.
HashingMapping between a key value and a data item. Provides efficient searching.
List interfaceOrdered collection of objects, supporting duplicate entries. Extends the Collection interface.
Two types of List implementationsArrayList and LinkedList.

Section 3

Question Answer
Difference between ArrayList and LinkedListArrayList 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 queueLinkedList.
Map InterfaceSpecial 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.
Two types of Map implementationsHashMap, TreeMap.

Section 4

Question Answer
Difference between HashMap and TreeMapHashMap 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 ClassOnce 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 classWrap 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 ArrayListRepresent a growable array. Vector is synchronized. ArrayList is not.

Section 5

Question Answer
Difference between Hashtable and HashMapBoth 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 largerMake 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?java.util.
Method in the System class to copy elements from one array to anotherSystem. arraycopy() .

Section 6

Question Answer
Use of Locale classUsed to tailor program output to the conventions of a particular geographic, political, or cultural region.
Use of GregorianCalendar classProvides support for traditional Western calendars .
Use of SimpleTimeZone classProvides support for a Gregorian calendar.
Use of ResourceBundle classUsed to store locale-specific resources. Can be loaded by a program to tailor the program's appearance to the particular locale in which it is being run.