Collections: Q1. What Are Limitations of Object Arrays?
Collections: 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 dont
know size in advance , arrays are not 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
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.
It defines the most commonly used methods which can be applicable for any
collection implemented class object
All the objects are arranged in some sorting order (Can be natural sorting
order or customizede).
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.
Best choice for retrieval purpose and worst if our frequent operation is
insertion or deletion in the middle
Allows duplicates
All methods present in Vector class are synchronized hence Vector class object
is thread safe.
Vector
ArrayList
1. No method is synchronized in the ArrayList
class
4. Introduced in 1.2 version and it is non legacy 4. Introduced in 1.0 version and it is legacy
LinkedList
1. The underlying data structure is Double
Linked List.
Enumeration ---Interface
Iterator
1 It is non-legacy and introduced in 1.2 version
2Applicable for any Collection implemented
class object.
3While iterating we can perform removal also in
addition to read operation.
4. By using iterator() method we can get
Iterator
object
While iterating the elements we are not allowed to perform removal operation
While iterating the elements by Iterator we can perform only read and
remove operations. But by using ListIterator we can perform
read,removal, replace and addition of new objects also.
insertion order is based on hashcode of the object hence insertion order is not
preserved
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 doesnt add that
object.
Q33. What is LinkedHashSet?
It is the child class of HashSet. The main difference between HashSet and
LinkedHashSet is:
In the case of HashSet insertion order is not preserved , but in the case of
LinkedHashSet insertion will be preserved.
LinkedHashSet
Q35. What are major enhancements in 1.4 version of collection frame work?
LinkedHashSet
LinkedHashMap
IdentityHashMap
All objects are stored according to some sorting order hence insertion order is
not preserved
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
Set
This interface can be used for defining natural sorting order of the objects.
Comparator
1This can be used for implementing customized
sorting
2 This is present in java.util package
It is marker interface
TreeSet
1The underlying data structure is balanced tree
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.
4null insertion is possible
LinkedHashMap
Hashtable
3.Performance is high
3. Performance is low
5.Introduced in 1.2 version and it is non legacy 5. Introduced in 1.0 version and it is legacy
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
Hashtable is a legacy Map and can be used to store objects as key value pairs.
Duplicates keys are not allowed but duplicate values are allowed
Insertion order is not preserved because here insertion is done based on some
sorting order
It defines several utility methods for arrays like sorting an array or searching
an element in array
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.