You are on page 1of 31

Collection

CDAC PATNA
Collection(I)

List (I) Set (I)

ArraryList (C)
CDAC PATNA
LinkedList(C) HashSet (C) SortedSet(I)
Vector (C) Queue (I)
LinkedHashSet (C)

Stack (C) PriorityQueue (C) TreeSet (C)


Collection-
• It is an interface
• If we want to represent a group of individual objects as a
single entity then we should go for collection.
 Methods:- CDAC PATNA
• boolean add (object o)
• boolean addAll(Collection c)
• boolean remove(object o)
• boolean removeAll(Collection c)
• boolean retainAll(Collection c)
• Void clear()
• boolean contains(object o)
CDAC PATNA
• Boolean containsAll(Collection c)
• isEmpty()
• size()
List
 It is child interface of collection
 Insertion order is preserved w.r.t index

CDAC PATNA
 Duplicates are allowed
 Index is very important
Mathods:-
• add(object o)
• add(index,object)
• addAll(index,Collection c)
• remove(object o)
• remove(int index)
• get(index)
• set(index,object o)
• indexOf(object o)
CDAC PATNA
• lastIndexOf(object o)
ArrayList
 It is class
 Duplicates allowed and insertion order preserved
 Holds heterogeneous objects
CDAC PATNA
Constructors :-
• ArrayList l = new ArrayList();
• ArrayList l = new ArrayList(int initialcapacity);
LinkedList
 It is class
 Duplicates allowed and insertion order preserved
 Holds heterogeneous objects
CDAC PATNA
Constructors :-
• LinkedList l = new LinkedList();
• LinkedList l = new LinkedList(Collection c );
Mathods:-
• void addFirst(Object O)
• void addLast(Object O)
• Object getFirst()
CDAC PATNA
• Object getLast()
• Object removeFirst()
• Object removeLast()
Vector
 Growable in nature
 Duplicates allowed and insertion order preserved
 Holds heterogeneous objects
CDAC PATNA
 Null is allowed

Constructors :-
• Vector l = new Vector();
• Vector I = new Vector(int initialcapacity);
• Vector l = new Vector(Collection c );
Mathods:-
• addElement(Object O)
• removeElement(Object O)
• removeElementAt(int index)
CDAC PATNA
• removeAllElements()
Stack
 Child class of vector
 LIFO order

Methods :- CDAC PATNA


• Object push(object o)
• Object peek()
• boolean empty()
• int search(object o)
Set
 Duplicates are not allowed
 Insertion order is not preserved
CDAC PATNA
HashSet
 It is implementation class of set interface
 Duplicates are not allowed
 Insertion order is not preserved

CDAC PATNA
 Null is allowed but only once
 Heterogeneous objects are allowed
Constructors :-
• HashSet h = new HashSet();
• HashSet h = new HashSet(int initialcapacity);
• HashSet h = new HashSet(Collection c);
LinkedHashSet
 It is the child class of HashSet
 It is exactly same as HashSet (including constructors and methods)
except one difference:-

CDAC PATNA
SortedSet
Methods :-
• first()
• last()


CDAC PATNA
headset(object o)
tailSet(object o)
• subSet(object o1, object o2)

❖NOTE – Default natural sorting order for numbers is


ascending and Default natural sorting order for string is
alphabetical order
TreeSet
 It is implementation class of set interface
 Duplicates are not allowed
 Insertion order is not preserved

CDAC PATNA
 Heterogeneous objects are not allowed
 Null is allowed but only once
Constructors :-
• TreeSet t = new TreeSet();
• TreeSet t = new TreeSet(Collection c);
➢Queue –
• It is the child interface of collection
• FIFO order
• Methods:-
CDAC PATNA
i. boolean offer(object o)
ii. Object poll()
iii. Object peek()
iv. Object element()
➢PriorityQueue –
• Insertion order is not preserved
• Duplicate objects are not allowed
CDAC PATNA
• Null is not allowed
• Object should be homogeneous , otherwise we will get
runtime error/Exception
• Constructor:-

i. PriorityQueue obj = new PriorityQueue();

CDAC PATNA
ii. PriorityQueue obj = new PriorityQueue(new initialcapacity);
iii. PriorityQueue obj = new PriorityQueue(Collection c);
Map

HashMap (C) SortedMap (I)

CDAC PATNA
LinkedHashMap (C)
TreeMap (C)

Hashtable (C)
Map
 Map is not related to Collection i.e this interface defines some special
method
 If we want to represent a group of objects as key-value pairs , then we

CDAC PATNA
should go for Map
 Each key-value pair is called “Entry” (Map is collection of Entry object )
Methods:-
• Object put(object key,object value)
• void putAll(Map m)
• Object get(object key)
• Object remove(Object key)
• boolean containsKey(Object key)
• boolean containsValue(Object value)
CDAC PATNA
• boolean isEmpty()
• int size()
• void clear()
• Set keySet()
• Set entrySet()
HashMap
 Duplicates keys are not allowed but values can be duplicate.
 Heterogeneous objects are allowed for both key and value
 Null is allowed for key(only once)
CDAC PATNA
 Null is allowed for values (any no. of times)
 Best choice in case of searching operation
Constructor:-
• HashMap m = new HashMap();
• HashMap m = new HashMap(int initial capacity);
• HashMap m = new HashMap(Map m);
LinkedHashMap
 It is child class of hash map
 Insertion order is preserved
CDAC PATNA
SortedMap
 It is child interface of Map
 Sorting is based on the key but not based on value
Mathods:-
• firstKey() CDAC PATNA
• lastkey()
• heapMap(key)
• tailMap(key)
• submap(startkey, endkey)
TreeMap
It is the implementation class of sorted Map.
Insertion order is not preserved .
Duplicates keys are not allowed but values can be duplicate
CDAC PATNA
For key( homogeneous type obect)
For values(heterogeneous type object)
Null is not allowed for key but for values, we can use ‘null’
any no. of times
Constructor:-
• TreeMap t = new TreeMap();
• TreeMap t = new TreeMap(Map m);
CDAC PATNA
• TreeMap t = new TreeMap(SortedMap m);
Hashtable
 It is a class
 Insertion order is not preserved .
 Duplicates keys are not allowed but values can be duplicate
CDAC PATNA
 For key and values both (heterogeneous type object)
 Null is not allowed for both key and value.
Constructor:-
• Hashtable t = new Hashtable();
• Hashtable t = new Hashtable(int initialcapacity);
CDAC PATNA
• Hashtable t = new Hashtable(Map m);
THANK YOU
CDAC PATNA

You might also like