0% found this document useful (0 votes)
8 views40 pages

Java Collections Framework Overview

The document provides an overview of the Java Collections Framework, detailing its purpose, structure, and various interfaces such as List, Set, Queue, and Map. It explains the advantages of using the framework, including reduced programming effort and increased performance, and describes specific classes like ArrayList, LinkedList, HashSet, and TreeSet. Additionally, it covers methods associated with these collections and highlights differences between them, such as synchronization and element order maintenance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views40 pages

Java Collections Framework Overview

The document provides an overview of the Java Collections Framework, detailing its purpose, structure, and various interfaces such as List, Set, Queue, and Map. It explains the advantages of using the framework, including reduced programming effort and increased performance, and describes specific classes like ArrayList, LinkedList, HashSet, and TreeSet. Additionally, it covers methods associated with these collections and highlights differences between them, such as synchronization and element order maintenance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

-JAVA

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

3
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 Array List, 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
4

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 [Link] (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
5

The primary advantages of a collections framework


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

The collections framework consists of:

[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]

Collection framework
Collection Interfaces: 8
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 collection frame work

Collection framework
Method of collection interface:
There are many methods declared in the Collection interface. They are 9
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
10
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
• Array List
• LinkedList
• Vector
• Stack

Syntax:
[Link] <data-type> list1= new Array List();
[Link] <data-type> list2 = new LinkedList();
[Link] <data-type> list3 = new Vector();
[Link] <data-type> list4 = new Stack();
Collection framework
11
Array
List
•Java Array List class can contain duplicate elements.
•Java Array List class maintains insertion order.
•Java Array List class is non synchronized.
•Java Array List allows random access because the array works
on an index basis.
•In Array List, 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

Array List<Integer> obj= new Array List<Integer>();

Collection framework
import [Link].*;
public class b {
public static void main(String[]args){
List<Integer> k=new Array List<Integer>();
12
[Link](31);
[Link](57);
[Link](30);
[Link](k);
[Link](1,25);
[Link](k);
Array List<Integer> m=new Array
List<Integer>();
[Link](2);
[Link](7);
[Link](m);
[Link](k);
[Link](k);
[Link](1);
[Link](k);
[Link]([Link]());
[Link]([Link](57));
[Link](m);
[Link]();
Presentation title [Link](m);
[Link](k);}}
LinkedList 13
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
14

C0llection framework
import [Link].*;
public class add{
public static void main(String[]args){
Scanner s=new Scanner([Link]);
15
LinkedList<String> ll=new
LinkedList<String>(); [Link]("sumanth");
LinkedList<String> vi=new [Link](ll);
LinkedList<String>(); [Link]();
[Link]();
[Link]("enter string"); [Link]([Link]());
[Link]([Link]()); [Link]("nithin");
[Link]("mohith"); [Link]();
[Link](ll); [Link](3);
[Link]("mani"); [Link](vi);
[Link](0,vi); [Link](1,"sai");
[Link](vi); [Link]();
[Link](ll); [Link](ll);
[Link]("karthik"); }
[Link]("vamsi"); }
[Link]([Link]("vamsi"));
[Link]([Link]());
[Link]([Link](1));
[Link](ll);
Collecton framework
Array List LinkedList
[Link] a dynamic array to store the [Link] a doubly linked list to store the
elements. elements.
16
[Link] as a list only because it [Link] as a list and queue
implements List only. because it implements List and queue.

[Link] memory location for the [Link] memory location for the
elements of an Array List is elements of an LinkedList is not
continuous. continuous.

4. Generally, when an Array List 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 Array List. created when a LinkedList is initialized.

5. an Array List is a resizable array 5. LinkedList implements the doubly


linked list of the list interface.

6. Manipulation with Array List [Link] with LinkedList


is slow because it internally uses an is faster than Array List 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
17
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 [Link] package and implements the List interface, so we can use all
•the
Vector is synchronized.
methods of List interface here.
•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
•Cloneable
•Random access
•Thread safe
Collection
framework
18
SN Constructor Description
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(intinitialCapacity,int It constructs an empty vector with the


capacityIncrement) specified initial capacity and capacity
increment.
4) Vector( Collection<? extends It constructs a vector that contains the
E> c) elements of a collection c.

Presentation title
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).
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
Stack
program
import [Link].*;
public class staks{
public static void main(String[]args){
Stack <Integer> s = new Stack<>();
[Link](31);
[Link](34);
[Link](30);
[Link](s);
[Link]([Link](31));
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
Collection
framework }
21
List
program
import [Link].*;
public class staks{
public static void main(String[]args){
Stack stk = new Stack();
[Link]("sai");
[Link](31);
List l=new Array List();
[Link]("sai");
[Link](31);
List k=new LinkedList();
[Link](31);
[Link]("sai");
[Link](stk+""+l+""+k);
}
}
Collection
framework
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 [Link].*;
public class sets {
public static void main(String[]args) { 26
HashSet hs=new HashSet();
HashSet s=new HashSet();
[Link](3);
[Link]("sai");
[Link]("main");
[Link]("karthik");
[Link]("pavan naik");
[Link]("vamsi");
[Link]("sai");
[Link](hs);

[Link]([Link](hs));
[Link]([Link]());
[Link]([Link]("sai"));
[Link]();
[Link](s);
[Link](hs);
[Link]("pavan");
[Link](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 [Link] 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.
import [Link].*;
public class sets {
public static void main(String[]args) { 29
LinkedHashSet hs=new LinkedHashSet();
HashSet s=new HashSet();
[Link](3);
[Link]("sai");
[Link]("main");
[Link]("karthik");
[Link]("pavan naik");
[Link]("vamsi");
[Link]("sai");
[Link](hs);

[Link]([Link](hs));
[Link]([Link]());
[Link]([Link]("sai"));
[Link]();
[Link](s);
[Link](hs);
[Link]("pavan");
[Link](hs);}

Presentation title }
30
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 [Link] if treeset is modified after the creation of iterator object,you will
get ConcurrentModificationExceotion

Collection
Constructors of Java 31

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.
32
Queues

The Queue interface is present in [Link] 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:
[Link]<String> q1 = new PriorityQueue();
[Link]<String> q2 = new ArrayDeque();

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

Collection framework
33
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
import [Link].*;
class queues{ 34
public static void main(String args[]){
PriorityQueue<String> queue=new PriorityQueue
<String>();
[Link](“vamsi");
[Link](“karthik");
[Link](“mohith");
[Link](“mani");
[Link](“pavan");
[Link]("head:"+[Link]());
[Link]("head:"+[Link]());
[Link]("iterating the queue elements:"
);
Iterator itr=[Link]();
while([Link]()){
[Link]([Link]());
}
[Link]();
[Link]();
[Link]("after removing two elements:")
;
Presentation title
Iterator<String> itr2=[Link]();
35
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 [Link] class.
36
Program of deques

import [Link].*;
public class dqueue {
public static void main(String[]args){
Deque<String> d = new
ArrayDeque<String>();
[Link]("sai");
[Link]("sai2");
//[Link]();
//[Link]();
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
[Link]([Link]("sai"));
[Link]([Link]("sai2"));
Collection
framework }}
37
Java 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. A Map
contains
Map unique
doesn't allow keys.
duplicate keys, but you can
have duplicate values. HashMap and
LinkedHashMap allow null keys and values,
but
doesn't allow any null key or value
• It has its own methods
• Hash maps
• Linked hash maps
• Tree maps
Collection
framework
38
Set Interface

Collection
framework
39
Hash map

Java HashMap class implements the Map interface which allows us to


store key and value pair, where keys should be unique. If you try to
insert the duplicate key, it will replace the element of the
corresponding key. It is easy to perform operations using the key index
like updation, deletion, etc. HashMap class is found in
the [Link] package.
•Java HashMap contains values based on the key.
•Java HashMap contains only unique keys.
•Java HashMap may have one null key and multiple null values.
•Java HashMap is non synchronized.
•Java HashMap maintains no order.
Collection framework
import [Link].*; 40
public class Map {
public static void main(String[]args) {
HashMap<Integer,String> m=new
HashMap<Integer,String>();
[Link](31,"sai");
[Link](57,"mani");
[Link](30, "vitu");
[Link](31, "gouse");
[Link](m);
HashMap<Integer,String> l=new
HashMap<Integer,String>(); [Link](32);
[Link](3,"sai"); [Link](m);
[Link](2,"mani"); [Link](31,"gouse");
[Link](1, "pavan"); [Link](m);
[Link](l); [Link]([Link](31));
[Link](m); [Link]([Link]("sai"));
[Link](32,"karthik"); [Link]([Link](3));
[Link](m); }
}
Collection
frameworks

You might also like