9 Key Interfaces of Collection framework
- Collection
- List
- Set
- SortedSet
- NavigableSet
- Queue
- Map
- SortedMap
- NavigableMap
Set Interface
.Duplicates are not allowed
Insertion order is not preserved.Child interface of the collection.
1. Set is the child interface of Collection.
2. If we want to represent a group of individual objects as a single entity, where duplicates are not allowed and insertion order is not preserved then we should go for Set.
3. Set interface doesn’t contain any new methods. So we have to use only Collection interface methods.
Implementation classes: HashSet (1.2 v) and LinkedHashSet(1.4 v)
HashSet:
1. The underlying data structure is Hashtable.
2. Duplicates are not allowed. If we are trying to insert duplicates, we won’t get any compiletime or runtime errors. add() method simply returns false.
3. Insertion order is not preserved and all objects will be inserted based n hash-code of objects.
4. Heterogeneous objects are allowed.
5. ‘null’ insertion is possible.
6. implements Serializable and Clonable interfaces but not RandomAccess.
7. HashSet is the best choice, if our frequent operation is Search operation.
1. The underlying data structure is Hashtable.
2. Duplicates are not allowed. If we are trying to insert duplicates, we won’t get any compiletime or runtime errors. add() method simply returns false.
3. Insertion order is not preserved and all objects will be inserted based n hash-code of objects.
4. Heterogeneous objects are allowed.
5. ‘null’ insertion is possible.
6. implements Serializable and Clonable interfaces but not RandomAccess.
7. HashSet is the best choice, if our frequent operation is Search operation.
SortedSet
1. It is the child interface of set.
2. If we want to represent a group of individual objects according to some sorting order and duplicates are not allowed then we should go for SortedSet
2. If we want to represent a group of individual objects according to some sorting order and duplicates are not allowed then we should go for SortedSet
Difference between List and Set?
LinkedList:
1. The underlying data structure is Double LinkedList.
2. Insertion order is preserved.
3. Duplicates are allowed.
4. Heterogeneous Objects are allowed.
5. Null insertion is possible.
6. LinkedList implements Serializable and Clonable interfaces but not RandomAccess interface.
7. LinkedList is the best choice if our frequent operation is insertion or deletion in the middle.
8. LinkedList is the worst choice if our frequent operation is retrieval operation.
9. Usually we can use LinkedList to implement stacks and queues to provide support for this requirement LinkedList class defines following specific methods.
void addFirst();
void addLast();
Object getFirst();
Object getLast();
Object removeFirst();
Object removeLast();
LinkedList Constructors:
LinkedList l1=new LinkedList();
1. creates an empty LinkedList Object. LinkedList l1=new LinkedList(Collection c);
2. Creates an equivalent LinkedList Object for the given Collection.
1. The underlying data structure is Double LinkedList.
2. Insertion order is preserved.
3. Duplicates are allowed.
4. Heterogeneous Objects are allowed.
5. Null insertion is possible.
6. LinkedList implements Serializable and Clonable interfaces but not RandomAccess interface.
7. LinkedList is the best choice if our frequent operation is insertion or deletion in the middle.
8. LinkedList is the worst choice if our frequent operation is retrieval operation.
9. Usually we can use LinkedList to implement stacks and queues to provide support for this requirement LinkedList class defines following specific methods.
void addFirst();
void addLast();
Object getFirst();
Object getLast();
Object removeFirst();
Object removeLast();
LinkedList Constructors:
LinkedList l1=new LinkedList();
1. creates an empty LinkedList Object. LinkedList l1=new LinkedList(Collection c);
2. Creates an equivalent LinkedList Object for the given Collection.
Differences between ArrayList and LinkedList
Vector class details?
1. The underlying Data structure for the vector is resizable array or growable array.
2. Duplicate objects are allowed.
3. Insertion order is preserved.
4. 'null' insertion is possible.
5. Heterogeneous objects are allowed.
6. Vector class implemented Serializable, Cloneable and RandomAccess interfaces.
7. Most of the methods present in Vector are synchronized. Hence Vector object is Thread-safe.
8. Best choice if the frequent operation is retrieval.
Vector Specific methods:
For adding objects:
1. add(Object o) [from collection – List(I)]
2. add(int index, Object o) [from List]
3. addElement(Object o) [from Vector]
4. For removing Objects:
5. Remove(Object o) [from collection]
6. removeElement(Object o) [from Vector]
7. remove(int index) [from List)
8. RemoveElementAt(int index) [from collection]
9. removeAllElements() [from Vector]
For Accessing Elements:
Object get(int index) [from Collection]
Object elementAt(int index) [from Vector]
Object firstElement() [from Vector]
Object lastElement() [from Vector]
Other Methods:
int size();
int capacity();
2. Duplicate objects are allowed.
3. Insertion order is preserved.
4. 'null' insertion is possible.
5. Heterogeneous objects are allowed.
6. Vector class implemented Serializable, Cloneable and RandomAccess interfaces.
7. Most of the methods present in Vector are synchronized. Hence Vector object is Thread-safe.
8. Best choice if the frequent operation is retrieval.
Vector Specific methods:
For adding objects:
1. add(Object o) [from collection – List(I)]
2. add(int index, Object o) [from List]
3. addElement(Object o) [from Vector]
4. For removing Objects:
5. Remove(Object o) [from collection]
6. removeElement(Object o) [from Vector]
7. remove(int index) [from List)
8. RemoveElementAt(int index) [from collection]
9. removeAllElements() [from Vector]
For Accessing Elements:
Object get(int index) [from Collection]
Object elementAt(int index) [from Vector]
Object firstElement() [from Vector]
Object lastElement() [from Vector]
Other Methods:
int size();
int capacity();
Constructors of vector class:
1. Vector v=new Vector();
Creates an empty vector object with default initial capacity 10, once vector reaches it’s max capacity a new vector object will be created with new capacity =2*current capacity.
2. Vector v=new Vector(int initialCapacity);
creates an empty Vector Object with specified initial capacity.
3. Vector v=new Vector(int initialCapacity, int incrementalCapacity);
4. Vector v=new Vector(Collection c);
Creates an equivalent Vector object for the given collection.
1. Vector v=new Vector();
Creates an empty vector object with default initial capacity 10, once vector reaches it’s max capacity a new vector object will be created with new capacity =2*current capacity.
2. Vector v=new Vector(int initialCapacity);
creates an empty Vector Object with specified initial capacity.
3. Vector v=new Vector(int initialCapacity, int incrementalCapacity);
4. Vector v=new Vector(Collection c);
Creates an equivalent Vector object for the given collection.
Stack class details (Collection Framework)?
1. It is a child class of Vector.
2. It is Specially designed class for Last in First Out order(LIFO)
Constructor of Stack:
Stack s=new Stack();
Methods in Stack:
1. Object push(Object obj);
For inserting an object to the stack.
2. Object pop():
TO removes and returns top of the stack.
3. Object peak();
To Returns the top of the stack without removal of object.
4. int search(Object obj);
if the specified object is available it returns its offset from top of the statck.
If the object is not available then it returns -1.
5. Object pop();
For inserting an object to the stack
2. It is Specially designed class for Last in First Out order(LIFO)
Constructor of Stack:
Stack s=new Stack();
Methods in Stack:
1. Object push(Object obj);
For inserting an object to the stack.
2. Object pop():
TO removes and returns top of the stack.
3. Object peak();
To Returns the top of the stack without removal of object.
4. int search(Object obj);
if the specified object is available it returns its offset from top of the statck.
If the object is not available then it returns -1.
5. Object pop();
For inserting an object to the stack
Three Cursors of Java - Enumeration
If we want to retrieve Objects one by one from the Collection, then we should go for Cursors.
There are three types of cursors are available in Java.
Enumeration
Iterator
ListIterator Enumeration:
Introduced in 1.0 version (for legacy)
We can use Enumeration to get Objects one by one from the old Collection Objects(Legacy Collection).
We can create Enumeration Object by using elements() method of Vector Class. Public Enumeration elements();
Example:
Enumeration e=v.elements();
Method of Enumeration
Enumeration defines the following two methods
1. public Boolean hasMoreElements();
2. public Object nextElement();
There are three types of cursors are available in Java.
Enumeration
Iterator
ListIterator Enumeration:
Introduced in 1.0 version (for legacy)
We can use Enumeration to get Objects one by one from the old Collection Objects(Legacy Collection).
We can create Enumeration Object by using elements() method of Vector Class. Public Enumeration elements();
Example:
Enumeration e=v.elements();
Method of Enumeration
Enumeration defines the following two methods
1. public Boolean hasMoreElements();
2. public Object nextElement();
Iterator:
1. We can apply iterator concept for any Collection object hence it is universal cursor.
2. By using iterator we can perform both read and remove operations.We can create iterator object by using iterator() method of Collection Interface
public iterator iterator();
Example:
Iterator itr=C.Iterator();
Where C is any Collection Object.
Methods In Iterator
Iterator Interface defines the following three methods
1. public Boolean hasNext()
2. public Object next()
3. public void remove()
1. We can apply iterator concept for any Collection object hence it is universal cursor.
2. By using iterator we can perform both read and remove operations.We can create iterator object by using iterator() method of Collection Interface
public iterator iterator();
Example:
Iterator itr=C.Iterator();
Where C is any Collection Object.
Methods In Iterator
Iterator Interface defines the following three methods
1. public Boolean hasNext()
2. public Object next()
3. public void remove()
Limitations of Iterator:
1. By using Enumeration and Iterator we can move only towards forward direction and we can’t move to the backward direction, and hence these are single direction cursors.
2. By using Iterator we can perform only read and remove operatiions and we can’t perform replacement of new Objects.
Note: To overcome above limitations of Iterator we should go for ListIterator
ListIterator:
1. By using ListIterator we can move either to the forward direction or to the backward direction, and hence ListIterator is bidirectional cursor
2. By using ListIterator we can perform replacement and addition of new Objects in addition to read and remove operations.
3. We can create ListIterator Object by using listIterator() method of List Interface. public ListIterator listIterator()
Example:
ListIterator itr=l.listIterator();
Where l is any List Object.
1. By using ListIterator we can move either to the forward direction or to the backward direction, and hence ListIterator is bidirectional cursor
2. By using ListIterator we can perform replacement and addition of new Objects in addition to read and remove operations.
3. We can create ListIterator Object by using listIterator() method of List Interface. public ListIterator listIterator()
Example:
ListIterator itr=l.listIterator();
Where l is any List Object.
Note: ListIterator is the most powerful cursor but its limitation is, it is applicable only for List implemented class objects and it is not a universal cursor.
Note: LinkedHashSet is the best choice to develop cache based applications, where duplicates are not allowed and insertion order must be preserved.
TreeSet
1. The underlying data structure for TreeSet Is Balanced Tree
2. Duplicate objects are allowed.
3. Insertion order not preserved, but all objects will be inserted according to some sorting order.
4. Heterogeneous objects are not allowed. If we are trying to insert heterogeneous objects then we will get runtime exception saying ClassCastException.
5. Null Insertion is allowed, but only once.
1. The underlying data structure for TreeSet Is Balanced Tree
2. Duplicate objects are allowed.
3. Insertion order not preserved, but all objects will be inserted according to some sorting order.
4. Heterogeneous objects are not allowed. If we are trying to insert heterogeneous objects then we will get runtime exception saying ClassCastException.
5. Null Insertion is allowed, but only once.
Null Acceptance:
1. For Empty TreeSet as the first element null insertion is possible. But After inserting that null if we are trying to insert any another element we will get NullPointerException.
2. For Non empty TreeSet if we are trying to insert Null then we will get NullPointerException.





















No comments:
Post a Comment