Collection_Framework[1]
Collection_Framework[1]
Collection Framework...!
Collection
Collection is growable in nature.
Collection can hold both homogenous and heterogenous data
elements.
Every Collection class is implemented based on some
standard data structure.
Need of Collections
To overcome the above limitations of Arrays we should go for
Collections.
Collections are growable in nature. i.e. Based on our
requirement we can increase (or) Decrease the size.
Collections can hold both homogeneous & heterogeneous
elements.
Every Collection class is implemented based on some
standard data structure. Hence readymade method support
is available for every requirement. Being a programmer we
have to use this method and we are not responsible to
provide implementation.
can increase or
decrease the size.
2. With respect to memory 2. With respect to
arrays are not collections are
recommended to use. recommended to use.
3. With respect to 3. With respect to
Performance Arrays are performance collections
recommended to use. are not recommended to
use.
4. Arrays can hold only 4. Collections can hold
homogeneous data type both homogeneous and
elements. heterogeneous
elements.
5. There is no underlying 5. Every Collections class
data structure for arrays is implemented based
and hence readymade on some standard data
method support is not structure. Hence
available. readymade method
support is available for
every requirement.
6. Array can hold both 6. Collections can hold
primitives and object only objects but not
types. primitive.
Java C++
Collection Container
Collection Framework STL (Standard Template
Library)
Collection VS Collections
- Collection is an interface which can be used to represent
a group of individual objects as a single entity.
- Collections is a utility class present in java.util.package
to define several methods (like Sorting, Searching.) for
Collection objects.
2. List: (1.2 v)
- List is child interface of Collection.
- If we want to represent a group of individual objects
as a single entity where duplicates are allowed and
insertion order preserved then we should go for List.
Collection (1.2 v)
Notes by vk
List (1.2 v)
Stack (1.0 v)
3. Set:
- It is the child interface of Collection.
- If we want to represent a group of individual objects
as a single entity where duplicates are not allowed
and insertion order not preserved then we should go
for Set.
Collection (1.2 v)
Set (1.2 v)
HashSet (1.2 v)
LinkedHashSet (1.4 v)
List Set
Notes by vk
4. SortedSet:
- It is the child interface of Set.
- If we want to represent a group of individual objects
as a single entity where duplicates are not allowed but
all objects should be inserted according to some
sorting order then we should go for SortedSet.
5. NavigableSet:
- It is the child interface of SortedSet if defines several
methods for navigation purposes.
TreeSet (1.2 v)
6. Queue:
- It is child interface of Collection.
- If we want to represent a group of individual objects
prior to processing then we should go for Queue.
- Ex…!
Before sending a mail all mail Id’s we have to store
somewhere and in which order we saved in the same
Notes by vk
LinkedBlockingQueue (1.5v)
PriorityBliockingQueue (1.5v)
Note:
- All the above interfaces (Collection, List, Set, SortedSet,
NavigableSet and Queue) meant for representing a
group of individual objects.
- If we want to represent a group of objects as key value
pairs then we should go for Map Interface.
7. Map:
- Map is not the child interface of Collection.
- If we want to represent a group of individual objects
as key value pairs then should go for Map.
Ex…
Id No Name
Notes by vk
1001 Varun
1002 vk
Both key and value are objects, duplicated keys are not
allowed but values can be allowed.
Legacy Class
Map (I) (1.2v)
Dictionary
(1.0v)
LinkedHashMap (1.4v)
Properties (1.0v)
8. SortedMap:
- It is the child interface of map.
- If we want to represent a group of key value pairs
according to some sorting order of keys then we
should go for SortedMap.
Iterable
Notes by vk
1. Collection Interface:
- If we want to represent a group of individual objects as a
single entity then we should go for Collection.
- In general collection interface is considered as root
interface of Collection Framework.
- Collection interface defines the most common methods
which are applicable for any collection object.
Important method of Collection Interface
a. boolean add (Object o)
b. boolean addAll (Collection c)
c. boolean remove (Object o)
d. boolean removeAll (Collection c)
e. boolean retainAll (Collection c)
To remove all objects except those, present in c
f. void clear ()
g. boolean contains (Object o)
Notes by vk
Note:
Collection interface doesn’t contain any method to retrieve
objects there is no concrete which implements collection
class directly.
2. List Interface
- It is the child interface of Collection
- If we want to represent a group of individual objects as a
single entity where duplicates are allowed and insertion
order must be preserved then we should go for List.
- We can differentiate duplicates by using index.
- We can preserve insertion order by using index, hence
index play very important role in list interface.
3. ArrayList
- The underlined data structure Resizable Array or
Growable Array.
- Duplicates are allowed.
- Insertion order is preserved.
- Heterogeneous objects are allowed (except TreeSet &
TreeMap everywhere heterogeneous objects are
allowed].
- Null insertion is possible.
Ex…
Import java.util.*;
Class ArrayListDemo {
Public static void main (String [] args) {
ArrayList al = new ArrayList ();
al.add (“A”);
al.add (10);
Notes by vk
al.add (“A”);
System,out,println(al); // [A,10,A,null]
al.remove(2);
System.out.println (al); // [A,10, null]
al.add (2, “M”);
al.add (“N”);
System.out.println(al); // [A, 10, M, null, N]
}
}
Usually, we can use Collections to hold and transfer Objects
from one place to another place, to provide support for this
requirement every Collection already implements Serializable
and Cloneable interfaces.
ArrayList and Vector classes implements RandomAccess
interface so that we can access any Random element with
the same speed.
Hence if our frequent operation is retrieval operation then
ArrayList is the best choice.
RandomAccess
Present in java.util package.
It doesn’t contain any methods and it is a Maker interface
Ex…
ArrayList
ArrayList l1 = new ArrayList ();
LinkedList l2 = new LinkedList ();
Notes by vk
Non-Synchronized
ArrayList l1 = new ArrayList ();
Synchronized
List I = Collections.synchronizedList (l1);
- Similarly, we can get Synchronized version of Set, Map
Objects by using the following methods of Collections
class.
public static Set synchronizedSet (Set s);
LinkedList
- The underlying data structure is Double Linked List.
- Insertion order is preserved.
- Duplicates are allowed.
- Heterogeneous Objects are allowed.
- Null insertion is possible.
LinkedList implements Serializable and Cloneable interfaces
but not RandomAccess interface.
LinkedList is the best choice if our frequent operation is
insertion or deletion in the middle.
LinkedList is the worst choice if our frequent operation is
retrieval operation.
Notes by vk
LinkedList Constructors
- LinkedList i1 = new LinkedList ();
Creates an empty LinkedList Object.
- LinkedList l1 = new LinkedList (Collection c);
Creates an equivalent LinkedList Object for the
given Collection.
Ex….
Class LinkedListDemo {
Public static void main(String[] args){
LinkedList l = new LinkedList();
l.add(“vk”);
l.add(30);
l.add(null);
l.add(“vk”);
Notes by vk
l.set(0, “software”);
l.add(0, “venkey”);
l.removeLast();
l.addFirst(“ccc”);
System.out.println(l);
}
}
Vector
- The underlying Data Structure for the vector is resizable
array or growable array.
- Duplicate objects are allowed.
- Insertion order is preserved.
- ‘null’ insertion is possible.
- Heterogeneous objects are allowed.
Notes by vk
Other Methods:
Notes by vk
System.out.println(v.capacity()); //[10]
v.addElement(“A”);
System.out.println(v.capacity());
System.out.println(v);//[1,2,,,,,,10,A] or [20]
}
}
Stack
- It is a child class of Vector.
- 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 stack.
- If the object is not available then it returns -1.
5. Object pop (0;
- For inserting an object to the stack.
Three cursors of Java
Notes by vk