0% found this document useful (0 votes)
22 views88 pages

CSE Module 4

Uploaded by

motoh6220
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views88 pages

CSE Module 4

Uploaded by

motoh6220
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 88

Collections

Dr. S Sudheer Mangalampalli


Assistant Professor, Senior Grade1
School of Computer Science and Engineering
VIT-AP University

June 14, 2022

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
JuneVIT-AP
14, 2022University
1 / 88
Intro about Collections framework

The Collection in Java is a framework that provides an architecture to


store and manipulate the group of objects.
Java Collections can achieve all the operations that you perform on a
data such as searching, sorting, insertion, manipulation, and deletion.
The Java collections framework provides a set of interfaces and
classes to implement various data structures and algorithms.
Java Collection InterfaceThe Collection interface is the root
interface of the collections framework hierarchy.
Java does not provide direct implementations of the Collection
interface but provides implementations of its subinterfaces like List,
Set, and Queue.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
JuneVIT-AP
14, 2022University
2 / 88
Java Collection framework hierarchy

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
JuneVIT-AP
14, 2022University
3 / 88
Why the Collections Framework

The Java collections framework provides various data structures and


algorithms that can be used directly.
We do not have to write code to implement these data structures and
algorithms manually.
Our code will be much more efficient as the collections framework is
highly optimized.
Moreover, the collections framework allows us to use a specific data
structure for a particular type of data. Here are a few examples
If we want our data to be unique, then we can use the Set interface
provided by the collections framework.
To store data in key/value pairs, we can use the Map interface.
The ArrayList class provides the functionality of resizable arrays.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
JuneVIT-AP
14, 2022University
4 / 88
Collections Framework Vs. Collection Interface

People often get confused between the collections framework and


Collection Interface.
The Collection interface is the root interface of the collections
framework.
These interfaces also may have sub interfaces
the Collection interface includes subinterfaces that are implemented
by Java classes.
All the methods of the Collection interface are also present in its
subinterfaces.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
JuneVIT-AP
14, 2022University
5 / 88
Subinterfaces of Collection Interface

List Interface- The List interface is an ordered collection that allows


us to add and remove elements like an array.
Set Interface-The Set interface allows us to store elements in
different sets similar to the set in mathematics. It cannot have
duplicate elements.
Queue Interface-The Queue interface is used when we want to store
and access elements in First In, First Out manner.
Java Map Interface-In Java, the Map interface allows elements to
be stored in key/value pairs. Keys are unique names that can be used
to access a particular element in a map. And, each key has a single
value associated with it.
Java Iterator Interface-In Java, the Iterator interface provides
methods that can be used to access elements of collections.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
JuneVIT-AP
14, 2022University
6 / 88
Java List

In Java, the List interface is an ordered collection that allows us to store


and access elements sequentially. It extends the Collection interface.
Classes that implement List-Since List is an interface, we cannot create
objects from it.
In order to use functionalities of the List interface, we can use these classes

ArrayList
LinkedList
Vector
Stack

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
JuneVIT-AP
14, 2022University
7 / 88
How to use List

In Java, we must import java.util.List package in order to use List.


// A r r a y L i s t i m p l e m e n t a t i o n o f L i s t
L i s t <S t r i n g > l i s t 1 = new A r r a y L i s t < >();
// L i n k e d L i s t i m p l e m e n t a t i o n o f L i s t
L i s t <S t r i n g > l i s t 2 = new L i n k e d L i s t < >();
Here, we have created objects list1 and list2 of classes ArrayList and LinkedList.
These objects can use the functionalities of the List interface.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
JuneVIT-AP
14, 2022University
8 / 88
Methods of List

The List interface includes all the methods of the Collection interface. Its
because Collection is a super interface of List.
Some of the commonly used methods of the Collection interface that’s also
available in the List interface are:
add() - adds an element to a list
addAll() - adds all elements of one list to another
get() - helps to randomly access elements from lists
iterator() - returns iterator object that can be used to sequentially
access elements of lists
set() - changes elements of lists
remove() - removes an element from the list

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
JuneVIT-AP
14, 2022University
9 / 88
Methods of List

removeAll() - removes all the elements from the list


clear() - removes all the elements from the list (more efficient than
removeAll())
size() - returns the length of lists
toArray() - converts a list into an array
contains() - returns true if a list contains specified element

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
10 / 88
Implementing ArrayList class

import java . u t i l . L i s t ;
import java . u t i l . A r r a y L i s t ;
c l a s s Main3 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
L i s t <I n t e g e r > numbers = new A r r a y L i s t < >();
numbers . add ( 1 ) ;
numbers . add ( 2 ) ;
numbers . add ( 3 ) ;
System . o u t . p r i n t l n ( ” L i s t : ” + numbers ) ;
i n t number = numbers . g e t ( 2 ) ;
System . o u t . p r i n t l n ( ” A c c e s s e d E l e m e n t : ” + number ) ;
i n t removedNumber = numbers . remove ( 1 ) ;
System . o u t . p r i n t l n ( ” Removed E l e m e n t : ” + removedNumber
}}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
11 / 88
Implementing LinkedList Class
import java . u t i l . L i s t ;
import java . u t i l . Link edList ;
c l a s s Main4 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
L i s t <I n t e g e r > numbers = new L i n k e d L i s t < >();
numbers . add ( 1 ) ;
numbers . add ( 2 ) ;
numbers . add ( 3 ) ;
System . o u t . p r i n t l n ( ” L i s t : ” + numbers ) ;
i n t number = numbers . g e t ( 2 ) ;
System . o u t . p r i n t l n ( ” A c c e s s e d E l e m e n t : ” + number ) ;
i n t i n d e x = numbers . i n d e x O f ( 2 ) ;
System . o u t . p r i n t l n ( ” P o s i t i o n o f 3 i s ” + i n d e x ) ;
i n t removedNumber = numbers . remove ( 1 ) ;
System . o u t . p r i n t l n ( ” Removed E l e m e n t : ” + removedNumber
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
12 / 88
Java ArrayList Vs Array

In Java, we need to declare the size of an array before we can use it.
Once the size of an array is declared, it’s hard to change it.
To handle this issue, we can use the ArrayList class. It allows us to
create resizable arrays.
Unlike arrays, arraylists can automatically adjust their capacity when
we add or remove elements from them. Hence, arraylists are also
known as dynamic arrays.
Creating an ArrayList Before using ArrayList, we need to import the
java.util.ArrayList package first. Here is how we can create arraylists in
Java:
A r r a y L i s t <Type> a r r a y L i s t= new A r r a y L i s t < >();
Here, Type indicates the type of an arraylist.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
13 / 88
// c r e a t e I n t e g e r t y p e a r r a y l i s t
A r r a y L i s t <I n t e g e r > a r r a y L i s t = new A r r a y L i s t < >();

// c r e a t e S t r i n g t y p e a r r a y l i s t
A r r a y L i s t <S t r i n g > a r r a y L i s t = new A r r a y L i s t < >();
In the above program, we have used Integer not int. It is because we cannot
use primitive types while creating an arraylist. Instead, we have to use the
corresponding wrapper classes.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
14 / 88
ArrayList Example

import java . u t i l . A r r a y L i s t ;

c l a s s Array1 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {

// c r e a t e A r r a y L i s t
A r r a y L i s t <S t r i n g > l a n g u a g e s = new A r r a y L i s t < >();

// Add e l e m e n t s t o A r r a y L i s t
l a n g u a g e s . add ( ” J a v a ” ) ;
l a n g u a g e s . add ( ” Python ” ) ;
l a n g u a g e s . add ( ” S w i f t ” ) ;
System . o u t . p r i n t l n ( ” A r r a y L i s t : ” + l a n g u a g e s ) ;
}
}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
15 / 88
Basic operations on ArrayList

The ArrayList class provides various methods to perform different operations


on arraylists.
Add elements
Access elements
Change elements
Remove elements

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
16 / 88
Adding elements to ArrayList

import java . u t i l . A r r a y L i s t ;

c l a s s Array2 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
// c r e a t e A r r a y L i s t
A r r a y L i s t <S t r i n g > l a n g u a g e s = new A r r a y L i s t < >();

// add ( ) method w i t h o u t t h e i n d e x p a r a m e t e r
l a n g u a g e s . add ( ” J a v a ” ) ;
l a n g u a g e s . add ( ”C ” ) ;
l a n g u a g e s . add ( ” Python ” ) ;
System . o u t . p r i n t l n ( ” A r r a y L i s t : ” + l a n g u a g e s ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
17 / 88
Access ArrayList Elements
To access an element from the arraylist, we use the get() method of the
ArrayList class.
import java . u t i l . A r r a y L i s t ;
c l a s s Array3 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
A r r a y L i s t <S t r i n g > a n i m a l s = new A r r a y L i s t < >();
// add e l e m e n t s i n t h e a r r a y l i s t
a n i m a l s . add ( ” Cat ” ) ;
a n i m a l s . add ( ” Dog ” ) ;
a n i m a l s . add ( ” Cow ” ) ;
System . o u t . p r i n t l n ( ” A r r a y L i s t : ” + a n i m a l s ) ;
// g e t t h e e l e m e n t from t h e a r r a y l i s t
String s t r = animals . get ( 1 ) ;
System . o u t . p r i n t ( ” E l e m e n t a t i n d e x 1 : ” + s t r ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
18 / 88
Change ArrayList Elements
To change elements of the arraylist, we use the set() method of the ArrayList
class.
import java . u t i l . A r r a y L i s t ;
c l a s s Array4 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
A r r a y L i s t <S t r i n g > l a n g u a g e s = new A r r a y L i s t < >();
// add e l e m e n t s i n t h e a r r a y l i s t
l a n g u a g e s . add ( ” J a v a ” ) ;
l a n g u a g e s . add ( ” K o t l i n ” ) ;
l a n g u a g e s . add ( ”C++”);
System . o u t . p r i n t l n ( ” A r r a y L i s t : ” + l a n g u a g e s ) ;
// c ha nge t h e e l e m e n t o f t h e a r r a y l i s t
languages . set (2 , ” JavaScript ” );
System . o u t . p r i n t l n ( ” M o d i f i e d A r r a y L i s t : ” + l a n g u a g e s
}
}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
19 / 88
Remove ArrayList Elements
To remove an element from the arraylist, we can use the remove() method
of the ArrayList class.
import java . u t i l . A r r a y L i s t ;
c l a s s Array5 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
A r r a y L i s t <S t r i n g > a n i m a l s = new A r r a y L i s t < >();
// add e l e m e n t s i n t h e a r r a y l i s t
a n i m a l s . add ( ” Dog ” ) ;
a n i m a l s . add ( ” Cat ” ) ;
a n i m a l s . add ( ” H o r s e ” ) ;
System . o u t . p r i n t l n ( ” A r r a y L i s t : ” + a n i m a l s ) ;
// remove e l e m e n t from i n d e x 2
S t r i n g s t r = a n i m a l s . remove ( 2 ) ;
System . o u t . p r i n t l n ( ” Updated A r r a y L i s t : ” + a n i m a l s ) ;
System . o u t . p r i n t l n ( ” Removed E l e m e n t : ” + s t r ) ;
}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
20 / 88
Iterate through an ArrayList
We can use the Java for-each loop to loop through each element of the
arraylist.
import java . u t i l . A r r a y L i s t ;
class Iterate {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
A r r a y L i s t <S t r i n g > a n i m a l s = new A r r a y L i s t < >();
a n i m a l s . add ( ” Cow ” ) ;
a n i m a l s . add ( ” Cat ” ) ;
a n i m a l s . add ( ” Dog ” ) ;
System . o u t . p r i n t l n ( ” A r r a y L i s t : ” + a n i m a l s ) ;
System . o u t . p r i n t l n ( ” A c c e s s i n g i n d i v i d u a l e l e m e n t s : ” ) ;
f o r ( String language : animals ) {
System . o u t . p r i n t ( l a n g u a g e ) ;
System . o u t . p r i n t l n ( ” , ” ) ;
}}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
21 / 88
Java Vector

The Vector class is an implementation of the List interface that allows us


to create resizable-arrays similar to the ArrayList class.
Java Vectors vs ArrayList
In Java, both ArrayList and Vector implements the List interface and
provides the same functionalities. However, there exist some
differences between them. The Vector class synchronizes each
individual operation.
This means whenever we want to perform some operation on vectors,
the Vector class automatically applies a lock to that operation.
It is because when one thread is accessing a vector, and at the same
time another thread tries to access it, an exception called
ConcurrentModificationException is generated. ‘
Hence, this continuous use of lock for each operation makes vectors
less efficient.However, in array lists, methods are not synchronized.
Instead, it uses the Collections.synchronizedList() method that
synchronizes the list as a whole.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
22 / 88
java Vector

Creating a Vector
V e c t o r <Type> v e c t o r = new V e c t o r < >();
Here, Type indicates the type of a linked list.
// c r e a t e I n t e g e r t y p e l i n k e d l i s t
V e c t o r <I n t e g e r > v e c t o r= new V e c t o r < >();

// c r e a t e S t r i n g t y p e l i n k e d l i s t
V e c t o r <S t r i n g > v e c t o r= new V e c t o r < >();

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
23 / 88
Add Elements to Vector

1 add(element) - adds an element to vectors


2 add(index, element) - adds an element to the specified position
3 addAll(vector) - adds all elements of a vector to another vector

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
24 / 88
Add elements to Vector

import java . u t i l . Vector ;


class vector1 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
V e c t o r <S t r i n g > mammals= new V e c t o r < >();
mammals . add ( ” Dog ” ) ;
mammals . add ( ” H o r s e ” ) ;
mammals . add ( 2 , ” Cat ” ) ;
System . o u t . p r i n t l n ( ” V e c t o r : ” + mammals ) ;
V e c t o r <S t r i n g > a n i m a l s = new V e c t o r < >();
a n i m a l s . add ( ” C r o c o d i l e ” ) ;
a n i m a l s . a d d A l l ( mammals ) ;
System . o u t . p r i n t l n ( ” New V e c t o r : ” + a n i m a l s ) ;
}}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
25 / 88
Access Vector elements

1 get(index) - returns an element specified by the index


2 iterator() - returns an iterator object to sequentially access vector
elements

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
26 / 88
Access Vector elements
import java . u t i l . I t e r a t o r ;
import java . u t i l . Vector ;
class vector2 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
V e c t o r <S t r i n g > a n i m a l s= new V e c t o r < >();
a n i m a l s . add ( ” Dog ” ) ;
a n i m a l s . add ( ” H o r s e ” ) ;
a n i m a l s . add ( ” Cat ” ) ;
S t r i n g element = animals . get ( 2 ) ;
System . o u t . p r i n t l n ( ” E l e m e n t a t i n d e x 2 : ” + e l e m e n t ) ;
I t e r a t o r <S t r i n g > i t e r a t e = a n i m a l s . i t e r a t o r ( ) ;
System . o u t . p r i n t ( ” V e c t o r : ” ) ;
w h i l e ( i t e r a t e . hasNext ( ) ) {
System . o u t . p r i n t ( i t e r a t e . n e x t ( ) ) ;
System . o u t . p r i n t ( ” , ” ) ;
}}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
27 / 88
Remove Vector elements

1 remove(index) - removes an element from specified position


2 removeAll() - removes all the elements
3 clear() - removes all elements. It is more efficient than removeAll()

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
28 / 88
Remove Vector elements

import java . u t i l . Vector ;


class vector3 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
V e c t o r <S t r i n g > a n i m a l s= new V e c t o r < >();
a n i m a l s . add ( ” Dog ” ) ;
a n i m a l s . add ( ” H o r s e ” ) ;
a n i m a l s . add ( ” Cat ” ) ;
System . o u t . p r i n t l n ( ” I n i t i a l V e c t o r : ” + a n i m a l s ) ;
S t r i n g e l e m e n t = a n i m a l s . remove ( 1 ) ;
System . o u t . p r i n t l n ( ” Removed E l e m e n t : ” + e l e m e n t ) ;
System . o u t . p r i n t l n ( ” New V e c t o r : ” + a n i m a l s ) ;
animals . c l e a r ( ) ;
System . o u t . p r i n t l n ( ” V e c t o r a f t e r c l e a r ( ) : ” + a n i m a l s )
}}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
29 / 88
Other Vector methods

1 set()-changes an element of the vector


2 size()–returns the size of the vector
3 toArray()-converts the vector into an array
4 toString()-converts the vector into a String
5 contains()-searches the vector for specified element and returns a
boolean result

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
30 / 88
Java Set Interface

The Set interface of the Java Collections framework provides the


features of the mathematical set in Java. It extends the Collection
interface.
Unlike the List interface, sets cannot contain duplicate elements.
Classes that implement Set Since Set is an interface, we cannot create
objects from it.
HashSet
LinkedHashSet
EnumSet
TreeSet

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
31 / 88
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
32 / 88
How to use set?
In Java, we must import java.util.Set package in order to use Set.
// S e t i m p l e m e n t a t i o n u s i n g HashSet
Set<S t r i n g > a n i m a l s = new HashSet < >();
Methods of Set The Set interface includes all the methods of the Collection
interface. It’s because Collection is a super interface of Set.
add() - adds the specified element to the set
addAll() - adds all the elements of the specified collection to the set
iterator() - returns an iterator that can be used to access elements of
the set sequentially
remove() - removes the specified element from the set
removeAll() - removes all the elements from the set that is present in
another specified set
retainAll() - retains all the elements in the set that are also present in
another specified set
clear() - removes all the elements from the set
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
33 / 88
size() - returns the length (number of elements) of the set
toArray() - returns an array containing all the elements of the set
contains() - returns true if the set contains the specified element
containsAll() - returns true if the set contains all the elements of the
specified collection
hashCode() - returns a hash code value (address of the element in the
set)
Set Operations
The Java Set interface allows us to perform basic mathematical set opera-
tions like union, intersection, and subset.
Union - to get the union of two sets x and y, we can use x.addAll(y)
Intersection - to get the intersection of two sets x and y, we can use
x.retainAll(y)
Subset - to check if x is a subset of y, we can use y.containsAll(x)

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
34 / 88
Implementation of the Set Interface
Implementing HashSet Class
import j a v a . u t i l . Set ;
i m p o r t j a v a . u t i l . HashSet ;
c l a s s Hash1 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Set<I n t e g e r > s e t 1 = new HashSet < >();
s e t 1 . add ( 2 ) ;
s e t 1 . add ( 3 ) ;
System . o u t . p r i n t l n ( ” S e t 1 : ” + s e t 1 ) ;
Set<I n t e g e r > s e t 2 = new HashSet < >();
s e t 2 . add ( 1 ) ;
s e t 2 . add ( 2 ) ;
System . o u t . p r i n t l n ( ” S e t 2 : ” + s e t 2 ) ;
set2 . addAll ( set1 ) ;
System . o u t . p r i n t l n ( ” Union i s : ” + s e t 2 ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
35 / 88
Implementation of Set Interface
Implementing TreeSet class
import j a v a . u t i l . Set ;
import java . u t i l . TreeSet ;
import java . u t i l . I t e r a t o r ;
c l a s s Treeset1 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Set<I n t e g e r > numbers = new T r e e S e t < >();
numbers . add ( 2 ) ;
numbers . add ( 3 ) ;
numbers . add ( 1 ) ;
System . o u t . p r i n t l n ( ” S e t u s i n g T r e e S e t : ” + numbers ) ;
System . o u t . p r i n t ( ” A c c e s s i n g e l e m e n t s u s i n g i t e r a t o r ( )
I t e r a t o r <I n t e g e r > i t e r a t e = numbers . i t e r a t o r ( ) ;
w h i l e ( i t e r a t e . hasNext ( ) ) {
System . o u t . p r i n t ( i t e r a t e . n e x t ( ) ) ;
System . o u t . p r i n t ( ” , ” ) ;
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
36 / 88
Java HashSet Class

The HashSet class of the Java Collections framework provides the function-
alities of the hash table data structure.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
37 / 88
Creating a HashSet

In order to create a hash set, we must import the java.util.HashSet package


first.Once we import the package, here is how we can create hash sets in
Java.
// HashSet w i t h 8 c a p a c i t y and 0 . 7 5 l o a d f a c t o r
HashSet<I n t e g e r > numbers = new HashSet <>(8, 0 . 7 5 ) ;
Here, we have created a hash set named numbers.
Notice, the part new HashSet¡¿(8, 0.75). Here, the first parameter is ca-
pacity, and the second parameter is loadFactor.
capacity - The capacity of this hash set is 8. Meaning, it can store 8
elements.
loadFactor - The load factor of this hash set is 0.6. This means,
whenever our hash set is filled by 60%, the elements are moved to a
new hash table of double the size of the original hash table.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
38 / 88
Default capacity and load factor

It’s possible to create a hash table without defining its capacity and load
factor. For example,
// HashSet w i t h d e f a u l t c a p a c i t y and l o a d f a c t o r
HashSet<I n t e g e r > numbers1 = new HashSet < >();
By default,
the capacity of the hash set will be 16
the load factor will be 0.75
Methods of HashSetThe HashSet class provides various methods that
allow us to perform various operations on the set.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
39 / 88
Insert Elements to HashSet
add() - inserts the specified element to the set
addAll() - inserts all the elements of the specified collection to the set
i m p o r t j a v a . u t i l . HashSet ;
c l a s s Hash2 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashSet<I n t e g e r > evenNumber = new HashSet < >();
evenNumber . add ( 2 ) ;
evenNumber . add ( 4 ) ;
evenNumber . add ( 6 ) ;
System . o u t . p r i n t l n ( ” HashSet : ” + evenNumber ) ;
HashSet<I n t e g e r > numbers = new HashSet < >();
numbers . a d d A l l ( evenNumber ) ;
numbers . add ( 5 ) ;
System . o u t . p r i n t l n ( ” New HashSet : ” + numbers ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
40 / 88
Access HashSet Elements
To access the elements of a hash set, we can use the iterator() method. In
order to use this method, we must import the java.util.Iterator package
i m p o r t j a v a . u t i l . HashSet ;
import java . u t i l . I t e r a t o r ;
c l a s s Hash4 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashSet<I n t e g e r > numbers = new HashSet < >();
numbers . add ( 2 ) ;
numbers . add ( 5 ) ;
numbers . add ( 6 ) ;
System . o u t . p r i n t l n ( ” HashSet : ” + numbers ) ;
I t e r a t o r <I n t e g e r > i t e r a t e = numbers . i t e r a t o r ( ) ;
System . o u t . p r i n t ( ” HashSet u s i n g I t e r a t o r : ” ) ;
w h i l e ( i t e r a t e . hasNext ( ) ) {
System . o u t . p r i n t ( i t e r a t e . n e x t ( ) ) ;
System . o u t . p r i n t ( ” , ” ) ;
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
41 / 88
Remove Elements

i m p o r t j a v a . u t i l . HashSet ;
c l a s s Hash6 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashSet<I n t e g e r > numbers = new HashSet < >();
numbers . add ( 2 ) ;
numbers . add ( 5 ) ;
numbers . add ( 6 ) ;
System . o u t . p r i n t l n ( ” HashSet : ” + numbers ) ;
b o o l e a n v a l u e 1 = numbers . remove ( 5 ) ;
System . o u t . p r i n t l n ( ” I s 5 removed ? ” + v a l u e 1 ) ;
b o o l e a n v a l u e 2 = numbers . r e m o v e A l l ( numbers ) ;
System . o u t . p r i n t l n ( ” Are a l l e l e m e n t s removed ? ” +v a l u e
}}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
42 / 88
Union of Sets
To perform the union between two sets, we can use the addAll() method.
i m p o r t j a v a . u t i l . HashSet ;
c l a s s Hash7 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashSet<I n t e g e r > evenNumbers = new HashSet < >();
evenNumbers . add ( 2 ) ;
evenNumbers . add ( 4 ) ;
System . o u t . p r i n t l n ( ” HashSet1 : ” + evenNumbers ) ;
HashSet<I n t e g e r > numbers = new HashSet < >();
numbers . add ( 1 ) ;
numbers . add ( 3 ) ;
System . o u t . p r i n t l n ( ” HashSet2 : ” + numbers ) ;
// Union o f two s e t
numbers . a d d A l l ( evenNumbers ) ;
System . o u t . p r i n t l n ( ” Union i s : ” + numbers ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
43 / 88
Intersection of Sets
To perform the intersection between two sets, we can use the retainAll()
method.
i m p o r t j a v a . u t i l . HashSet ;
c l a s s Hash8 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashSet<I n t e g e r > primeNumbers = new HashSet < >();
primeNumbers . add ( 2 ) ;
primeNumbers . add ( 3 ) ;
System . o u t . p r i n t l n ( ” HashSet1 : ” + primeNumbers ) ;
HashSet<I n t e g e r > evenNumbers = new HashSet < >();
evenNumbers . add ( 2 ) ;
evenNumbers . add ( 4 ) ;
System . o u t . p r i n t l n ( ” HashSet2 : ” + evenNumbers ) ;
evenNumbers . r e t a i n A l l ( primeNumbers ) ;
System . o u t . p r i n t l n ( ” I n t e r s e c t i o n i s : ” + evenNumbers ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
44 / 88
Difference of Sets
To calculate the difference between the two sets, we can use the removeAll()
i m p o r t j a v a . u t i l . HashSet ;
c l a s s Hash9 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashSet<I n t e g e r > primeNumbers = new HashSet < >();
primeNumbers . add ( 2 ) ;
primeNumbers . add ( 3 ) ;
primeNumbers . add ( 5 ) ;
System . o u t . p r i n t l n ( ” HashSet1 : ” + primeNumbers ) ;
HashSet<I n t e g e r > oddNumbers = new HashSet < >();
oddNumbers . add ( 1 ) ;
oddNumbers . add ( 3 ) ;
oddNumbers . add ( 5 ) ;
System . o u t . p r i n t l n ( ” HashSet2 : ” + oddNumbers ) ;
primeNumbers . r e m o v e A l l ( oddNumbers ) ;
System . o u t . p r i n t l n ( ” D i f f e r e n c e : ” + primeNumbers ) ;
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
45 / 88
Subset
i m p o r t j a v a . u t i l . HashSet ;
c l a s s Hash10 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashSet<I n t e g e r > numbers = new HashSet < >();
numbers . add ( 1 ) ;
numbers . add ( 2 ) ;
numbers . add ( 3 ) ;
numbers . add ( 4 ) ;
System . o u t . p r i n t l n ( ” HashSet1 : ” + numbers ) ;
HashSet<I n t e g e r > primeNumbers = new HashSet < >();
primeNumbers . add ( 2 ) ;
primeNumbers . add ( 3 ) ;
System . o u t . p r i n t l n ( ” HashSet2 : ” + primeNumbers ) ;
b o o l e a n r e s u l t = numbers . c o n t a i n s A l l ( primeNumbers ) ;
System . o u t . p r i n t l n ( ” I s HashSet2 i s s u b s e t o f HashSet1 ?
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
46 / 88
Other methods of HashSet

1 clone() Creates a copy of the HashSet


2 contains() Searches the HashSet for the specified element and returns
a boolean result
3 isEmpty() Checks if the HashSet is empty
4 size() Returns the size of the HashSet
5 clear() Removes all the elements from the HashSet
Why HashSet?
In Java, HashSet is commonly used if we have to access elements randomly.
It is because elements in a hash table are accessed using hash codes.
The hashcode of an element is a unique identity that helps to identify the
element in a hash table.
HashSet cannot contain duplicate elements. Hence, each hash set element
has a unique hashcode.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
47 / 88
Linked list data structure

A linked list is a linear data structure that includes a series of connected


nodes. Here, each node stores the data and the address of the next node.

Figure: Singly linked list

Each node has data and a pointer to the next node.


Note: You might have played the game Treasure Hunt, where each clue
includes the information about the next clue. That is how the linked list
operates.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
48 / 88
Doubly Linked list

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
49 / 88
Circular Linked list

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
50 / 88
Java LinkedHashSet

The LinkedHashSet class of the Java collections framework provides func-


tionalities of both the hashtable and the linked list data structure.
It implements the Set interface.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
51 / 88
Java LinkedHashSet

Elements of LinkedHashSet are stored in hash tables similar to


HashSet.
However, linked hash sets maintain a doubly-linked list internally for
all of its elements.
The linked list defines the order in which elements are inserted in hash
tables.
Create a LinkedHashSet
In order to create a linked hash set, we must import the java.util.LinkedHashSet
package first.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
52 / 88
creating LinkedHashSet

// L i n k e d H a s h S e t w i t h 8 c a p a c i t y and 0 . 7 5 l o a d f a c t o r
L i n k e d H a s h S e t <I n t e g e r > numbers = new L i n k e d H a s h S e t <>(8
Default capacity and load factor
// L i n k e d H a s h S e t w i t h d e f a u l t c a p a c i t y and l o a d f a c t o r
L i n k e d H a s h S e t <I n t e g e r > numbers1 = new L i n k e d H a s h S e t < >

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
53 / 88
Methods of LinkedHashSet
The LinkedHashSet class provides methods that allow us to perform various
operations on the linked hash set.
Insert Elements to LinkedHashSet
import java . u t i l . LinkedHashSet ;
class linked1 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
L i n k e d H a s h S e t <I n t e g e r > evenNumber = new L i n k e d H a s h S e t <
evenNumber . add ( 2 ) ;
evenNumber . add ( 4 ) ;
evenNumber . add ( 6 ) ;
System . o u t . p r i n t l n ( ” L i n k e d H a s h S e t : ” + evenNumber ) ;
L i n k e d H a s h S e t <I n t e g e r > numbers = new L i n k e d H a s h S e t < >()
numbers . a d d A l l ( evenNumber ) ;
numbers . add ( 5 ) ;
System . o u t . p r i n t l n ( ” New L i n k e d H a s h S e t : ” + numbers ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
54 / 88
Accessing LinkedHashSet

import java . u t i l . LinkedHashSet ;


import java . u t i l . I t e r a t o r ;
class linked2 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
L i n k e d H a s h S e t <I n t e g e r > numbers = new L i n k e d H a s h S e t < >()
numbers . add ( 2 ) ;
numbers . add ( 5 ) ;
numbers . add ( 6 ) ;
System . o u t . p r i n t l n ( ” L i n k e d H a s h S e t : ” + numbers ) ;
I t e r a t o r <I n t e g e r > i t e r a t e = numbers . i t e r a t o r ( ) ;
System . o u t . p r i n t ( ” L i n k e d H a s h S e t u s i n g I t e r a t o r : ” ) ;
w h i l e ( i t e r a t e . hasNext ( ) ) {
System . o u t . p r i n t ( i t e r a t e . n e x t ( ) ) ;
System . o u t . p r i n t ( ” , ” ) ;
}}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
55 / 88
Remove Elements from HashSet

import java . u t i l . LinkedHashSet ;


class linked3 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
L i n k e d H a s h S e t <I n t e g e r > numbers = new L i n k e d H a s h S e t < >()
numbers . add ( 2 ) ;
numbers . add ( 5 ) ;
numbers . add ( 6 ) ;
System . o u t . p r i n t l n ( ” L i n k e d H a s h S e t : ” + numbers ) ;
b o o l e a n v a l u e 1 = numbers . remove ( 5 ) ;
System . o u t . p r i n t l n ( ” I s 5 removed ? ” + v a l u e 1 ) ;
b o o l e a n v a l u e 2 = numbers . r e m o v e A l l ( numbers ) ;
System . o u t . p r i n t l n ( ” Are a l l e l e m e n t s removed ? ” + v a l u
}}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
56 / 88
Set Operations
Union of Sets To perform the union between two sets, we can use the
addAll() method.
import java . u t i l . LinkedHashSet ;
class linked4 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
L i n k e d H a s h S e t <I n t e g e r > evenNumbers = new L i n k e d H a s h S e t
evenNumbers . add ( 2 ) ;
evenNumbers . add ( 4 ) ;
System . o u t . p r i n t l n ( ” L i n k e d H a s h S e t 1 : ” + evenNumbers ) ;
L i n k e d H a s h S e t <I n t e g e r > numbers = new L i n k e d H a s h S e t < >()
numbers . add ( 1 ) ;
numbers . add ( 3 ) ;
System . o u t . p r i n t l n ( ” L i n k e d H a s h S e t 2 : ” + numbers ) ;
numbers . a d d A l l ( evenNumbers ) ;
System . o u t . p r i n t l n ( ” Union i s : ” + numbers ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
57 / 88
Intersection of Sets
To perform the intersection between two sets, we can use the retainAll()
method.
import java . u t i l . LinkedHashSet ;
class linked5 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
L i n k e d H a s h S e t <I n t e g e r > primeNumbers = new L i n k e d H a s h S e
primeNumbers . add ( 2 ) ;
primeNumbers . add ( 3 ) ;
System . o u t . p r i n t l n ( ” L i n k e d H a s h S e t 1 : ” + primeNumbers ) ;
L i n k e d H a s h S e t <I n t e g e r > evenNumbers = new L i n k e d H a s h S e t
evenNumbers . add ( 2 ) ;
evenNumbers . add ( 4 ) ;
System . o u t . p r i n t l n ( ” L i n k e d H a s h S e t 2 : ” + evenNumbers ) ;
evenNumbers . r e t a i n A l l ( primeNumbers ) ;
System . o u t . p r i n t l n ( ” I n t e r s e c t i o n i s : ” + evenNumbers ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
58 / 88
Difference of Sets
To calculate the difference between the two sets, we can use the removeAll()
method.
import java . u t i l . LinkedHashSet ;
class linked6 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
L i n k e d H a s h S e t <I n t e g e r > primeNumbers = new L i n k e d H a s h S e
primeNumbers . add ( 2 ) ;
primeNumbers . add ( 3 ) ;
primeNumbers . add ( 5 ) ;
System . o u t . p r i n t l n ( ” L i n k e d H a s h S e t 1 : ” + primeNumbers ) ;
L i n k e d H a s h S e t <I n t e g e r > oddNumbers = new L i n k e d H a s h S e t
oddNumbers . add ( 1 ) ;
oddNumbers . add ( 3 ) ;
oddNumbers . add ( 5 ) ;
System . o u t . p r i n t l n ( ” L i n k e d H a s h S e t 2 : ” + oddNumbers ) ;
primeNumbers . r e m o v e A l l ( oddNumbers ) ;
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
59 / 88
Subset

import java . u t i l . LinkedHashSet ;


class linked7 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
L i n k e d H a s h S e t <I n t e g e r > numbers = new L i n k e d H a s h S e t < >()
numbers . add ( 1 ) ;
numbers . add ( 2 ) ;
numbers . add ( 3 ) ;
System . o u t . p r i n t l n ( ” L i n k e d H a s h S e t 1 : ” + numbers ) ;
L i n k e d H a s h S e t <I n t e g e r > primeNumbers = new L i n k e d H a s h S e
primeNumbers . add ( 2 ) ;
primeNumbers . add ( 3 ) ;
System . o u t . p r i n t l n ( ” L i n k e d H a s h S e t 2 : ” + primeNumbers ) ;
b o o l e a n r e s u l t = numbers . c o n t a i n s A l l ( primeNumbers ) ;
System . o u t . p r i n t l n ( ” I s L i n k e d H a s h S e t 2 i s s u b s e t o f L i n
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
60 / 88
LinkedHashSet Vs. HashSet

Both LinkedHashSet and HashSet implements the Set interface. However,


there exist some differences between them.
1 LinkedHashSet maintains a linked list internally. Due to this, it
maintains the insertion order of its elements.
2 The LinkedHashSet class requires more storage than HashSet. This is
because LinkedHashSet maintains linked lists internally.
3 The performance of LinkedHashSet is slower than HashSet. It is
because of linked lists present in LinkedHashSet.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
61 / 88
Java Map Interface
The Map interface of the Java collections framework provides the function-
ality of the map data structure.
Working of Map In Java, elements of Map are stored in key/value pairs.
Keys are unique values associated with individual Values.
A map cannot contain duplicate keys. And, each key is associated with a
single value.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
62 / 88
Working of Map

1 We can access and modify values using the keys associated with them.
2 The Map interface maintains 3 different sets:
3 the set of keys
4 the set of values
5 the set of key/value associations (mapping).
Hence we can access keys, values, and associations individually.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
63 / 88
classes implementing Map Interface

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
64 / 88
Methods of Map

put(K, V) - Inserts the association of a key K and a value V into the


map. If the key is already present, the new value replaces the old
value.
putAll() - Inserts all the entries from the specified map to this map.
putIfAbsent(K, V) - Inserts the association if the key K is not already
associated with the value V.
get(K) - Returns the value associated with the specified key K. If the
key is not found, it returns null.
getOrDefault(K, defaultValue) - Returns the value associated with the
specified key K. If the key is not found, it returns the defaultValue.
containsKey(K) - Checks if the specified key K is present in the map
or not.
containsValue(V) - Checks if the specified value V is present in the
map or not.
replace(K, V) - Replace the value of the key K with the new specified
value V.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
65 / 88
Methods of Map

replace(K, oldValue, newValue) - Replaces the value of the key K


with the new value newValue only if the key K is associated with the
value oldValue.
remove(K) - Removes the entry from the map represented by the key
K.
remove(K, V) - Removes the entry from the map that has key K
associated with value V.
keySet() - Returns a set of all the keys present in a map.
values() - Returns a set of all the values present in a map.
entrySet() - Returns a set of all the key/value mapping present in a
map.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
66 / 88
Implementation of the Map Interface
Implementing HashMap Class
i m p o r t j a v a . u t i l . Map ;
i m p o r t j a v a . u t i l . HashMap ;
c l a s s map1 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Map<S t r i n g , I n t e g e r > numbers = new HashMap < >();
numbers . p u t ( ” One ” , 1 ) ;
numbers . p u t ( ”Two” , 2 ) ;
System . o u t . p r i n t l n ( ”Map : ” + numbers ) ;
System . o u t . p r i n t l n ( ” Keys : ” + numbers . k e y S e t ( ) ) ;
System . o u t . p r i n t l n ( ” V a l u e s : ” + numbers . v a l u e s ( ) ) ;
System . o u t . p r i n t l n ( ” E n t r i e s : ” + numbers . e n t r y S e t ( ) ) ;
i n t v a l u e = numbers . remove ( ”Two ” ) ;
System . o u t . p r i n t l n ( ” Removed V a l u e : ” + v a l u e ) ;
}}
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
67 / 88
Implementing TreeMap Class

i m p o r t j a v a . u t i l . Map ;
i m p o r t j a v a . u t i l . TreeMap ;
c l a s s map2 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
Map<S t r i n g , I n t e g e r > v a l u e s = new TreeMap < >();
v a l u e s . p u t ( ” Second ” , 2 ) ;
v a l u e s . put (” F i r s t ” , 1 ) ;
System . o u t . p r i n t l n ( ”Map u s i n g TreeMap : ” + v a l u e s ) ;
v a l u e s . r e p l a c e (” F i r s t ” , 1 1) ;
v a l u e s . r e p l a c e ( ” Second ” , 2 2 ) ;
System . o u t . p r i n t l n ( ” New Map : ” + v a l u e s ) ;
i n t r e m o v e d V a l u e = v a l u e s . remove ( ” F i r s t ” ) ;
System . o u t . p r i n t l n ( ” Removed V a l u e : ” + r e m o v e d V a l u e ) ;
}}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
68 / 88
Java HashMap
The HashMap class of the Java collections framework provides the
functionality of the hash table data structure.
It stores elements in key/value pairs. Here, keys are unique identifiers
used to associate each value on a map.
The HashMap class implements the Map interface.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
69 / 88
Create HashMap

In order to create a hash map, we must import the java.util.HashMap pack-


age first. Once we import the package, here is how we can create hashmaps
in Java.
HashMap<K, V> numbers = new HashMap < >();
In the above code, we have created a hashmap named numbers. Here, K
represents the key type and V represents the type of values.
HashMap<S t r i n g , I n t e g e r > numbers = new HashMap < >();

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
70 / 88
Create HashMap in Java

i m p o r t j a v a . u t i l . HashMap ;
c l a s s map3{
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashMap<S t r i n g , I n t e g e r > l a n g u a g e s = new HashMap < >();
l a n g u a g e s . put (” Java ” , 8 ) ;
l a n g u a g e s . put (” J a v a S c r i p t ” , 1 ) ;
l a n g u a g e s . p u t ( ” Python ” , 3 ) ;
System . o u t . p r i n t l n ( ” HashMap : ” + l a n g u a g e s ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
71 / 88
Add elements to a HashMap

i m p o r t j a v a . u t i l . HashMap ;
c l a s s map4 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashMap<S t r i n g , I n t e g e r > numbers = new HashMap < >();
System . o u t . p r i n t l n ( ” I n i t i a l HashMap : ” + numbers ) ;
numbers . p u t ( ” One ” , 1 ) ;
numbers . p u t ( ”Two” , 2 ) ;
numbers . p u t ( ” Three ” , 3 ) ;
System . o u t . p r i n t l n ( ” HashMap a f t e r p u t ( ) : ” + numbers ) ;
}}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
72 / 88
Access HashMap elements

i m p o r t j a v a . u t i l . HashMap ;
c l a s s map5 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashMap<I n t e g e r , S t r i n g > l a n g u a g e s = new HashMap < >();
l a n g u a g e s . put (1 , ” Java ” ) ;
l a n g u a g e s . p u t ( 2 , ” Python ” ) ;
l a n g u a g e s . put (3 , ” J a v a S c r i p t ” ) ;
System . o u t . p r i n t l n ( ” HashMap : ” + l a n g u a g e s ) ;
String value = languages . get ( 1 ) ;
System . o u t . p r i n t l n ( ” V a l u e a t i n d e x 1 : ” + v a l u e ) ;
}}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
73 / 88
change HashMap value

i m p o r t j a v a . u t i l . HashMap ;
c l a s s map6 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashMap<I n t e g e r , S t r i n g > l a n g u a g e s = new HashMap < >();
l a n g u a g e s . put (1 , ” Java ” ) ;
l a n g u a g e s . p u t ( 2 , ” Python ” ) ;
l a n g u a g e s . put (3 , ” J a v a S c r i p t ” ) ;
System . o u t . p r i n t l n ( ” O r i g i n a l HashMap : ” + l a n g u a g e s ) ;
l a n g u a g e s . r e p l a c e ( 2 , ”C++”);
System . o u t . p r i n t l n ( ” HashMap u s i n g r e p l a c e ( ) : ” + l a n g u
}}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
74 / 88
Remove HashMap elements

i m p o r t j a v a . u t i l . HashMap ;
c l a s s map7 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
HashMap<I n t e g e r , S t r i n g > l a n g u a g e s = new HashMap < >();
l a n g u a g e s . put (1 , ” Java ” ) ;
l a n g u a g e s . p u t ( 2 , ” Python ” ) ;
l a n g u a g e s . put (3 , ” J a v a S c r i p t ” ) ;
System . o u t . p r i n t l n ( ” HashMap : ” + l a n g u a g e s ) ;
S t r i n g v a l u e = l a n g u a g e s . remove ( 2 ) ;
System . o u t . p r i n t l n ( ” Removed v a l u e : ” + v a l u e ) ;
System . o u t . p r i n t l n ( ” Updated HashMap : ” + l a n g u a g e s ) ;
}}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
75 / 88
Java Wrapper classes

The wrapper classes in Java are used to convert primitive types (int, char,
float, etc) into corresponding objects.
Each of the 8 primitive types has corresponding wrapper classes.

Primitive Type Wrapper Class


byte Byte
boolean Boolean
char Character
double Double
float Float
int Integer
long Long
short Short

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
76 / 88
Convert Primitive Type to Wrapper Objects

We can also use the valueOf() method to convert primitive types into cor-
responding objects.
c l a s s wrapper1 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
int a = 5;
double b = 5.65;
I n t e g e r aObj = I n t e g e r . v a l u e O f ( a ) ;
Double bObj = Double . v a l u e O f ( b ) ;
i f ( aObj i n s t a n c e o f I n t e g e r ) {
System . o u t . p r i n t l n ( ” An o b j e c t o f I n t e g e r i s c r e a t e d . ” )
}
i f ( bObj i n s t a n c e o f Double ) {
System . o u t . p r i n t l n ( ” An o b j e c t o f Double i s c r e a t e d . ” ) ;
}}}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
77 / 88
In the above example, we have used the valueOf() method to convert
the primitive types into objects.
Here, we have used the instanceof operator to check whether the
generated objects are of Integer or Double type or not.
However, the Java compiler can directly convert the primitive types
into corresponding objects.This process is known as auto-boxing.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
78 / 88
Wrapper Objects into Primitive Types

c l a s s wrapper2 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
// c r e a t e s o b j e c t s o f w r a p p e r c l a s s
I n t e g e r aObj = I n t e g e r . v a l u e O f ( 2 3 ) ;
Double bObj = Double . v a l u e O f ( 5 . 5 5 ) ;
// c o n v e r t s i n t o p r i m i t i v e t y p e s
i n t a = aObj . i n t V a l u e ( ) ;
d o u b l e b = bObj . d o u b l e V a l u e ( ) ;
System . o u t . p r i n t l n ( ” The v a l u e o f a : ” + a ) ;
System . o u t . p r i n t l n ( ” The v a l u e o f b : ” + b ) ;
}
}
In the above example, we have used the intValue() and doubleValue()
method to convert the Integer and Double objects into corresponding prim-
itive types.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
79 / 88
Java autoboxing

import java . u t i l . A r r a y L i s t ;
c l a s s auto {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
A r r a y L i s t <I n t e g e r > l i s t = new A r r a y L i s t < >();
// a u t o b o x i n g
l i s t . add ( 5 ) ;
l i s t . add ( 6 ) ;
System . o u t . p r i n t l n ( ” A r r a y L i s t : ” + l i s t ) ;
}
}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
80 / 88
Java Unboxing

import java . u t i l . A r r a y L i s t ;
c l a s s unbox {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
A r r a y L i s t <I n t e g e r > l i s t = new A r r a y L i s t < >();
// a u t o b o x i n g
l i s t . add ( 5 ) ;
l i s t . add ( 6 ) ;
System . o u t . p r i n t l n ( ” A r r a y L i s t : ” + l i s t ) ;
// u n b o x i n g
i n t a = l i s t . get ( 0 ) ;
System . o u t . p r i n t l n ( ” V a l u e a t i n d e x 0 : ” + a ) ;
}}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
81 / 88
Java Generics

The Java Generics allows us to create a single class, interface, and


method that can be used with different types of data (objects).
This helps us to reuse our code.
Java Generics ClassWe can create a class that can be used with any type
of data. Such a class is known as Generics Class.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
82 / 88
c l a s s gen1 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
// i n i t i a l i z e g e n e r i c c l a s s
// w i t h I n t e g e r d a t a
G e n e r i c s C l a s s <I n t e g e r > i n t O b j = new G e n e r i c s C l a s s < >(5)
System . o u t . p r i n t l n ( ” G e n e r i c C l a s s r e t u r n s : ” + i n t O b j .
// i n i t i a l i z e g e n e r i c c l a s s
// w i t h S t r i n g d a t a
G e n e r i c s C l a s s <S t r i n g > s t r i n g O b j = new G e n e r i c s C l a s s <>(
System . o u t . p r i n t l n ( ” G e n e r i c C l a s s r e t u r n s : ” + s t r i n g O
}}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
83 / 88
// c r e a t e a g e n e r i c s c l a s s
c l a s s G e n e r i c s C l a s s <T> {
// v a r i a b l e o f T t y p e
p r i v a t e T data ;
p u b l i c G e n e r i c s C l a s s (T d a t a ) {
t h i s . data = data ;}
// method t h a t r e t u r n T t y p e v a r i a b l e
p u b l i c T getData ( ) {
r e t u r n t h i s . data ;
}}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
84 / 88
Java Generics Method
Similar to the generics class, we can also create a method that can be used
with any type of data. Such a class is known as Generics Method.
c l a s s gen2 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
DemoClass demo = new DemoClass ( ) ;
demo.< S t r i n g >g e n e r i c s M e t h o d ( ” J a v a Programming ” ) ;
demo.< I n t e g e r >g e n e r i c s M e t h o d ( 2 5 ) ;
}}
c l a s s DemoClass {
p u b l i c <T> v o i d g e n e r i c s M e t h o d (T d a t a ) {
System . o u t . p r i n t l n ( ” G e n e r i c s Method : ” ) ;
System . o u t . p r i n t l n ( ” Data P a s s e d : ” + d a t a ) ;
}}
In the above example, we have created a generic method named generic-
sMethod.
Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1
Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
85 / 88
p u b l i c <T> v o i d g e n e r i c M e t h o d (T d a t a ) { . . . }
Here, the type parameter < T > is inserted after the modifier public and
before the return type void.
We can call the generics method by placing the actual type < String > and
< Integer > inside the bracket before the method name.
demo.< S t r i n g >g e n e r i c M e t h o d ( ” J a v a Programming ” ) ;

demo.< I n t e g e r >g e n e r i c M e t h o d ( 2 5 ) ;

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
86 / 88
Bounded Types

In general, the type parameter can accept any data types (except primitive
types).
However, if we want to use generics for some specific types (such as accept
data of number types) only, then we can use bounded types.
In the case of bound types, we use the extends keyword. For example,
<T e x t e n d s A>
This means T can only accept data that are subtypes of A.

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
87 / 88
Example for Bounded Type

c l a s s G e n e r i c s C l a s s <T e x t e n d s Number> {
public void display () {
System . o u t . p r i n t l n ( ” bounded t y p e g e n e r i c s c l a s s . ” ) ;
}}
c l a s s gen3 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
// c r e a t e an o b j e c t o f G e n e r i c s C l a s s
G e n e r i c s C l a s s <S t r i n g > o b j = new G e n e r i c s C l a s s < >();
}
}

Dr. S Sudheer Mangalampalli Assistant Professor, Senior Grade1


Collections
School of Computer Science and Engineering
June 14,
VIT-AP
2022 University
88 / 88

You might also like