0% found this document useful (0 votes)
10 views34 pages

Collections Framework (1)

The document provides an overview of the Java Collections Framework, detailing its purpose, components, and advantages. It explains various collection interfaces such as List, Set, Queue, and Map, along with their implementations like ArrayList, LinkedList, HashSet, and TreeSet. Additionally, it highlights the methods available in these collections and the concept of hashing for efficient data storage and retrieval.

Uploaded by

manikumarande57
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
10 views34 pages

Collections Framework (1)

The document provides an overview of the Java Collections Framework, detailing its purpose, components, and advantages. It explains various collection interfaces such as List, Set, Queue, and Map, along with their implementations like ArrayList, LinkedList, HashSet, and TreeSet. Additionally, it highlights the methods available in these collections and the concept of hashing for efficient data storage and retrieval.

Uploaded by

manikumarande57
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 34

-JAVA

Collections
Framework
Topics
Introduction​
Types of collection interfaces
​list
set
Queue
Map

What is collection?
A Collection represents a single unit of objects,

i.e., a group.
What is a framework in Java?
•. It provides readymade architecture.
•It represents a set of classes and interfaces.
4
Collection frame work

What is Collection framework ?


 Collection Framework is a combination of classes and interface,
which is used to store and manipulate the data in the form of objects. It
provides various classes such as ArrayList, Vector, Stack, and HashSet,
etc. and interfaces such as List, Queue, Set, etc. for this purpose.
allowing them to be manipulated independently of the details of their r
It has:
Interfaces and its implementations, i.e., classes,Algorithm
In order to handle group of objects we can use array of objects.
If we have a class called Employ with members name and id,
if we want to store details of 10 Employees, create an array of object to hold 10 Employ details.
Employ obj[] = new Employ [10];
Collection frameworks
6

Collection Object:
 A collection object is an object which can store group
of other objects.
 A collection object has a class called Collection class or
Container class.
 All the collection classes are available in the package
called java.util (util stands for utility).
 Group of collection classes is called a Collection
Framework.
 A collection object does not store the physical copies
of other objects; it stores references of other objects.
Collection framework
7

The primary advantages of a collections framework


are that it:
• Reduces programming effort
• Increases performance
• Provides interoperability between unrelated APIs
• Reduces the effort required to learn APIs
• Reduces the effort required to design and implement APIs
• Fosters software reuse
8

The collections framework consists of:

1.Collection
2.List
3.Set
4.SortedSet
5.NavigableSet
6.Queue
7.Map
8.SortedMap
9.NavigableMap

Collection framework
Collection Interfaces: 9
The Collection interface is not directly implemented by any
class. However, it is implemented indirectly via its subtypes or
subinterfaces like List, Queue, and Set. For Example, the HashSet
class implements the Set interface which is a subinterface of the
Collection interface.
• It has most common methods
• Such as:add(),remove(),clear(),empty()
• Root interface of colletion frame work

Collection framework
Method of collection interface:
There are many methods declared in the Collection interface. They are 10
as follows:
Methods Description
public boolean add(E e) It is used to insert an element in this
collection.
public boolean addAll(Collection<? extends E> c) It is used to insert the specified collection
elements in the invoking collection.
public boolean remove(Object element) It is used to delete an element from the
collection.
Public boolean removeAll(collection<?>c) It is used to delete all the elements of the
specified collection from the invoking
collection.
default boolean removeIf(Predicate<? super E> filter) It is used to delete all the elements of the
collection that satisfy the specified predicate.
public boolean retainAll(Collection<?> c) It is used to delete all the elements of
invoking collection except the specified
collection.
public int size() It returns the total number of elements in the
collection.
Collection framework
public void clear() It removes the total number of elements from
11
List
Interface
List interface is the child interface of Collection interface. It inhibits a list type data
structure in which we can store the ordered collection of objects. It can have duplicate
values.
List interface is implemented by the classes
• ArrayList
• LinkedList
• Vector
• Stack

Syntax:
1.List <data-type> list1= new ArrayList();
2.List <data-type> list2 = new LinkedList();
3.List <data-type> list3 = new Vector();
4.List <data-type> list4 = new Stack();
Collection framework
12
t:
•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 the array works
on an index basis.
•In ArrayList, manipulation is a little bit slower than the
LinkedList in Java because a lot of shifting needs to occur if any
element is removed from the array list.
•We can not create an array list of the primitive types, such as
int, float, char, etc. It is required to use the required wrapper
class in such cases

ArrayList<Integer> obj= new ArrayList<Integer>();

Collection
framework
import java.util.*;
public class b {
public static void main(String[]args){
List<Integer> k=new ArrayList<Integer>();
13
k.add(31);
k.add(57);
k.add(30);
System.out.println(k);
k.add(1,25);
System.out.println(k);
ArrayList<Integer> m=new
ArrayList<Integer>();
m.add(2);
m.add(7);
k.addAll(m);
Collections.sort(k);
System.out.println(k);
k.remove(1);
System.out.println(k);
System.out.println(k.size());
System.out.println(k.indexOf(57));
m.removeAll(m);
k.clear();
Presentation title System.out.println(m);
System.out.println(k);}}
LinkedList 14
Java LinkedList class uses a 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.
•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 occur.
•Java LinkedList class can be used as a list, stack or queue.

Collection framework
15

C0llection framework
import java.util.*;
public class add{
public static void main(String[]args){
Scanner s=new Scanner(System.in);
16
LinkedList<String> ll=new
LinkedList<String>(); ll.offerLast("sumanth");
LinkedList<String> vi=new System.out.println(ll);
LinkedList<String>(); ll.poll();
ll.pollFirst();
System.out.println("enter string"); System.out.println(ll.clone());
ll.add(s.nextLine()); ll.push("nithin");
ll.add("mohith"); ll.remove();
System.out.println(ll); ll.remove(3);
vi.add("mani"); ll.removeAll(vi);
ll.addAll(0,vi); ll.set(1,"sai");
System.out.println(vi); ll.pop();
System.out.println(ll); System.out.println(ll);
ll.addFirst("karthik"); }
ll.addLast("vamsi"); }
System.out.println(ll.contains("vamsi"));
System.out.println(ll.element());
System.out.println(ll.get(1));
System.out.println(ll);
Collecton framework
ArrayList LinkedList
1.uses a dynamic array to store the 1.uses a doubly linked list to store the
elements. elements.
17
2.act as a list only because it 2.act as a list and queue
implements List only. because it implements List and queue.

3.The memory location for the 3.The memory location for the
elements of an ArrayList is elements of an LinkedList is not
continuous. continuous.

4. Generally, when an ArrayList is 4. There is no case of default capacity in a


initialized, a default capacity of 10 is LinkedList. In LinkedList, an empty list is
assigned to the ArrayList. created when a LinkedList is initialized.

5. an ArrayList is a resizable array 5. LinkedList implements the doubly


linked list of the list interface.

6. Manipulation with ArrayList 6.Manipulation with LinkedList


is slow because it internally uses an is faster than ArrayList because it uses a
array. If any element is removed from doubly linked list, so no bit shifting is
the array, all the other elements are required in memory.
shifted in memory
Collection framework
18
Vecto
r

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 and implements the List interface, so we can use all
the methods of List interface here.
HTML Tutorial
•Vector is synchronized.
•Java Vector contains many legacy methods that are not the part of a
collections framework.
•Heterogenious are allowed
•Dupilicates are allowed
•Null pointer can de inserted
•Follows Insertion order

Collection
framework
19
Stac
k
The stack is a linear data structure that is used to store the collection of objects. It is based
on Last-In-First-Out (LIFO). Java collection
framework provides many interfaces and classes to store the collection of objects. One of
them is the Stack class that provides different operations such as push, pop, search, etc.

Collection framework
20
import java.util.*;
public class staks{
public static void main(String[]args){
Stack <Integer> s = new Stack<>();
s.push(31);
s.push(34);
s.push(30);
System.out.println(s);
System.out.println(s.search(31));
System.out.println(s.pop());
System.out.println(s.peek());
System.out.println(s.empty());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.empty());
}

Presentation title
21
import java.util.*;
public class staks{
public static void main(String[]args){
Stack stk = new Stack();
stk.push("sai");
stk.push(31);
List l=new ArrayList();
l.add("sai");
l.add(31);
List k=new LinkedList();
k.add(31);
k.add("sai");
System.out.println(stk+""+l+""+k);
}
}

Presentation title
22
What is Hashi
ng?

Hashing is a technique or process of mapping keys,


and values into the hash table by using a hash
function. It is done for faster access to elements. The
efficiency of mapping depends on the efficiency of the
hash function used.
Let a hash function H(x) maps the value x at the
index x%10 in an Array. For example if the list of
values is [11,12,13,14,15] it will be stored at positions
{1,2,3,4,5} in the array or Hash table respectively.
Collection
framework
23
Java
HashSet
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.
 HashSet stores the elements by using a mechanism called hashing.
 HashSet contains unique elements only.
 HashSet allows null value.
 HashSet class is non synchronized.
 HashSet doesn't maintain the insertion order. Here, elements are inserted on the basis of their hashcode.
 HashSet is the best approach for search operations.
 The initial default capacity of HashSet is 16, and the load factor is 0.75.

Collection framework
24
Constructors of Hashset
Class

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

HashSet(int capacity) It is used to initialize the capacity


of the hash set to the given
integer value capacity. The
capacity grows automatically as
elements are added to the
HashSet.
HashSet(Collection<? It is used to initialize the hash set
extends E> c) by using the elements of the
Collection collection c.
framework
25
Methods in hashsets

Method Description
add() It is the element which will be added to this set.
Clear() It is used to return a shallow copy of this HashSet
instance: the elements themselves are not cloned.
Contains() It is used to return true if this set contains the
specified element.
isEmpty() It is used to return true if this set contains no
elements.

iterator() It is used to return an iterator over the elements in


remove(Object It is used to remove the specified element from this
o) set if it is present.
Collection
framework Size() It is used to return the number of elements in the
import java.util.*;
public class sets {
public static void main(String[]args) { 26
HashSet hs=new HashSet();
HashSet s=new HashSet();
hs.add(3);
hs.add("sai");
hs.add("main");
hs.add("karthik");
hs.add("pavan naik");
hs.add("vamsi");
s.add("sai");
s.addAll(hs);

System.out.println(s.containsAll(hs));
System.out.println(hs.isEmpty());
System.out.println(hs.contains("sai"));
s.clear();
System.out.println(s);
System.out.println(hs);
hs.remove("pavan");
System.out.println(hs);}
Collection
framework }
27
LinkedHash
Set
Java LinkedHashSet class is a Hashtable and Linked list implementation of the
Set interface. It inherits the HashSet class and implements the Set interface.

•Java LinkedHashSet class contains unique elements only like HashSet.

•Java LinkedHashSet class provides all optional set operations and


permits null elements.

•Java LinkedHashSet class is non-synchronized.

•Java LinkedHashSet class maintains insertion order.

• the declaration for java.util.LinkedHashSet class.


Collection framework
28
Constructors of Java LinkedHashSet
Class

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.
LinkedHashSet(int capacity) It is used to initialize the
capacity of the linked hash set
to the given integer value
capacity.
LinkedHashSet(int capacity, float It is used to initialize both the
fillRatio) capacity and the fill ratio (also
called load capacity) of the hash
Collection framework set from its argument.
29
Java
TreeSet
Java TreeSet class implements the Set interface that uses a tree for storage. It
inherits AbstractSet class and implements the NavigableSet interface. The
objects of the TreeSet class are stored in ascending order.
•Java TreeSet class contains unique elements only like HashSet.
•Java TreeSet class access and retrieval times are quiet fast.
•Java TreeSet class doesn't allow null element.
•Java TreeSet class is non synchronized.
•Java TreeSet class maintains ascending order.
•The TreeSet can only allow those generic types that are comparable.
•For example The Comparable interface is being implemented by the StringBuffer class.
•Tree sets must be of comparable type if not it shows classcastexception at run time.
•Tree set is of fail-fast nature.means if treeset is modified after the creation of iterator object,you will
get ConcurrentModificationExceotion

Collection
Constructors of Java 30

TreeSet Class

Constructor Description

TreeSet() It is used to construct an empty tree


set that will be sorted in ascending
order according to the natural order
of the tree set.
TreeSet(Collection<? extends E> c) It is used to build a new tree set that
contains the elements of the
collection c.
TreeSet(Comparator<? super E> It is used to construct an empty tree
comparator) set that will be sorted according to
given comparator.
TreeSet(SortedSet<E> s) It is used to build a TreeSet that
contains the elements of the given
Collection
framework
SortedSet.
31
Queues

The Queue interface is present in java.util package and extends the


Collection interface is used to hold the elements about to be processed in FIFO(First
In First Out) order. It is an ordered list of objects with its use limited to inserting
elements at the end of the list and deleting elements from the start of the list, (i.e.),
it follows the FIFO or the First-In-First-Out principle.
Queue interface can be instantiated as:
1.Queue<String> q1 = new PriorityQueue();
2.Queue<String> q2 = new ArrayDeque();

There are various classes that implement the Queue interface, some of them are
given below.
 PriorityQueue
 ArrayDeque

Collection framework
32
PriorityQue
ue
o The PriorityQueue class implements the Queue interface. It holds the elements or
objects which are to be processed by their priorities. PriorityQueue doesn't allow
null values to be stored in the queue.
o Not allow null values
o Do not have insertion order
o Insertion performed at the end of queues
o descending order
o Order for deletion of elements from a queue.

Collection
framework
33
ArrayDeque
class

We know that it is not possible to create an object of an interface in Java.


Therefore, for instantiation, we need a class that implements the Deque
interface, and that class is ArrayDeque. It grows and shrinks as per
usage. It also inherits the AbstractCollection class.
The important points about ArrayDeque class are:
•Unlike Queue, we can add or remove elements from both sides.
•Null elements are not allowed in the ArrayDeque.
•ArrayDeque is not thread safe, in the absence of external
synchronization.
•ArrayDeque has no capacity restrictions.
•ArrayDeque is faster than LinkedList and Stack.
•declaration
Collection
framework for java.util.ArrayDeque class.
34
Program of deques

Collection
framework

You might also like