Professional Documents
Culture Documents
Java Collections
Java Collections
Java Interview Questions site discussing core java IT technical interview questions in detail. These
are some of the java job interview questions me and my friends have faced regularly in campus
interviews and off campuses. I have consolidated all of them from different people and communities
into a vast resource of core java interview questions all in one place. So I am giving you a chance to
prepare well by going through each of the java interview faqs organized by java topics, before attending
a technical interview on java.
Or
Iterator : Enables you to cycle through a collection in the forward direction only, for obtaining or
removing elements
ListIterator : It extends Iterator, allow bidirectional traversal of list and the modification of elements
2)Fail-safe is relevant from the context of iterators. If an iterator has been created on a collection object
and some other thread tries to modify the collection object "structurally”, a concurrent modification
exception will be thrown. It is possible for other threads though to invoke "set" method since it doesn’t
modify the collection "structurally”. However, if prior to calling "set", the collection has been modified
structurally, "IllegalArgumentException" will be thrown.
Map m = Collections.synchronizeMap(hashMap);
What is the difference between set and list?
A Set stores elements in an unordered way and does not contain duplicate elements, whereas a list
stores elements in an ordered way but may contain duplicate elements.
Vector is synchronized whereas ArrayList is not. The Vector class provides the capability to implement
a growable array of objects. ArrayList and Vector class both implement the List interface. Both classes
are implemented using dynamically resizable arrays, providing fast random access and fast traversal.
In vector the data is retrieved using the elementAt() method while in ArrayList, it is done using the get()
method. ArrayList has no default size while vector has a default size of 10. when you want programs to
run in multithreading environment then use concept of vector because it is synchronized. But ArrayList
is not synchronized so, avoid use of it in a multithreading environment.
The Iterator is an interface, used to traverse through the elements of a Collection. It is not advisable to
modify the collection itself while traversing an Iterator.
The Collections API is a set of classes and interfaces that support operations on collections of objects.
Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.
The Set interface provides methods for accessing the elements of a finite mathematical set. Sets do not
allow duplicate elements.
Stack is a data structure that is based on last-in-first-out rule (LIFO), while queues are based on First-
in-first-out (FIFO) rule.
The properties class is a subclass of Hashtable that can be read from or written to a stream. It also
provides the capability to specify a set of default values to be used.
Which implementation of the List interface provides for the fastest insertion of a new element
into the middle of the list?
a. Vector
b. ArrayList
c. LinkedList
d. None of the above
ArrayList and Vector both use an array to store the elements of the list. When an element is inserted
into the middle of the list the elements that follow the insertion point must be shifted to make room for
the new element. The LinkedList is implemented using a doubly linked list; an insertion requires only
the updating of the links at the point of insertion. Therefore, the LinkedList allows for fast insertions and
deletions.
This class implements the set interface, backed by a hash table (actually a HashMap instance). It
makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the
order will remain constant over time. This class permits the Null element.
This class offers constant time performance for the basic operations (add, remove, contains and size),
assuming the hash function disperses the elements properly among the buckets.
What are differences between Enumeration, ArrayList, Hashtable and Collections and
Collection?
Enumeration: It is series of elements. It can be use to enumerate through the elements of a vector,
keys or values of a hashtable. You can not remove elements from Enumeration.
ArrayList: It is re-sizable array implementation. Belongs to 'List' group in collection. It permits all
elements, including null. It is not thread -safe.
Hashtable: It maps key to value. You can use non-null value for key or value. It is part of group Map in
collection.
An ArrayList is resizable, where as, an array is not. ArrayList is a part of the Collection Framework. We
can store any type of objects, and we can deal with only objects. It is growable. Array is collection of
similar data items. We can have array of primitives or objects. It is of fixed size. We can have multi
dimensional arrays.
Yes you can limit the initial capacity. We can construct an empty vector with specified initial capacity
The functionality of Enumeration interface is duplicated by the Iterator interface. Iterator has a remove()
method while Enumeration doesn't. Enumeration acts as Read-only interface, because it has the
methods only to traverse and fetch the objects, where as using Iterator we can manipulate the objects
also like adding and removing the objects.