0% found this document useful (0 votes)
8 views

Collection Framework

Uploaded by

Binod Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Collection Framework

Uploaded by

Binod Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 60

Collection Framework

Collections in java is a framework that


provides an architecture to store and
manipulate the group of objects.
All the operations that you perform on a data
such as searching, sorting, insertion,
manipulation, deletion etc. can be performed
by Java Collections.
Contd…

Iterable interfacebase iterface


collection interfaceit extends Iterable
interface
1.setHashSet,LinkedHashSet,TreeSet
2.listArrayList,LinkedList,Vector,Stack
3.Queue->Dqueue,PriorityQueue.
3.mapHashMap,LinkedHashMap,TreeMap,Ha
shTable
Collection Framework
Array Collection

Fix in size/Static in nature Grow able size/Dynamic nature

Group of Homogeneous primitive Group of heterogeneous or


type or object type element Homogeneous object type
elements
Manipulation of elements creates No complexity, readymade
programing complexity functions are available

Memory wastage is possible No memory wastage

No underlying data structure Uses underlying data structure


Methods of collection Interface

public boolean add(Object element) is used to insert an element in this


collection.

public boolean addAll(Collection c) is used to insert the specified collection


elements in the invoking collection.

public boolean remove(Object element) is used to delete an element from this


collection.
public boolean removeAll(Collection c) is used to delete all the elements of
specified collection from the invoking
collection.
public boolean retainAll(Collection c) is used to delete all the elements of
invoking collection except the specified
collection.
public int size() return the total number of elements in
the collection.
public void clear() removes the total no of element from the
collection.
public boolean containsAll(Collection c) is used to search the specified collection in
this collection.

public Iterator iterator() returns an iterator.

public Object[] toArray() converts collection into array.

public boolean isEmpty() checks if collection is empty.

public boolean equals(Object element) matches two collection.

public int hashCode() returns the hashcode number for collection.


ArrayList

Java ArrayList class uses a dynamic array for storing


the elements. It inherits AbstractList class and
implements List interface.
The important points about Java ArrayList class are:
Java ArrayList class can contain duplicate elements.
Java ArrayList class maintains insertion order.
Java ArrayList class is non synchronized.
Java ArrayList allows random access because array
works at the index basis.
In Java ArrayList class, manipulation is slow because a
lot of shifting needs to be occurred if any element is
removed from the array list.
Interview Questions

??What is difference between Array and


ArrayList?
Constructor

Constructor Description

ArrayList() It is used to build an empty array list.


ArrayList(Collection c) It is used to build an array list that is
initialized with the elements of the
collection c.
ArrayList(int capacity) It is used to build an array list that has
the specified initial capacity.
Methods…

Method Description

void add(int index, Object element) It is used to insert the specified element
at the specified position index in a list.

boolean addAll(Collection c) It is used to append all of the elements


in the specified collection to the end of
this list, in the order that they are
returned by the specified collection's
iterator.

void clear() It is used to remove all of the elements


from this list.
int lastIndexOf(Object o) It is used to return the index in this list of
the last occurrence of the specified
element, or -1 if the list does not contain
this element.
Iterator interface

Iterator interface provides the facility of


iterating the elements in forward direction
only.
Method Description

public boolean hasNext() It returns true if iterator has more


elements.
public Object next() It returns the element and moves the
cursor pointer to the next element.

public void remove() It removes the last elements returned


by the iterator. It is rarely used.
LinkedList

Java LinkedList class uses doubly linked list to store


the elements. It provides a linked-list data structure.
It inherits the AbstractList class and implements List
and Deque interfaces.
The important points about Java LinkedList are:
Java LinkedList class can contain duplicate elements.
Java LinkedList class maintains insertion order.
Java LinkedList class is non synchronized.
In Java LinkedList class, manipulation is fast because
no shifting needs to be occurred.
Java LinkedList class can be used as list, stack or
queue.
Constructor

Constructor Description

LinkedList() It is used to construct an empty list.


LinkedLis(Collection c) It is used to construct a list containing
the elements of the specified collection,
in the order they are returned by the
collection's iterator.
Methods

Method Description

Void add(int index, Object element) It is used to insert the specified element at
the specified position index in a list.

void addFirst(Object o) It is used to insert the given element at the


beginning of a list.

void addLast(Object o) It is used to append the given element to the


end of a list.

int size() It is used to return the number of elements


in a list

boolean add(Object o) It is used to append the specified element to


the end of a list.

boolean contains(Object o) It is used to return true if the list contains a


specified element.
ListIterator Interface

ListIterator Interface is used to traverse the


element in backward and forward direction.
Method Description

boolean hasNext() This method return true if the list iterator


has more elements when traversing the list
in the forward direction.

Object next() This method return the next element in the


list and advances the cursor position.

boolean hasPrevious() This method return true if this list iterator


has more elements when traversing the list
in the reverse direction.

Object previous() This method return the previous element in


the list and moves the cursor position
backwards.
Interview Question:

What is difference between ArrayList and


Vector?
What is difference between Size and
Capacity?
What is generics in java?
What is difference between pop and peek?
What is difference between list and set
interface?
Vector

Vector is like the dynamic array which can


grow or shrink its size.
Unlike array, we can store n-number of
elements in it as there is no size limit.
It is a part of Java Collection framework since
Java 1.2. It is found in the java.util package.
It is recommended to use the Vector class in
the thread-safe implementation only
Contd…

1).vector():It constructs an empty vector


with the default size as 10.
2).vector(int initialCapacity):It constructs
an empty vector with the specified initial
capacity and with its capacity increment
equal to zero.
3).vector(int initialCapacity, int
capacityIncrement):It constructs an empty
vector with the specified initial capacity and
capacity increment.
 1)add()It is used to append the specified element in the given
vector.
 2)addAll():It is used to append all of the elements in the specified
collection to the end of this Vector.
 3)addElement():It is used to append the specified component to
the end of this vector. It increases the vector size by one.
 4)capacity(): It is used to get the current capacity of this vector.
 5)clear():It is used to delete all of the elements from this vector.
 6)clone():It returns a clone of this vector.
 7)contains():It returns true if the vector contains the specified
element.
 8)containsAll():It returns true if the vector contains all of the
elements in the specified collection.
 9)copyInto():It is used to copy the components of the vector into
the specified 0)
 array.
 10)elementAt():It is used to get the component at the specified index.
 11)elements():It returns an enumeration of the components of a
vector.
 12)ensureCapacity():It is used to increase the capacity of the vector
which is in use, if necessary. It ensures that the vector can hold at
least the number of components specified by the minimum capacity
argument.
 13)equals():It is used to compare the specified object with the vector
for equality.
 14)firstElement():It is used to get the first component of the vector.
 15)forEach():It is used to perform the given action for each element
of the Iterable until all elements have been processed or the action
throws an exception.
 vector.
 17)hashCode():It is used to get the hash code value
of a vector.
 18)indexOf():It is used to get the index of the first
occurrence of the specified element in the vector. It
returns -1 if the vector does not contain the
element.
 19)insertElementAt():It is used to insert the
specified object as a component in the given vector
at the specified index.
 20)isEmpty():It is used to check if this vector has
no components.
Contd…

21)iterator():It is used to get an iterator over


the elements in the list in proper sequence.
22)lastElement():It is used to get the last
component of the vector.
23)lastIndexOf()It is used to get the index of
the last occurrence of the specified element
in the vector. It returns -1 if the vector does
not contain the element.
24)listIterator():It is used to get a list
iterator over the elements in the list in proper
Stack

It works based on LIFO (Last in First Out).


Example: Set of bowls….There are 5 bowls ,If
you want to retrieve first bowl then you need
to remove 5th,4th,3rd,2nd,1st.
Stack is used to store values on the basis of
LIFO.
Statck uses top pointer to point the last
element
Methods:

1.push(item):This method inserts an element


onto top of the element.
2.pop():This method removes an element from
the top of stack and returns the same element
as the value of thaat function.
3.peek():This method looks at the top element
of the stack without removing it
4.empty():checks the stack is empty of not
5.search(item):It searches for the specified
object and returns the position of the object.
Set Interface

It should contain unique elements.


It stores only one null value.
It is implemented by HashSet ,
LinkedHashSet ,TreeSet
HashSet class

Java HashSet class is used to create a collection


that uses a hash table for storage.
 It inherits the AbstractSet class and implements
Set interface.
The important points about Java HashSet class
are:
HashSet stores the elements by using a
mechanism called hashing.
HashSet contains unique elements only.
HashSet contains only one null value.
It does not maintain insertion order.
Constructor

Constructor Description

HashSet() It is used to construct a default HashSet.

HashSet(Collection c) It is used to initialize the hash set by


using the elements of the collection c.
Method Description

void clear() It is used to remove all of the elements


from this set.
boolean contains(Object o) It is used to return true if this set
contains the specified element.
boolean add(Object o) It is used to adds the specified element
to this set if it is not already present.
boolean isEmpty() It is used to return true if this set
contains no elements.
boolean remove(Object o) It is used to remove the specified
element from this set if it is present.
Object clone() It is used to return a shallow copy of this
HashSet instance: the elements
themselves are not cloned.
Iterator iterator() It is used to return an iterator over the
elements in this set.
int size() It is used to return the number of
elements in this set.
LinkedHashSet

Java LinkedHashSet class is a Hash table and


Linked list implementation of the set interface.
It inherits HashSet class and implements Set
interface.
The important points about Java
LinkedHashSet class are:
Contains unique elements only like HashSet.
Provides all optional set operations, and
permits null elements.
Maintains insertion order.
Constructor

Constructor Description

LinkedHashSet() It is used to construct a default HashSet.

LinkedHashSet(Collection c) It is used to initialize the hash set by


using the elements of the collection c.
TreeSet

Java TreeSet class implements the Set


interface that uses a tree for storage.
 The objects of TreeSet class are stored in
ascending order.
The important points about Java TreeSet
class are:
Contains unique elements only like HashSet.
Access and retrieval times are quiet fast.
Maintains ascending order.
Can’t store null value.
Constructor

Constructor Description

TreeSet() It is used to construct an empty tree set


that will be sorted in an ascending order
according to the natural order of the tree
set.
TreeSet(Collection c) It is used to build a new tree set that
contains the elements of the collection
c.
Contd…

boolean remove(Object o) It is used to remove the first occurence


of the specified element in a list.
Object getFirst() It is used to return the first element in a
list.
Object getLast() It is used to return the last element in a
list.
int indexOf(Object o) It is used to return the index in a list of
the first occurrence of the specified
element, or -1 if the list does not contain
any element.

int lastIndexOf(Object o) It is used to return the index in a list of


the last occurrence of the specified
element, or -1 if the list does not contain
any element.
Methods

Method Description

boolean addAll(Collection c) It is used to add all of the elements in


the specified collection to this set.
boolean contains(Object o) It is used to return true if this set
contains the specified element.
boolean isEmpty() It is used to return true if this set
contains no elements.
boolean remove(Object o) It is used to remove the specified
element from this set if it is present.
void add(Object o) It is used to add the specified element to
this set if it is not already present.
void clear() It is used to remove all of the elements
from this set.
Contd…

Object clone() It is used to return a shallow copy of this


TreeSet instance.
Object first() It is used to return the first (lowest)
element currently in this sorted set.
Object last() It is used to return the last (highest)
element currently in this sorted set.
int size() It is used to return the number of
elements in this set.
Queue

Java Queue interface orders the element in


FIFO(First In First Out) manner.
 In FIFO, first element is removed first and
last element is removed at last.
Methods

boolean add(object) It is used to insert the specified


element into this queue and return
true upon success.

boolean offer(object) It is used to insert the specified


element into this queue.
Object remove() It is used to retrieves and removes
the head of this queue.
Object poll() It is used to retrieves and removes
the head of this queue, or returns
null if this queue is empty.

Object element() It is used to retrieves, but does not


remove, the head of this queue.
Object peek() It is used to retrieves, but does not
remove, the head of this queue, or
returns null if this queue is empty.
PriorityQueue

This quese it process elements based on their


prioroty.
It follows FIFO pattern
Map Interface

A map contains values on the basis of key i.e.


key and value pair.
 Each key and value pair is known as an
entry.
Map contains only unique keys.
Values can be duplicate.
Map is useful if you have to search, update or
delete elements on the basis of key.
Key-value pair.
Methods

Method Description

Object put(Object key, Object value) It is used to insert an entry in this


map.
void putAll(Map map) It is used to insert the specified map
in this map.
Object remove(Object key) It is used to delete an entry for the
specified key.
Object get(Object key) It is used to return the value for the
specified key.
boolean containsKey(Object key) It is used to search the specified key
from this map.
Set keySet() It is used to return the Set view
containing all the keys.
Set entrySet() It is used to return the Set view
containing all the keys and values.
Map.Entry Interface

Entry is the sub interface of Map. So we will


be accessed it by Map.Entry name. It
provides methods to get key and value.

Method Description

Object getKey() It is used to obtain key.


Object getValue() It is used to obtain value.
HashMap class

Java HashMap class implements the map


interface by using a hashtable. It inherits
AbstractMap class and implements Map
interface.
The important points about Java HashMap
class are:
A HashMap contains values based on the key.
It contains only unique elements.
It may have one null key and multiple null
values.
It does not maintains insertion order.
Constructor

Constructor Description

HashMap() It is used to construct a default


HashMap.
HashMap(Map m) It is used to initializes the hash map
by using the elements of the given
Map object m.
Method

Method Description

void clear() It is used to remove all of the


mappings from this map.
boolean containsKey(Object key) It is used to return true if this map
contains a mapping for the specified
key.
boolean containsValue(Object value) It is used to return true if this map
maps one or more keys to the
specified value.
boolean isEmpty() It is used to return true if this map
contains no key-value mappings.
Object clone() It is used to return a shallow copy of
this HashMap instance: the keys and
values themselves are not cloned.
Interview Questions

Q.1 What is difference between HashSet and


HashMap?
LinkedHashMap

Java LinkedHashMap class is Hash table and


Linked list implementation of the Map interface,
with predictable iteration order.
 It inherits HashMap class and implements the
Map interface.
The important points about Java LinkedHashMap
class are:
A LinkedHashMap contains values based on the
key.
It contains only unique elements.
It may have one null key and multiple null values.
Contd…

It is same as HashMap instead maintains


insertion order.
LinkedHashMap and HashMap are non-
synchronized.
Constructor

Constructor Description

LinkedHashMap() It is used to construct a default


LinkedHashMap.
LinkedHashMap(Map m) It is used to initialize the LinkedHashMap
with the elements from the given Map
class m.
TreeMap class

Java TreeMap class implements the Map interface


by using a tree. It provides an efficient means of
storing key/value pairs in sorted order.
The important points about Java TreeMap class are:
A TreeMap contains values based on the key.
It contains only unique elements.
It cannot have null key but can have multiple null
values.
It is same as HashMap instead maintains ascending
order.
TreeMap is non synchronized.
Constructor

Constructor Description

TreeMap() It is used to construct an empty tree


map that will be sorted using the natural
order of its key.
TreeMap(Map m) It is used to initialize a tree map with the
entries from m, which will be sorted
using the natural order of the keys.
Methods

Method Description

boolean containsKey(Object key) It is used to return true if this map


contains a mapping for the specified key.

boolean containsValue(Object value) It is used to return true if this map maps


one or more keys to the specified value.

Object firstKey() It is used to return the first (lowest) key


currently in this sorted map.
Object get(Object key) It is used to return the value to which
this map maps the specified key.
Object lastKey() It is used to return the last (highest) key
currently in this sorted map.
Methods

Object remove(Object key) It is used to remove the mapping for this


key from this TreeMap if present.
void putAll(Map map) It is used to copy all of the mappings
from the specified map to this map.
Set entrySet() It is used to return a set view of the
mappings contained in this map.
int size() It is used to return the number of key-
value mappings in this map.
Collection values() It is used to return a collection view of
the values contained in this map.
Method

Method Description

Object get(Object key) It is used to return the value to which


this map maps the specified key.
void clear() It is used to remove all mappings from
this map.
boolean containsKey(Object key) It is used to return true if this map maps
one or more keys to the specified value.
Contd…

Set entrySet() It is used to return a collection view of


the mappings contained in this map.
Set keySet() It is used to return a set view of the
keys contained in this map.
Object put(Object key, Object value) It is used to associate the specified
value with the specified key in this
map.
int size() It is used to return the number of key-
value mappings in this map.
Collection values() It is used to return a collection view of
the values contained in this map.
Hashtable class

Java HashTable class implements a hashtable, which


maps keys to values
The important points about Java Hashtable class are:
A Hashtable is an array of list. Each list is known as a
bucket.
 The position of bucket is identified by calling the
hashcode() method.
A Hashtable contains values based on the key.
It contains only unique elements.
It may have not have any null key or value.
It is synchronized.
Constructor

Constructor Description

Hashtable() It is the default constructor of hash table


it instantiates the Hashtable class.
Hashtable(int size) It is used to accept an integer parameter
and creates a hash table that has an
initial size specified by integer value
size.
Methods

Method Description

void clear() It is used to reset the hash table.


boolean contains(Object value) This method return true if some value
equal to the value exist within the hash
table, else return false.
boolean containsValue(Object value) This method return true if some value
equal to the value exists within the hash
table, else return false.
boolean containsKey(Object key) This method return true if some key
equal to the key exists wi
Contd…

boolean isEmpty() This method return true if the hash table


is empty; returns false if it contains at
least one key.
void rehash() It is used to increase the size of the hash
table and rehashes all of its keys.
Object get(Object key) This method return the object that
contains the value associated with the
key.
Object remove(Object key) It is used to remove the key and its
value. This method return the value
associated with the key.
int size() This method return the number of
entries in the hash table.
WAP to copy all mappings from the specified
map to another map.
WAP to get a collection view of the contained
in this map.
WAP to get a key-value mapping associated
with the greatest key and the last currently in
map.

You might also like