Interview Question Collection Framework PDF
Interview Question Collection Framework PDF
com
tutorial4us.com
A Perfect Place for All Tutorials Resources
www.tutorial4us.com
A Perfect Place for All Tutorials Resources
Collection
www.tutorial4us.com
www.tutorial4us.com
It defines set of classes and interfaces which can be used for representing a
group of objects as single entity
It defines set of classes and inter faces which can be used for representing
a group of objects as single entity
· All the objects are arranged in some sorting order (Can be natural
sorting order or customizede).
· Duplicates are not allowed.
Vector
ArrayList
1. No method is synchronized in the 1. All methods in Vector are
ArrayList class synchronized.
2. ArrayList object is not thread safe. 2. Vector is thread safe.
3. Relatively performance is high 3. Relatively performance is low
4. Introduced in 1.2 version and it is 4. Introduced in 1.0 version and it is
non legacy legacy
EX
ArrayList l= new ArrayList();
List l2=Collections.synchronizedList(l);
Similarly we can get synchronized versions of Set and Map objects by the
following methods.
ArrayList LinkedList
1. The underlying data structure is 1. The underlying data structure is
resizable or growable array. Double Linked List.
2. This is Best choice if frequent 2. This is Best choice if frequent
operation is retrieval and worst choice operation is insertion or deletion in
if frequent operation is insertion or the middle and worst choice if
deletion in the middle. frequent operation is retrieval .
3. This class implements Serializable , 3. This class implements Serializable ,
Cloneable and RandomAccess Cloneable but not RandomAccess
interfaces. interface.
· Enumeration ---Interface
· Dictonary ------Abstract class
· Hashtable -----Concrete class
· Properties -----Concrete class
· Vector -----Concrete class
· Stack -----Concrete class
Enumeration Iterator
1. It is legacy interface and 1 It is non-legacy and introduced in
introduced in 1.0 version 1.2 version
2Applicable only for legacy classes 2Applicable for any Collection
and it is not universal cursor implemented class object.
3While iterating the elements we are 3While iterating we can perform
not allowed to remove the objects removal also in addition to read
just we can perform only read operation.
operation
4By using elements() method we can 4. By using iterator() method we can
get Enumeration object get Iterator
object
Ex
Class Beer{
KO,KF,RC,FO
}
In the case of HashSet insertion order is not preserved , but in the case of
LinkedHashSet insertion will be preserved.
HashSet LinkedHashSet
1The Underlying datastructure is 1The underlying datastructure is
Hashtable combination of LinkedList and
Hashtable
2Insertion Order is not preserved 2 Insertion order is preserved.
3Introduced in 1.2 version 3 Introduced in 1.4 version
LinkedHashSet
LinkedHashMap
IdentityHashMap
List Set
1Insertion Order is preserved 1Insertion Order is not preserved
2Duplicate Objects are allowed 2 Duplicate Objects are not allowed
3The implemented classes are 3 The implemented classes are
ArrayList,LinkedList , Vector and HashSet, LinkedHashSet and
Stack classes Tree
· This interface can be used for defining natural sorting order of the
objects.
· It is present in java.lang package
· It contains a method public int compareTo(Object obj1)
Comparable Comparator
1This can be used for natural sorting 1This can be used for implementing
order customized sorting
2This interface present in java.lang 2 This is present in java.util
package package
3Contains only one method: 3 It contains two methods.
public int compare(Object ,Object)
public int compareTo(Object obj1) public Boolean equals(Object)
4 It is marker interface 4 It is not a marker interface.
www.tutorial4us.com
HashSet TreeSet
1The underlying data structure is 1The underlying data structure is
Hashtable balanced tree
2Heterogeneous objects are allowed 2 Heterogeneous objects are not
allowed bydefalut
3Insertion order is not preserved and 3 Insertion order is not preserved
it is based on hashcode of the objects and all the objects are inserted
according to some sorting order.
4null insertion is possible 4 As the first element only null
insertion is possible and in all other
cases we will get NullPointerException
interface Map{
//more code here
interface Entry{
Object getKey()
Object getValue()
Object setValue(Object new)
}}
In the case of HashMap the insertion order is not preserved but in the case
of LinkedHashMap insertion order is preserved. Introduced in 1.4 version
HashMap LinkedHashMap
1.The underlying data structure is 1.The underlying data structure is a
Hashtable combination of Hashtable and
linkedlist
2.Insertion order is not preserved and 2 Insertion order is preserved
it is based on hashcode of keys
3.Introduced in 1.2 version 3 Introduced in 1.4 version.
www.tutorial4us.com
HashMap Hashtable
1.The underlying data structure is 1.The underlying data structure of
Hashtable Hashtable
2.No method is synchronized and 2 .All methods are synchronized and
hence HashMap object is not thread hence it is thread safe
safe
3.Performance is high 3. Performance is low
4.null insertion is possible for both 4. null insertion is not possible for
keys and values both key and value violation leads to
NullPointerException
5.Introduced in 1.2 version and it is 5. Introduced in 1.0 version and it is
non legacy legacy
In the HashMap JVM uses equals() method to identify duplicate keys but
in the case of IdentityHashMap JVM uses == operator for this.
· For non empty TreeMap if we are trying to insert null keys we will get
NullPointerException
· There are no restrictions for null values.
Hashtable is a legacy Map and can be used to store objects as key value
pairs.
All methods present in the Vector are synchronized and hence any method
can be executed by only one thread at a time. It slows down the execution.
But in Array List, no method is synchronized and hence multiple thread are
allowed execute simultaneously which speed up the execution.