Collection
Collection
2. Memory point of view arrays are not 2. Memory point of view collections are
recommended to use recommended to use.
3. Performance point of view arrays are 3. Performance point of view collections are not
recommended to use recommended to use.
4. Arrays can hold only homogeneous elements 4. Collections can hold both homogeneous and
heterogeneous elements.
5. Arrays can hold both primitives as well as 5. Collections can hold only objects.
objects
6. For any requirement, there is no ready method 6. For every requirement ready made method
support compulsory programmer has to code support is available. Being a programmer we
explicitly. have to know how to use those methods and we
are not responsible to implement those.
Vector
ArrayList
1. No method is synchronized in the ArrayList 1. All methods in Vector are synchronized.
class
4. Introduced in 1.2 version and it is non legacy 4. Introduced in 1.0 version and it is 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.
Public static List synchronizedSet(Set s)
Public static List synchronizedMap(Map m)
Q23. What is difference between size and capacity of a Collection Object?
size means number of objects present where as capacity means no of objects it can accommodate.
Q24. What is difference between ArrayList and Linked List?
ArrayList LinkedList
1. The underlying data structure is resizable or 1. The underlying data structure is Double
growable array. Linked List.
2. This is Best choice if frequent operation is 2. This is Best choice if frequent operation is
retrieval and worst choice if frequent operation is insertion or deletion in the middle and worst
insertion or deletion in the middle. choice if frequent operation is retrieval .
3. This class implements Serializable , Cloneable 3. This class implements Serializable , Cloneable
and RandomAccess interfaces. but not RandomAccess interface.
Q25. What are legacy classes and interfaces present in Collections framework ?
• Enumeration ---Interface
• Dictonary ------Abstract class
• Hashtable -----Concrete class
• Properties -----Concrete class
• Vector -----Concrete class
• Stack -----Concrete class
1. It is legacy interface and introduced in 1.0 1 It is non-legacy and introduced in 1.2 version
version
2Applicable only for legacy classes and it is not 2Applicable for any Collection implemented
universal cursor class object.
3While iterating the elements we are not allowed 3While iterating we can perform removal also in
to remove the objects just we can perform only addition to read operation.
read operation
4By using elements() method we can get 4. By using iterator() method we can get
Enumeration object Iterator
object
Q35. What are major enhancements in 1.4 version of collection frame work?
LinkedHashSet
LinkedHashMap
IdentityHashMap
It is Collection object which can be used to represent a group of objects according to some sorting
order.
• The underlying datastructure is Balanced tree
• Duplicates are not allowed
• All objects are stored according to some sorting order hence insertion order is not preserved
• Heterogeneous objects are not allowed violation leads to ClassCastException
• For an Empty TreeSet as firs element null value can be inserted but after inserting that first
value if we are trying to insert any other objects then we will get NullPointerException
• For an non empty TreeSet if we are trying to inser null value at run time u will get
NullPointerException
1This can be used for natural sorting order 1This can be used for implementing customized
sorting
2This interface present in java.lang package 2 This is present in java.util package
1The underlying data structure is Hashtable 1The underlying data structure is balanced tree
2Heterogeneous objects are allowed 2 Heterogeneous objects are not allowed
bydefalut
3Insertion order is not preserved and it is based 3 Insertion order is not preserved and all the
on hashcode of the objects objects are inserted according to some sorting
order.
interface Map{
//more code here
interface Entry{
Object getKey()
Object getValue()
Object setValue(Object new)
}
}
Q43. Explain about HashMap?
It is a Map Object which can be used used to represent a group of objects as key-value pairs.
• The underlying data structure is Hashtable
• Duplicaes keys are not allowed duplicate values are allowed
• Insertion order is not preserved because insertion is based on hashcode of keys.
• Heterogeneous objects are allowed for both keys and values
• null key is allowed only once
• null values are allowed multiple times
• Introduced in 1.2 version
Q44. Explain about LinkedHashMap?
It is child class of HashMap. It is exactly same as HashMap except the following difference.
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
Q45. Differences between HashMap and LinkedHashMap ?
HashMap LinkedHashMap
1.The underlying data structure is Hashtable 1.The underlying data structure is a combination
of Hashtable and linkedlist
2.Insertion order is not preserved and it is based 2 Insertion order is preserved
on hashcode of keys
3.Introduced in 1.2 version 3 Introduced in 1.4 version.
1.The underlying data structure is Hashtable 1.The underlying data structure of Hashtable
2.No method is synchronized and hence 2 .All methods are synchronized and hence it is
HashMap object is not thread safe thread safe
4.null insertion is possible for both keys and 4. null insertion is not possible for both key and
values value violation leads to NullPointerException
5.Introduced in 1.2 version and it is non legacy 5. Introduced in 1.0 version and it is legacy
Q47. What is IdentityHashMap?
It is exactly same as HashMap except the following difference.
In the HashMap JVM uses equals() method to identify duplicate keys but in the case of
IdentityHashMap JVM uses == operator for this.
Hashtable is a legacy Map and can be used to store objects as key value pairs.
• The underlying data sturucture is Hashtabe
• Duplicates keys are not allowed but duplicate values are allowed
• null insertion is not possible for both keys and values
• all methods are synchronized
• insertion order is not preserved because it is based on hashcode of keys
• heterogeneous Objects are allowed for both keys and values
• introduced in 1.0 version it is legacy class
Q53. What is PriorityQueue?
It represents a data structure to hold group of individual objects prior to processing based on some
priority .it can be natural sorting order and it can be customized sorting order described by Comparator.
It is the implementation class of Queue interface.
• Insertion order is not preserved because here insertion is done based on some sorting order
• Duplicates are not allowed
• null insertion is not possible even as first element also
• If we are depending on natural sorting order Objects should be homogeneous violation leads to
ClassCastException
• If we are depending on customized sorting order Objects can be heterogeneous also.
Q55. We are planning to do an indexed search in a list of objects. Which of the two Java
collections should you use: ArrayList or LinkedList?
ArrayList