Collections in Java
Collections in Java
COLLECTIONS FAQs
Q1. What are limitations of object Arrays?
• These are fixed in size ie once we created an array object there is no chance of
increasing or decreasing size based on our requirement. Hence If we don’t know size
in advance , arrays are not recommended to use
• Arrays can hold only homogeneous elements.
• There is no underlying data structure for arrays and hence no readymade method
support for arrays. Hence for every requirement programmer has to code explicitly
Collections
Arrays
1. Arrays r fixed in size and hence once we 1. Collections are growable in nature and
created an array we are not allowed to hence based on our requirement we can
increase or decrease the size based on our increase or decrease the size.
requirement.
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
recommended to use not recommended to use.
4. Arrays can hold only homogeneous 4. Collections can hold both homogeneous
elements 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 6. For every requirement ready made method
method support compulsory programmer has support is available. Being a programmer we
to code explicitly. have to know how to use those methods and
we are not responsible to implement those.
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
Collection is an interface which can be used for representing a group of individual objects as
single entity and it acts as root interface of collection frame work.
Collections is an utility class to define several utility methods for Collection implemented
class objects.
List interface is a child interface of Collection interface. This can be used to represent group
of individual objects in as a single entity where
it is child interface of Set interface. it can be used to represent a group of individual objects
in to a single entity where
• All the objects are arranged in some sorting order (Can be natural sorting order or
customizede).
• Duplicates are not allowed.
It is child interface of SortedSet and provides several utility methods for navigation purposes
Remember it is not a child Interface of Collection Interface and hence Map and Collection
Interfaces doesn’t have any relationship.
• If we want to represent a group of objects as key value pairs where all the entries are
arranged according some sorting order of keys then we should go for SortedMap.
• It is child interface of Map.
• It has introduced in 1.2 version
• It is child interface of SortedMap and defines several method for navigation purpose
• It is introduced in 1.6 version
ArrayList is a Collection which can be used to represent a group of objects as a single entity.
LinkedList is a Collection implemented class which can be used for representing a group of
objects as a single entity.LinkedList is the implemetation class for List interface
Vector is a legacy collection class which can be used to represent a group of objects.
Vector
ArrayList
1. No method is synchronized in the ArrayList 1. All methods in Vector are synchronized.
class
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 non 4. Introduced in 1.0 version and it is 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.
size means number of objects present where as capacity means no of objects it can
accommodate.
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 insertion or deletion in the middle and worst
operation is insertion or deletion in the choice if frequent operation is retrieval .
middle.
3. This class implements Serializable , 3. This class implements Serializable ,
Cloneable and RandomAccess interfaces. Cloneable 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
Enumeration Iterator
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 2Applicable for any Collection implemented
not universal cursor class object.
3While iterating the elements we are not 3While iterating we can perform removal also
allowed to remove the objects just we can in addition to read operation.
perform only read operation
4By using elements() method we can get 4. By using iterator() method we can get
Enumeration object Iterator
object
• While iterating the elements we are not allowed to perform removal operation
• It is applicable only for legacy classes and it is not a universal cursor.
• It can retrieve the elements only in forward direction
An enum can be used to define a group of named constants .It has introduced in 1.5 version
Ex
Class Beer{
KO,KF,RC,FO
}
Q32. If we are trying to insert duplicate values in Set what will happen?
If we are trying to insert duplicate objects to the HashSet , we wont get any compile time or
run time errors just the add(Object o) returns false and it doesn’t add that object.
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 Hashtable 1The underlying datastructure is 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
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.
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 Stack HashSet, LinkedHashSet and Tree
classes
• 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 order 1This can be used for implementing
customized sorting
2This interface present in java.lang package 2 This is present in java.util 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.
HashSet TreeSet
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 3 Insertion order is not preserved and all the
based on hashcode of the objects 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)
}
}
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
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 2 Insertion order is preserved
based on hashcode of keys
3.Introduced in 1.2 version 3 Introduced in 1.4 version.
HashMap Hashtable
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
HashMap object is not thread safe is thread safe
3.Performance is high 3. Performance is low
4.null insertion is possible for both keys and 4. null insertion is not possible for both key
values and value violation leads to
NullPointerException
5.Introduced in 1.2 version and it is non 5. Introduced in 1.0 version and it is legacy
legacy
In the HashMap JVM uses equals() method to identify duplicate keys but in the case of
IdentityHashMap JVM uses == operator for this.
In case of HashMap an Object is not eligible for garbage collection if it is associated with
HashMap even though it dosent have any external references. ie HashMap dominates
garbage collector.
But in case of WeakHashMap , if an Object is not having any external references then it is
always eligible for garabage collectoion even though it is associated with
weakHashMap. ie garbage collector dominates WeakHashMap
TreeMap can be used to store a group of objects as key-value pairs where all the entries are
arranged according to some sorting order of keys.
• For empty TreeMap as first entry with null values are allowed but after inserting that
entry if we are trying to insert any other entry we will get NullPointerException
• 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.
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
Q56. Why ArrayList is faster than Vector?
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 ArrayList, no method is synchronized and hence multiple thread are allowed execute
simultaneously which speed up the execution.