What is difference between Iterator and ListIterator?
- ListIterator is the child interface of the Iterator
- Iterator is the single direction cursor where as ListIterator is bidirectional cursor.
- 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.
- Iterator is applicable for every Collecton implemented class object but ListIterator is applicable only for List implemented class objects.
- Iterator can be get by using iterator() of Collection interface where as ListIterator can be get by using listIterator() method of List interface
- both are introduced in 1.2 version
What is difference between Collections and Collection?
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.
Explain about List interface?
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
- Duplicates are allowed
- Insertion order is preserved
Explain about Set interface?
Set is a child interface of Collection interface. it can be used to represent a group of individual objects as a single entity where
- Duplicate objects are not allowed.
- Insertion order is not preserved
Explain about SortedSet interface?
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.
Explain about NavigableSet ?
It is child interface of SortedSet and provides several utility methods for navigation purposes
- It doesn’t allows duplicates
- Insertion order is preserved
- It is introduced in 1.6 version
Explain about Queue interface?
If we want to represent a group of individual objects prior to processing, then we should go for Queue interface. It is child interface of Collection interface.
It has introduced in 1.5 version.
Explain about Map interface?
Remember it is not a child Interface of Collection Interface and hence Map and Collection Interfaces doesn’t have any relationship.
- It can be used for representing a group of Objects as key, value pairs.
- Both keys and values should be objects
- Keys can t be duplicated but values can be duplicated.
- it has introduced in 1.2 version
Explain about SortedMap ?
- 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
Explain about NavigableMap?
- It is child interface of SortedMap and defines several method for navigation purpose
- It is introduced in 1.6 version
Explain about ArrayList class?
ArrayList is a Collection which can be used to represent a group of objects as a single entity.
- it is a implemented class for List interface
- Introduced in 1.2 version
- The underlying data structure is resizable or growable array.
- Insertion order is preserved
- Duplicates are allowed
- Heterogeneous objects are allowed
- null insertion is possible
- This class implements RandomAccess , Serializable , Cloneable interfaces
- Best choice for retrieval purpose and worst if our frequent operation is insertion or deletion in the middle
What is RandomAccess Interface?
- If a collection class implements RandomAccess interface then we can access any of its element with the same speed.
- RandomAccess interface is marker interface and it dosent contains any methods.
- ArrayList and vector classes implements this interface.
Explain about LinkedList class?
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
- Introduced in 1.2 version
- Underlying data Structure is DoubleLinkedList
- Allows duplicates
- Insertion order is preserved
- Allows heterogeneous objects
- null insertion is possible
- LinkedList class implements Seriallizable and Cloneable interface but not RandomAccess interface
- Best choice if frequent operation is insertion or deletion an objects in middle but worst choice if frequent operation is retrieval.
Explain about Vector class?
Vector is a legacy collection class which can be used to represent a group of objects.
- Introduced in 1.0 version. it is legacy class
- The underlying data structure is resizable or growable array.
- Insertion order is preserved
- Duplicates are allowed
- Heterogeneous objects are allowed
- It is a implemented class for List interface
- null insertion is possible
- Vector class implements RandomAccess ,Serializable,Cloneable interfaces
- Best Choice if frequent operation is retrieval and worst choice if frequent operation is insertion or deletion in the middle.
- All methods present in Vector class are synchronized hence Vector class object is thread safe.
What is difference between ArrayList and Vector?
ArrayList
|
Vector
|
1. No method is synchronized in the ArrayList class
|
1. All methods in Vector are 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 non legacy
|
4. Introduced in 1.0 version and it is legacy
|
How we can get synchronized version of ArrayList?
Collections class contains synchronizedList() method for this
Public static List synchronizedList(List l)
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)
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.
What is difference between ArrayList and Linked List?
ArrayList
|
LinkedList
|
1. The underlying data structure is resizable or growable array.
|
1. The underlying data structure is Double Linked List.
|
2. This is Best choice if frequent operation is retrieval and worst choice if frequent operation is insertion or deletion in the middle.
|
2. This is Best choice if frequent operation is insertion or deletion in the middle and worst choice if frequent operation is retrieval .
|
3. This class implements Serializable , Cloneable and RandomAccess interfaces.
|
3. This class implements Serializable , Cloneable but not RandomAccess interface.
|
ArrayList
|
Vector
|
Methods are not synchronized. So this is the better candidate for non threading environment.
|
Methods are internally synchronized. So it will be better in threading environment.
|
Performance wise good.
|
Little bit performance lacking due to synchronization.
|
No default size
|
The default size is 10.
|
No built in support for increasing the array size. When the array was full the size was 50% increased to current size.
|
In constructor itself we can mention the incremental size. By default it grows 100%
|
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
what is difference Enumeration and Iterator?
Enumeration
|
Iterator
|
1. It is legacy interface and introduced in 1.0 version
|
1 It is non-legacy and introduced in 1.2 version
|
2Applicable only for legacy classes and it is not universal cursor
|
2Applicable for any Collection implemented class object.
|
3While iterating the elements we are not allowed to remove the objects just we can perform only read operation
|
3While iterating we can perform removal also in addition to read operation.
|
4By using elements() method we can get Enumeration object
|
4. By using iterator() method we can get Iterator
object
|
What are limitations of Enumeration?
- 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
What is difference between enum and Enumeration?
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
}
Enumeration is cursor to retrieve Objects one by one from Collection objects.
Explain about HashSet class?
- The underlying data structure is Hashtable
- null values are accepted
- duplicates are not allowed
- insertion order is based on hashcode of the object hence insertion order is not preserved
- best suitable if frequent operation is search operations
- HashSet class implements Serializable and Cloneable
- it is implementation class for Set interface
- heterogeneous objects are allowed
- it is introduced in 1.2 version
Differences between HashSet and LinkedHashSet?
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
|
What are differences between List and Set interfaces?
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 ArrayList,LinkedList , Vector and Stack classes
|
3 The implemented classes are HashSet, LinkedHashSet and Tree
|
What is Comparable interface?
- 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)
What is Comparator interface?
- This interface can be used for implementing customized sorting order.
- It is present in java.util package
- It contains two methods
- public int compare(Object ,Object)
- public boolean equals(Object)
What are differences between Comparable and Comparator?
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:
public int compareTo(Object obj1)
|
3 It contains two methods.
public int compare(Object ,Object)
public Boolean equals(Object)
|
No comments:
Post a Comment