okay we'll see one concept uh collections collections in Java so first of all
what is the generic meaning of uh collection so English meaning collection so when we will
use a collection the word collection means a group of element right whenever you want to
represent something some group of elements we will say collection let's say collection of
books or collection of pens or collection of some items so whenever you want to represent
some group of items or group of elements or group of objects we call it as a collection of
elements or collection of books or collection of items something like this so collection is a
word which is representing a group of uh objects or a group of items or something basically it's
a it representing a particular group which is called collection right collection is a English
uh word and in Java collection collection is an interface predefined interface in Java there is
an interface called collection Java there is an interface called collection and this collection
is having set a number of methods and variables and this collection interface is implemented
by n number of classes there are many classes are there uh uh sorry so collection is a basical
interface and this is again extended into two more interfaces list interface and set interface so
let me show you the hierarchy of interfaces just a yeah yes look at this so this is the structure
in Java we have a collection interface which is a root interface of all the collections and this
collection interface again exted into two more interfaces list and set interfaces list is one
interface set is another interface so this is the first one and this is second one and here they
have extended not implemented so one interface we can extend into another interface right one
parent interface one ja interface so collection interface is extended to list interface and also
set interface now way in further list interface is implemented by ARR list class and set interface is
implemented by hash set class so whenever there is an interface that should be implemented by using a
class so class must be there so the list interface is implemented by array list class set interface
is implemented by hash set class similarly there are some more interfaces are there some more
classes are implemented those interfaces and we call all of them called collections collections
means these interfaces or these classes we can use to represent a group of objects we can represent
group of objects object or group of elements or group of item whatever it is whenever you're
representing some group of items we go for either array list class or we can go for either
hash set class or anything like we have a link list okay and hash set hash link list so many
collections are there up to 10 to 15 collections are there and majorly we use this list collection
and set collection there is another one is called ashap that is totally different we will discuss
that later so majorly collection is is a root interface which is implemented which is extended
into again list interface and set interface list interface is implemented by ARR list set
interface is implemented by hashit just remember this hierarchy okay now I have one question here
let us say in the collection there are certain number of methods and uh list interface and set
interfaces extends The Collection so is there any possibility to have common methods in list
and set interfaces is there any possibility to have common methods yes similarly do you have any
possibility to have common methods in Array list and haset Har list is implemented list collection
and list interface hash set is also implemented set interface now do we have any common methods in
Array list and hash any possibility yes so array list and has a set are two different classes there
is no connection between these two why because ARR list is implemented list collection list
interface hash set is implemented set interface so there could be some common methods are there
apart from those common methods y list can have some additional methods haset is also having
some more additional methods which are not commonly available in both the classes possibility
is there right because some methods will Implement from list interface and let us say there are five
methods are coming from list interface and a list can have another five methods totally 10 methods
possibility is there similarly few methods coming from the set interface hash set is also can have
their own methods possibility is there so array list specific methods may not be available in
hash set interface sry hash set class hash set specific methods cannot be available able in aist
class okay so this is a basic understanding which we need to have so why we need the interfaces
and collection uh classes because we need this to representing group of object especially the
collections all kinds of collections it can be list ARR list haset hash any type of collection
if you take basically the collections can hold objects not primitive types remember this
okay we cannot store primitive types of data in collections so list is one collection set is
a collection hash map is a collection all these things are called collections why because they
can hold group of objects they can hold group of objects or group of elements or group of items
so whenever I say object that should not be a primitive okay whenever I say object that should
not be a primitive primitive is in the sense what in float double okay and these are all primitive
types so this type of data we cannot store but still if you want to store in numbers right
in a collection then what we can do we have to use something called wrapper classes because
wrapper classes are basically what we can create an objects right for integer class we can have
integer object for double class we have a double object something like this so if you still want
to store numbers right or Boolean or some uh data so we have to use wrapper classes instead of
primitive type so string we can directly use no problem because string itself is a class so
we can directly store them in the collection okay so primitive data is not allowed string
is not a primitive okay this is nonprimitive data type string is a class basically right
so strings we can store no problem strings are allowed other than strings we can store
any type of objects in collections remember one Point collections allows only object type of
data objects only allowed it can be predefined or user defined whatever it is only object type
of data we can store primitive data not allowed in collections okay and collections especially
designed for only objects for object oriented programming only the collections we have to
use so all objects a group of objects we can represent in collection and colle collection
means what collection is representing a group of elements or group of objects that's a general
meaning but in Java collection is an interface of all the collection root interface of all the
collection and this interface is again extended into list interface and set interface list is a
parent of array list set is a parent of hash set and both are totally different having their own
behavior their own nature okay we will see what what are the differences where we have to use
a list where we have to ask it and when it is applicable when it is not applicable so we'll
try to understand all the scenarios one by one okay now let me start with the first list
then we will discuss it and hashmap is totally different hashmap is a again class which is
implemented class of map interface and there is no con connection between collection interface
and map interface the map is also there is another type of interface okay we will discuss map later
first we will start with collection list and set collections let me minimize it first listen
then ask the questions okay I will take all the questions at the end of the session even if you
type now I I'm not going to address them because there are a lot of things we need to First discuss
and once you understand then ask the questions most of the questions will be clarified okay right
so now go to the package and you can just take new package and uh I name it this is day 20 okay yeah
so whatever we are discussing Java everything we are using in automation there is no question about
it okay so let's say few Concepts we are using automation few Concepts not not using automation
it's not like that the entire automation will use all the concepts from java whatever we discussed
so far somewhere in some scenarios we will use everything so otherwise why we should discuss
why I should teach the Java sessions and why you should teach all the concepts unnecessarily if
you're not using in automation right so whatever topic even it is small topic we are discussed in
Java including collections we are going to use in automation okay there is no second question on
that everything we are going to use in automation but when you will know that once you start
automation once you start using the Java Concepts in automation then you will understand okay how
we can use these Concepts in automation where exactly these Concepts will fit into and where
it is applicable so once you start automation you will aware of it okay but before that we are
learning all the concepts so there is no secondary question everything every concept whatever we
discuss in Java we are going to use in automation okay all right now go to package now I'm going
to create new class and first we'll start with the array list so yeah array list listen very
carefully very important concept and I'll show you multiple things in one example and there
are lot of differences also there and we need to understand them even interor perspective also
these collections are very very important right so array list demo and uh I'm taking main method
of also click on the Finish okay so first of all let us first understand what is an array list and
where it is applicable and what we can achieve by using array list class so in Java as I told
you collection is basically uh a terminology in Java collection is an interface so collection to
representing group of elements objects data into single entity we use collection and collection
is an interface available in java.util package in Java we have a package called java.util in this
package all the collections are available so now let us discuss something about erray list so what
are the properties of error list when to use why we need to use all these things so array list is
basically what it is a class remember array list is a a class which is implemented class of list
interface so as we have seen uh in hierarchy The Collection is an interface root interface which is
implemented by many many again which is extended into many interfaces list is one of the interface
and list is again implemented by erray list right overall the collection and list interfaces are
implemented by what err list okay so err list is a class which is implemented class of what list
interface okay so list interface is implemented by array list class so array list is a class which
is implemented list interface which is implemented list interface these are all predefined which
is already there in java. U package okay so the first point in Array list is heterogeneous data
is allowed heterogeneous data is allowed in Array list what does mean what is mean heterogeneous
data meaning is what heterogeneous homogeneous data heterogeneous data we have a two kinds
of data homo genous data means same type of data heterogeneous means different type of data
or different type of objects different type of elements is called heterogeneous data so array
list allows to store heterogeneous data that means it doesn't mean homogeneous data we cannot
store no homogeneous data also we can store along with that heterogeneous data also allowed we
can store different type of objects okay so for example I have EMP employee type of objects
student type of objects so all kinds of objects we can store heterogeneous data is allowed second
Point insertion order is preserved that means it will follow index just like a normal arrays
concept array list is also followed the array list is also follow the indexing concept index
will start from zero 0 1 2 3 4 like this just like there are some similarities are there between
arrays and array list but major difference is in arrays normally we prefer to use for primitive
data types but ARR list we will prefer to use objects okay whenever you're working with objects
we prefer to use array list when you're working with the Primitive types we use normal arrays and
another difference is in the normal arrays if you declare an array with some size we cannot store
more than those elements when I say array size is five that is a fixed we cannot store more than
five elements but AIS is a dynamic there is no specific size we don't need to specify any size
we can keep adding more number of objects okay so that's the reason we can also call this array list
is a dynamic array means we can have n number of objects how many objects you want to store you can
store there is no limit for that and we know need to specify the size also okay according to the
number of object it keep increasing dynamically so the array list is also called as Dynamic array
so array list allows to store heterogeneous data all kinds of data we can store means all the
data means what there should be again objects type of data not primitive data now insertion
order is also preserved index concept it will follow zero index start from zero and 1 2 3
4 something like this even the data which is there in the ARR list also we will represent
by using index concept so indexing is followed whenever indexing is followed that can store the
data in order insertion order is preserved means in whichever order we have added the data the
data is also provided in the same order that's called insertion order preserved so observe very
carefully first understand the points then we will try to implement practically insert order
preserved indexing is nothing but what suppose you have created a list like this and uh let's
say you added some data like say 10 20 30 like this right in whichever order you specify the data
in the same order the data will be stored in the memory I said this is zero element first element
second element so 10 20 30 so same order it will maintain because indexing is possible here it will
support indexing concept so index is a something called is a kind of algorithm to store the data
so to store the data in the memory there are two kinds of algorithms are there one is indexing
second one is hashing indexing hashing okay indexing is nothing but the data will be stored
in the same place in particular order let's say suppose if I take array list or arrays concept we
are giving some indexes right so let's say this is entire memory and suppose if you create an array
okay and uh let's say is some memory wherever it is got free it will go there and it will create
an array like this and the memory location 0 1 2 3 in the same position it will store all the
elements we can find in one single place and those elements we can represent by using Index this
is indexing technique indexing it will store in particular order okay hashing is nothing but what
elements will be stored in the different places in the memory there is no proper order maintained
hashing technique in hashing technique there is no proper order maintained wherever the memory is
got free it will go and store that object objects will store in the different different places in
the memory whereas in indexing technique objects will be stored in the sequential of memory like
this okay so here insertion order is preserved means in whichever order we have stored the
elements in the same order it will maintain but in hashing it will not maintain the same order
so in whichever order we have added the data same order it will not maintain in hashing technique
okay so remember this these are two kinds of in algorithms are there I'm not going to deep in this
because that is not much important for us just for understanding the point inser order preserved
means what index follows if index is supported insertion order is preserved but index is not
supported means what it will support hashing technique if it support hashing technique of
algorithm then it will not preserve the insertion order just remember only these two points don't
go go beyond that okay so some collections will use indexing some collections will use hashing for
example uh hash set hash map so these collections will use hashing technique of algorithm array
index or array list we use indexing concept okay so some collections we use use hashing
technique some collections will use indexing technique okay right so as of now array list will
follow indexing algorithm so insertion order is preserved insertion order is preserved first point
heterogeneous data is allowed insertion order is preserved third Point duplicate elements also
allowed that means a duplicate objects also we can store duplicates are allowed and multiple
nulls also allowed suppose sometimes I am not aware of the value unknown values are there or
unknown objects are there still we can store them by using null so nulls also allowed multiple nulls
also allowed heterogeneous data allowed insertion order is preserved indexing is followed index
will start from zero and duplicate objects also allowed or duplicate elements are allowed multiple
nulls also allowed these are the four points we have to remember before using array list so how
we will know when you use ARR list when we have to use array list based upon the points we have
to decide or based upon the data which we have to define suppose you have some data or suppose
you have some objects and you want to store these objects in the collection and how we will know
which collection should be preferred you need to understand this point okay if your objects are
different then you can go with the ARR list and if you want to store the objects in the proper
order and at the time of adding at the time of retrieving if you want to use IND indexing then
you can go with the array list and if you have a duplicate objects also you have to go with the
array list and if you have a multiple nulls in the data then also we can go with the array list
so if all the points are satisfied if your data is satisfied all these points then go with the
error list concept okay if your objects or if you have some data which is satisfying all these
points then go with the error list concept and similarly have set is having different properties
hashmap is having different properties So based upon the data or based upon the availability of
the data and type of data we have to choose the right type of collection in your automation or in
your programming multiple nulls in the sense if the data value is unknown for example this is an
array list and have some objects already stored but in this location I don't know which object
I want to store but in future I will store some object here as of now I don't know what kind of
object I should have in this so simply you can put null okay simply you can put null and later you
can add this if you want so null is nothing but unknown null is nothing but not an empty value it
is unknown means as of now I put a null and later I will add this object okay so multiple nulls
are allowed so duplicate elements also allowed insertion order is preserved heterogeneous data
is also allow so these are the four points which you have to remember when you're working with
the array list concept and at the end I will also compare with other collections we'll compare
has hashmap and everything first individually we will try to understand and also practically we do
different type of operations and finally we will summarize it okay now let's see the Practical
part so how we can use array list what are the methods are there as I said array list is a class
err list is a class means what which contains so many methods right and what are those methods and
how we can use those methods to perform different type of operations okay I have defined some array
list I have put some objects in the array list what is the use we have to perform certain type of
operations right on the data and those operations we can do by using methods which are available
in the ARR list class because whenever I say class that means what which contains some
methods predefined class means predefined methods are available now we'll discuss what are
those methods are available how we can use those methods to perform different type of operations on
array list okay now go to eclipse and we already created one class called array list demo and when
you're giving the class names don't give existing class names like suppose array list array list is
already the class which is already available so we should not use the same name to the class which
we have created so you can just add something like a list Dem or aist example something like this
but don't give the same name okay right so now I created one new class and there is a main method
available now we'll see how we can use this arist first thing uh how to declare the array list
declaration part how to create a variable for the array list declaration there are many ways to
declare the array list variable so I will show you all the ways here you guys can tell me whether
the Declaration is valid or not okay the first declaration array list is a class so we can create
an object for the class right so I say array list array list okay and I'll say my list equal
to new array list okay array list is a external class which is coming from java.util package so
that we have to import so when you place the when you place the cursor here it will show you the
import package you can see Java AR list comes from java. util package so import this right so
now tell me is the syntax correct or not can we create an object for the class or not yes so if
you look at the here object we created for the child class and reference variable is also child
class if I look at here array list class right for this array list we created object as well as we
created a reference variable so both we have done so this is exactly perfect syntax so we created
an object for what a list class that object is referred by my list variable this is perfect now
can I do like this list my list equal to new erray list new error list and the list is an interface
which we have to import again from java. so now tell me is the syntax correct correct or not yes
obviously why because child class object can hold in the parent class variable child class object
we can hold in the parent class variable right everything so we can access everything from by
using this object so this is also the correct notation this is one type of Declaration okay so
if I use these two types of declarations either first one or second one the array list allows
to store heterogeneous data we can store any type of objects all kinds of types we can store
suppose sometimes I want to restrict a realist to store homogeneous data suppose I don't have
requirement to store different type of data I want to store same type of data then what we can
do we can do like this array list my list equal to new ARR list this is normal definition right
so it will allow all erogeneous data now I want to store only homogeneous data suppose I want
to store only integer type of numbers then in the angular bracket you have to specify rapper
class here and here both the sides and here okay now this particular ARR list allows to store
only integer type of data integer in the sense again they are the integer type of objects not
primitive types okay integer type of data it is allowed for example instead of integer if I
say string then this arist can store it allows to store only string type of data both the sides
we have to put string so this array list can hold only string type of data only string type of data
we can store okay suppose if I create a list like this suppose I have something called employee
class okay now here you can specify employee and here also you can specify employee so then
what type of objects it can store what type of objects it can store employee type of object
suppose you have already employ class and I have created multiple objects of that class let's
say emp1 emp2 emp3 and you want to store all those objects into single entity okay so then you can
specify that let's say you have an employee class okay in the employee class you created and you
have multiple objects emp1 emp2 and so on now I want to represent all these objects into a single
entity or single variable then you can put here and here employee then this array list can hold
multiple EMP objects okay so ultimately the AR list can hold only objects type of data it can
be predefined objects or it can be user defined objects anything even the numbers also can we put
here int and int no because primitive types are not allowed but still if you want to store numbers
we have to specify corresponding rapper class that's the reason rapper class are introduced okay
rapper class we can use remember this so these are the many ways we can declare the array list so if
you want to store heterogeneous data you can go with these two definitions declarations both are
correct and if you want to store only homogeneous data then go with this kind of a syntax and this
will store only strings and if you want to store you can specify integer wer class or double wer
class or any type of object we can specify okay so these are the different type of declarations
we can do it all are correct now I want to store heterogeneous data is my requirement so you can
use this one or you can use this one anything is okay now I'm enabling the first one okay
this is the Declaration part how to declare the are list variable now the next item once you
created this array list there is no specific size for that okay we can store any number of objects
now in this array list how to add data so I want to add multiple objects right so next thing is
adding data into ARR list adding data into into array list so how we can add data into this array
list so whenever I create an object of this array list class this object can have certain number of
methods right so there is a method called my list dot add this is the method and by using this
add method we can add data into the array list so how we can add suppose I want to add numbers
right so I can still add number but it will store as a integer rapper class it will not store as a
primitive type similarly I can add some more data in the array list by using add method so I want
to add homogeneous data s heterogeneous data so what I will do is I will put let's say 10.5 is a
decimal number and here I will add string and here I will add one character okay all are taking
as a wrapper classes so even though if you're not passing some primitive type it will it will
convert into rapper class type so we can also add okay a is a character then I can add Boolean value
which is true or false Boolean and duplicates also allowed right so as per the array list definition
duplicates allowed so same 100 value again I will assign one more time I will add 100 and multiple
nulls also allowed so my list do add null okay and multiple nulls also allowed I will add one more
null so my list do add null okay so heterogeneous data I have added all kinds of data I have covered
so this is a way we can add data into array list now we use add method add is a method through
which we can add data into array list fine now if you want to find size of an array list size size
means what how many how much of data we have added to the array list so if you want to find the size
of an array list we have a method called size okay size of array list so to find the size of array
list we have a method called my list object dot size in case of arrays what is a how we can find
the length of an array size of an array array length array dot length length is a keyword here
we have a method called size so this will give you exactly how much of data we have in this array
list okay so here you can say size of an array list size of an array list okay so how much of
data we have stored this will give you that number and again if you want to print this array list the
data I want to print so you can simply print also you can say printing array list there are many
ways to print array list but straightforward is you can just specify the name of the object that's
it so here I can say printing data from ARR list okay I'm just added some data in the array list
then what I have done I have printed the size of the array list and then I have printed the data
from the array list so when I execute it this will give the same thing so if you just observe
size of the list what how many values are there eight total eight are there 1 2 3 4 5 6 7 8 which
is also calculated duplications so totally eight data we have added and size is eight so this is
returning eight and now you can see the uh data is also got printed in the form of list so this is
a data which is created inside the array list okay now let us see some more operations what else we
can do in the AR list data after printing suppose uh as of now I have these elements in the array
list so we also added duplic Ates so duplicates data is also added you can see duplicates are
allowed and multiple nulls also allowed or duplicate nulls also allowed erogeneous data is
allowed and also insertion order is preserved if I look at here in whichever order I have added the
data at the printing also done in the same order that means insertion is order also followed it
means indexing concept is followed so all points are satisfied now we'll see some more operations
on this array list suppose I want to remove some object from this array list there are multiple
items are there I want to remove one of the item or one of the element so how we can do is we can
call them as objects elements items and whatever you want it's basically representing some group
okay so now I want to remove some object from the array list remove or element from array list so
to remove some particular element from the AR list how we can remove it currently these are the items
I have eight items now I want to remove some item or I want to remove some element so then how can
we remove for example let's say based on the index we have to remove the element so we have to know
the index number so for example if I look at here this is 0o 1 2 okay 3 4 5 and so on now what I
want to do is I have found out two duplicates here 100 is repeated two times now I don't want to have
duplicate now I just want to remove this one okay the 100 I want to remove so second time when it
comes in which position it is there so 0o one 2 3 4 and five in the fifth position 100 is there that
I want to remove so what I can do is simply can say my list dot remove my list do do remove and
here we specify the index number let's say five that's it so this method will remove the value
or object which is there in the fifth position so after removing again I'm trying to print the
error list so this is a copy statement I'm again copying this so after removing Fifth Element
I'm again printing so here I'm writing after removing after removing I'm printing the
array list so you need to calculate from 0 1 2 3 4 something like this how to calculate
the index 0 it will start 0 1 2 3 4 okay now Java application now we can see this is a new list
after removing The Fifth Element so if you want to remove particular object from the ARR list you
have to know the index okay you cannot know the index based on the value you have to see the data
if you know what exactly the data is then only you can find the index okay so here we don't have
any formula like length length is not there first of all for array list so don't compare the things
guys if you learn something just learn that only individually don't compare with other things so
whenever you want to compare you need to compare with the similar things right for example you can
compare one apple with another type of apple but you cannot compare Apple with orange right so
the concepts are like that basically what you are doing is you are comparing apple with orange
no comparision possible array is different array list is different whatever options are there for
array they are not there in Array list whatever options are there for array list they are not
there for array so length is a related to array concept length meth is there that is related to
Strings concept here we have a size okay size is different size is related to array list so
there is no they are not comparable basically right so after removing the element from the
array list this is the latest list so now we can see 100 is 100 repeated only one time so
this is the latest list okay so 100 is once come only comes once and fifth position 100 is
got removed after applying this remove method so if you want to remove any element from the array
list we can directly call the remove method just by passing index and here five is index five is
a index of element particular element whichever element you want to remove you can specify that
index and index you need to count from zero 0 1 2 3 4 You need to count so this is a zero element
this is a one element this is a second element third element fourth element and Fifth Element
so we say five here that's the reason 100 is got removed okay now suppose I want to insert a new
element in the middle of somewhere in the middle of somewhere I want to insert let us say I want
to put some value uh before welcome and a between these two or between these two or between these
two somewhere in the middle of something I want to insert some value then how we can do it insert
element in the array list insertion is different adding is different okay remember this adding
means what nothing but what suppose here we have called method called add so add means what
these elements will be added one after another one after another I will tell you the difference
here listen carefully adding and insertion suppose you already have some objects in the releas like
this these elements already there and if you call add method of some value here okay and this will
be added at the end of the list and again if you call one more add method and allue add one more
value this will be added at the end of the list so whenever you call this add method along with
some value it will keep adding the values at the end of the list at the end of the list but my
requirement is I want to add value in the middle of the list somewhere in the middle of the list
then how we can add we use same add method but additionally we have to follow we have to provide
index so how to use this add method just we pass only value right but if you want inser the data
then what we can do is we say my list. ADD of a method overloaded method is available this
add method take only one parameter another add method will take two parameters the first
parameter should be an index second parameter should be a value now what happens this value will
insert exactly in this particular index or in this particular position so that's how we can insert
the value in the array list insertion is nothing but in the middle of the array list somewhere
we can insert the value okay suppose you have inserted a value then what happens to the rest
of the values for example let's say currently this is my array list okay suppose here x y j 1 2
some elements are there 0 1 2 3 4 these are these are the indexes right now in the second position I
have written method like this add second position and put some a I have written statement like
this in the second position I want to insert a then how to after executing the statement how
this array list becomes how this array list becomes so X is same same position y first
position same 0 1 2 3 4 5 and uh second position earlier we have a Jed in the second
position we have a j but in the second position which value we have inserted a now it becomes a
second position it will come a but rest of the three values will be move to the next positions
or will occupy the next positions that means J 1 2 these Valu Still Remains the Same the value
will not be deleted okay existing value Still Remains the Same just what happens between these
two so exactly in the second position we have added a here then what happens it will create
a new memory location here rest of the values will move next locations okay so after insertion
you will get the latest array list like this so insertion is nothing but what we can insert the
value in the middle of the list adding is nothing but what we can add values at the end of the list
okay you understand the difference between adding and insertion adding means at the end of the list
we can add insertion means at the middle of the list we can insert and what is the difference if
you provide only value here that will be added at the end of the list if you provide value along
with the index then the value will be inserted in appropriate index insertion will happen
Okay index will automatically update updated in the sense rest of the value will move to the
next positions and it will make free the second position and the new value will be inserted in
the second position so this is how it will work insertion okay now we'll see so as of now this
is my array list right now let us try to insert something in the middle of the list so I said
my list dot add this is overloaded method Okay add two parameters we have to pass and in which
position I want to add let's say second position uh I want to add something called Java okay
after insertion again I'm printing the list after insertion okay after insertion here this is
after removing this is a final list then after insertion again I'm printing the list just
observe yes now we can see this is the latest one now you just compare first element exactly
same no change second 10.5 10.5 same now we can see in the second position Java is occupied and
these values are moved to the next positions next positions are occupied so that's the reason it
is more Dynamic according to the data it will keep adding keep reducing the sizes so this is
how we can insert an element in the list adding insertion add means we can add data in the at
the end of the list insert means we can add data in the middle of the list this is one method of
suppose I want to modify the value modification change the value for example here I have added
Java now I don't want to have Java here instead of I want to make it as a python now I want to
change this data or modify the data then how we can do it modify element how to modify element in
the arror list just modification yeah it is not uh yeah it is we can also call it as replacement
yes replacement or modification or change whatever you want that's all are correct modification
in the sense what we can call it as a modify or replace or change okay now how we can do this
as of now these are the elements I have new list in this Java is in the second position that I
want to replace with the python or I want to modify this with the python then what you can do
is my list dot my list dot set is a method set and here we have to specify index and value so which
value you want to change Java what is the index of java two is an index and what is the new value
you want to replace with new value is python that I can specify okay so in the same second position
whatever existing value which we want to replace with the new value now after replacing again I'm
printing after modification or after replacing again I'm printing my list after replacing now
just observe this is the new list we got after replacing okay now we can see Java is
replaced with the python this is called replacement modification or change anything
you can call okay suppose I have these many elements as of now in my err now I want to
access specific object I want to retrieve the specific spefic object let's say I want
to extract this part I want to extract 10. or I want to extract python specific object how
to extract or specific element how to extract or access access specific element from array
list specific element so for example I want to access welcome then what is an index of
Welcome index number of Welcome is what 0 1 1 2 and three three is an index of Welcome three
is a index of welcome you need to count from zero 0 1 2 3 so my list dot there is a method
called get get and this will accept index number so we need to just pass the index number
index number so what is an index of this 0o 1 2 three so what is the problem guys can you see
my screen everyone okay fine so video lagging means your internet connection is slow you can
just check some from your side okay all right so my list do get off three so what is this
three three is a index number so what is what is the value which we have in the third place
0 1 2 3 welcome is there so the get method will return the object get method will return that
object so that I can print my list dot get of three you can print like this so get is a method
through which we can extract or we can access or we can retrieve the value or element from the
array list so we need to pass index so here three is index three is a index let's execute now
so this will give you the element which is there in the third position so this is return welcome
so how to access the value from the list by using get method specific value we can access that is
using get method and get method will accept index so if you know index then only we can extract
the value by using index we can extract the value okay if array is too long you can find
the size right dot size will give you total number how many number of indexes are there so
that's the only way we can count total number of index okay now after accessing now the next
thing is how to read all the values so there are multiple objects are there in my array list now
I want to read all of them then how we can read reading all the elements from arist so this is
the most important stuff there are many ways to read the data from the array list we can use a
normal classic for Loop we can use enhanced for Loop or for each Loop or we also have something
called iterator iterator is something new which is especially designed for collections okay by
using iterator iterator is basically called as a cursor and by using that iterator also we
can read the data from the collections all collections are supported so there are three
different ways to read the data from array list now I'll show you one by one listen very
carefully so the first approach reading all the data from the array list I'm not going to print
reading in a sense not like this when you put my list it will just print all the data but not
like this I want to get individual values all of them so the first approach using normal for
Loop using normal for Loop so what I can do is using normal for Loop how to read the data by
using normal for Loop so a list anyway follow the indexing concept right so what we can do is
we need to know starting point ending point and the condition how much value we have to increment
it is just like a simple normal array concept so how we will read the array by using normal for
loops similar way we can also read data from array list suppose this is my array list I have
some data 0 1 2 3 so we have to start from zero then we have to stop here and every time we have
to increase I value by one so let us see how to read the data from the array list simple for I'm
using for Loop directly and what is the starting point in I equal to 0 index start from zero and
where we should stop suppose if you don't know size of an array list so how we can get size of an
array list using size method so I less than I less than my list do size I less than my list dot size
because index is starting from zero you just say only less than is okay suppose if you still want
to put less than or equal to then what you should say you should say minus one here okay minus one
so why we have to do like this already explained in the array concept just you have to remember
this and if you don't specify minus one say only less than because we have to take from zero and
last index you start from one the last index will be n minus one so this is a mest do size and
how much value have to increment every time by one so I ++ okay now let us retrieve the data
from the err list so my list Dot get is a method available right we already seen what is the get
method will do get method will retrieve the data by using index so my list. getet I'm passing I as
an index and then semicolon that's it one single statement and that I'm going to print so system
print Ln and list.get we can put inside the print method and here inside this you can put my Lister
do get offi so this will retrieve the values one by one based upon the index and then it will print
by using print Ln right now execute yes now we can see we got all the values individually right 100
is a starting value so this is a value so we will able to read the data from ARR list using normal
Fun Loop this is one approach now let me show you another one we can also read data from ARR list
using for each Loop or enhanced for Loop using enhanced for Loop or for each Loop okay it is
very simpler than the first one here we don't need to use any indexing or nothing directly
write one for Loop and what is the name of the array list my list so from the my list I will
extract each individual value into a variable let me take some variable called X now tell
me what is a type of this x variable what is a type what is the type of X yes so if I look at
this from this array list we have different type of objects right heterogeneous data we have
so at the run time we don't know what type of data we are getting into X sometimes you will get
integers sometimes you will get decimal sometimes you will get Boolean different type of data you're
getting into the X in every iteration so X should be an object type then only it can hold all kinds
of data right and then I'll print the value of x simple so this is the way we can print the data
from the array list so if I look at here this is the data which we have got so from here it is
red so this is a data so by using enhanced for Loop we can also read the data from the array
list now the third approach is completely new for us so these two approaches we already know
earlier because we already work with arrays concept same thing we followed here also now the
third approach is this is specific to collections iterator by using iterator we can also read
the data from the error list next option using iterator yes I is a index number in the first Loop
we have read the data by using indexing so we need to follow the index here in the for each Loop we
don't need to do any index because for each Loop will automatically get the values 1 by one into
variable and then we are able to print it okay now now come to the iterator iterator is a Something
New Concept so far we have not learned because this iterator is purely related to collections
it will not work for any other concept only for collections all kinds of collections will support
we can use the seator for a releas hash map hash set for all kinds of collections we can use the
seator to retrieve the data from the collections now let us try to understand how to use this
iterator concept for array list how we can read the data from array list using iterator okay
so basically the iterator is a cursor in we say okay we can create one iterator variable like this
iterator it equal to my list dot we have a method called iterator so iterator is a method which is
already there in the array list okay iterator is a method which is already there in the error list
we can call that method so when you call this iterator method what this will return is this will
return something called iterator type of an object it will return something called iterator type of
an object so here we have to Define some variable let's say it and the type of the variable is what
iterator type okay the type of this variable is what iterator type and this iterator we have
to import from java.util package there are multiple packages are there you need to carefully
import only java.util okay now in this particular statement from the my list. iterator I just call
this iterator method and what this iterator method will return is iterator type of object so I have
created this variable with the iterator okay now we need to write something called while loop
whenever you are using iterator we prefer to use while loop okay why suppose if you know exactly
how much of data you have in a list okay then you can go with for each Loop or you can go with the
normal for Loop suppose if you don't know how much of data you have in Array list you don't know how
much of data you have in err list so what I can do is first I will check the data exist or not
if exist then I will get it and take and check another data is exist or not if exists then I will
take it so before getting the data I will check the data is exist or not okay so what we can do in
this we have to write one while loop okay in this from it object I will call one method called hash
has next has next what this method will do is this method will check the element is present or not
it will not follow indexing remember this iterator also will not follow any indexing this will also
itate the list one by one just like a for each Lo okay so has next is a method this will check
the first element is present or not if the first element is present then this will become true this
will return return true conditional statement if it is written true then from the same iterator we
will get the value by calling next method next so this method will get the exact value from the
list that I am going to print by using system do do print Ln I can go the print so has next and
has sorry has next and next these are the only two methods we have to remember so system. pelm it.
next so has a next method we'll check the next element is present or not if it is present then
get it value then again it will go up next again it will check the next element is there not yes if
it is exist then get the element how many times it will repeat till it reaches the end of the list
and after it reaches end of the list if it could not find any more elements then this condition
become false then it will automatically exit so when you run this just observe it will also
read all the data from The Collection can just see here so we got this data so by using iterate
also we can read the data from The Collection I will repeat once again listen very carefully there
is no break nothing it will automatically repeat everything till end of the array list as soon as
the elements are not found then this condition become false it will exist from the while loop
okay so why we prefer this while loop here because we do not know how much of data we have in this
array list that's the reason dynamically we are checking each and every element exist istance then
we are reading whenever it found no value in the ARR list then it will stop executing okay so has
next is a method through which we can check the element exist or not id. next is a method this
will get that particular element from the array list so before doing this we need to call one
method called iterator from the array list and that will return iterator type of variable so
we need to create this variable using iterator object these are again predefined so from this we
can access as next method e next okay so when you run this you will get all the data fine so now for
example I'm not writing any y Loop okay just what I will do is it. next it. nextt I'm just printing
it I'm not using any looping statement now tell me what is an output of the statement will it execute
first if it execute what is an output of the statement so what is an output of ID do next okay
so here we have not used any looping statement we are not checking existence or not has a next
method we are not doing anything so if you're not doing anything it will just return the first
value in the array list the first value in the array list which is 100 so which is retaining the
first value in the array list next time I want to get the second element third element fourth
element Fifth Element right so for that reason we have to use has next so has next method will
do two task one is it will go to the next element in each every iteration and check the existing
element or not that's how has next method will work for example first time I'm using it do has
next what it will do in the first round it will focus on the first element and if it is exist
or not if it is exist then it will become true and it will come inside and get this element
and next iteration has next method will check the next element present or not if it is present
the next method will retrieve that value in the next round of iteration again has the next method
we check the next element is present or not if it is present the next method will retrieve another
value now in the next iteration again has next method will check the next element is present or
not if it is present return that value so this is how has next method will work has next method will
repeat every iteration go to the next element in each and every round and after going it will
check the element existing or not so once it is reached end even this is also done now after
that it will check has an next it will go to the next position so no value is available then this
condition Falls and it will automatically exit so this is how we need to use iterator to get the
data from the array list is it clear everyone how to read the data by using iterator concept so
we need to First create first we need to call the method called iterator from the list Object
Store into variable the variable type is iterator and then by using while loop by putting has next
and next these are the two methods which we need to remember okay so has next and next by using
these two methods we can get the data so next me if I use next method alone it will return return
always first value okay but when I use next method inside the loop right then what happens this has
next method will go to each and every element one by one right so whichever element the has next
is focused that element will be written by the next method you understood the point so whichever
the element is focused by has next method that element will be returned by the next because has
next will navigate through array list that is a point here any confusion guys it is simple topic
actually unnecessarily are making complication so has the next method will go to each and every
element one by one now navigation is happened through the has next method next method will
capture the element whichever is there in that position so the com by combining has next and next
methods we will able to retrieve the data from the hash okay so array do not have values see simple
logic guys in Array I have stored some values this is the first value this is the last value right
so has next method first Focus here then here then here then here then here then after completion
of that is there any location is available no no right after completion of the value no no
element no location is there so obviously has next method will return false if the value is
present then only it will return true if the value is not present return false got my point so
here there is no question of uh having location or having element nothing as soon as you found
the last element the next no no element right so has next method will go to the next and check
no value so it will exit automatically it will exit yes it is just like a it is not similar to
the object actually iterator method we are calling from the my list my list is what a list object
from from the array list object we are calling iterator method so iterator method will return
the iterator type of variable okay iterator will return return iterator type of variable that's
the reason this variable which we created in iterator type iterator type so methods will return
something it's very simple concept guys observe very carefully suppose if I create one method
suppose M1 method I created if this method is returning string for example if this method is
returning string so when I call M1 method and I created one variable called X now tell me what is
the type of X what is the type of X string right why X is a string why X is a string because M1
method is returning string output string outcome that's the reason this variable the holding
variable should be string right this is clear everyone similarly in arror list we have a method
called iterator and this iter Ator method do something here and finally it will return iterator
type of object so this iterator is a written type so when you call this iterator method when
you call this iterator method from the my list array list object so what is the written type of
iterator method what is the type of this iterator method iterator is a type so this variable should
be iterator so are we Crea any object here are we creating any object no we are not creating
whenever you see new keyword that is a creating an object okay there is a difference storing the
object in a variable is different creating an object is different when I use new keyword that
will create an object here we are not creating any object we are storing the output in a variable
that variable type is iterator you understood now everyone all right so this is how we can specify
the iterator okay sometimes for example the my list is having homogeneous data homogeneous data
suppose my list having only strings only strings so then here we can specify some rapper class
called string if the iterator contains only strings suppose if the array list contains only
strings then you can specify rapper class as a string suppose my ARR list contains only integers
then we need to specify integer wrapper class if my collection contains only employee objects then
you have to specify the employee class if your collection contains heterogeneous data integers
strings booleans everything then it should be object okay so this is optional so specifying
this angular bracket is optional even though if you're not specifying still it works but even
though if you specify that there is no problem you understood my point so based upon the data
we have to specify proper uh type in the angular bracket so this is how we can use iterator to
read the data from the array list okay so yes this is depends upon the what type of objects
you have in the array list so when I object here it will allow us to store all kinds of data
so better to go with object itself okay now next item suppose I have some items in my array list
as of now what are the items I have so these are the elements or I have in my AR list so I want
to check my array list contains items or not is my array list is empty or not I want to check
okay so how we can check but before reading we will normally check right if the array contains
then we will read it so based on the condition we can do it so checking array list is empty or
not checking array list is empty or not how we can check my list dot is empty there is a method
called is empty so this will return true if it is empty returns true if it is not empty return
false now tell me the output of this true or false true or false so this will return array
list is empty or not is array list empty it will return yes yes or no it will return true or
false it will will it return false or true it will return false why because currently we have some
elements already present in Array list right so if there are no elements in Array list then only
it will return true so if I look at here it is written false because error list contains some
data okay now I want to remove I want to remove all the data from array list remove see earlier
we have seen how to remove specific value right by using remove method so by using remove method
we can remove specific value from the array list but if we want to remove all the objects from list
all the elements I want to remove then how we can remove remove all the elements from array list
how to remove all the elements at the same time at time I want to remove everything very simple
my list dot remove all remove all that's it here we can specify something called list so remove
all method we cannot remove all elements at a time but remove all method directly won't work
we need to pass some parameter here I'll tell you how to remove all the elements at a time so with
single statement it is not possible okay remove all method we remove all the stuff but before
that we need to do something so what we need to do is remove all method cannot remove all the
elements if you want to remove all the elements if you want to clear everything you have to call
clear method there is a method called clear that will remove but if you want to remove multiple
elements specific elements there are multiple things are there let me show you let's say this
is my array list and I have some elements a b c x y z so these are the Ms currently I have
suppose if you want to this is 0 1 2 3 4 and five now I want to remove specific element what
you will do remove and specify the index so index of C is what two and this will remove C this will
remove this particular element particular object suppose if you call clear method just so here my
list. remove my list. clear so when I say my list do clear all the elements will be cleared so it
will return empty nothing but my requirement is different my requirement is I want to remove
multiple values randomly for example I want to remove a b c or I want to remove a and X or
I want to remove B and Y how it is possible I don't want to write remove methods multiple times
okay I don't want to remove this method I want I don't want to repeat this remove method multiple
times if I want to remove a and C you can write two times mest do remove of zero mest do remove of
two two statements you can write two elements will be removed but I want to remove multiple elements
with the single shot then how it is possible okay first what you need to do is we have to create
a separate list okay I say my list two I will create a separate list my list and in the my list
what we need to do is we what are the elements we want to remove we have to put those elements in
that new list let me show you practically then you can understand so here I'm going to create
a new array list variable array list my list two equal to new array list and I created a new array
list variable just observe no remove method cannot allow two parameters only one parameter is allowed
that is index okay now ARR list my list to equal to this is a new array list I have created and in
this array list okay my list 2 do add so what are the elements you want to remove from the ARR
list those elements we need to to add to the new ARR list suppose I want to remove a so that
I can specify here a I want to remove my list to do add and what else you want to remove I want to
remove B also B you can specify and what else you want to remove my list 2. add X also I want to
remove so I can say x okay so what are elements you want to remove pick those elements and uh
create a new array list my list is different my list is already I created earlier and my list
contains these elements now my list two contains three elements okay my list two contains a b and X
now I want to remove a b x from the verginal list so what I can do now here is take the verginal
list my list dot remove all remove all because remove all will allows another type of colle as
a parameter that's the reason we have to pass this mileage to two here this is the statement so
what exactly Happening Here is from the my list my list is what this is the one original list from
this remove all what are all we have to remove whatever the elements are there in the my list two
those elements should be removed so what are the elements are there in the my list two a b x so
these three El elements will be removed from my list you understood the point this is an example
okay so what are all elements we have to remove we need to keep those elements in another array
list separate array list variable and that array list variable we have to pass now in our context
so what I will do is I will pass our data let's say 100 I want to remove from my array list and
then welcome also I want to remove from my array list these are the two values I want to remove
from my array list okay now let us understand this yeah there is no shortcut way this is the
only way we can remove all the elements at a time or else you can use Clear three scenarios I
told you right three scenarios I'm repeating the same thing again and again you have to focusly
listen see this is my array list I have multiple elements if you want to remove specific element
one element then what you should do remove method you can use remove of you can pass index
whichever element you want to remove you can pass index that will remove single element if
you want to remove all the elements every element then you can call clear method and this will
remove all the elements but these are not my requirement my requirement is what randomly
I want to remove here and there some element few elements not every element then you need
to follow this approach this approach is for what removing multiple elements randomly if you
want to remove all the elements straightforward clear method is available if you want to remove
single element then remove method is available if you want to remove multiple elements randomly then
you can go with this support no direct method is available got my point everyone understood these
are the three scenarios now in this scenario what I'm doing I have added these two elements in
the new array list variable that array list I passing as a parameter into remove all method
so after removing now let us print my list so after removing multiple elements I'm printing
my list okay now this iterator also I'm just commenting for now okay now just observe how
it is going to remove multiple elements okay now we can see after replacing these are the
latest list now after removing how many elements we remove two elements see 100 and welcome now
we can see in this list we cannot find 100 and welcome that means two elements are got removed
at a time so this is how we can remove multiple elements randomly so this is a workaround for
us there is no strip method available okay there is no Direct method available so this
is the work for us so in the line number 81 what we exactly we doing from the my list I'm
removing all which are there in the my list two so whatever we have added to the my list
two those elements we are removing from the my list right so whatever elements are there in the
my list to only those elements will be removed from the my list whatever elements we are there
in the my list to only those elements will be removed from the my list okay is this clear
everyone this is more than enough actually all right so if you want to remove all elements
for example then what you can do remove all the element remove all the elements or clear what
you can do simple my list dot clear that's it so after clearing right after clearing uh I'm
checking array list is empty or not so for that what you can do this statement I can use after
clearing I'm executing this now tell me what is an output of this statement now this time what's
an output of the statement true or false true this time it will return true because array list is
cleaned up so is array list is empty yes or no is the is empty true or false yes true so when
I execute it this will return true yes okay so these are all different type of operations we
can do with the array list everything is clear so far so that's it for array list concept
array list is a collection in which we can store heterogeneous data insertion order is
preserved duplicate elements are Al allowed multiple NS also allowed and we can do we can
add elements we can insert elements we can remove single element we can remove multiple elements
we can remove all the elements we can find size of an array list we can find array list array
list is empty or not so all these things we can check so once it is clear we cannot get back
okay all the elements are got cleared that's it once it is removed or once it is got
cleared we cannot get back those elements okay so can you please guys confirm in the chat
box is it clear to everyone how to work with a list it is very easy concept actually there is no
objectoriented programming nothing no conditions nothing it's very simple and straightforward
okay now let us move on to the next collection called hash set so if I look at here
the collection interface again extend into set collection and this the set
collection again implemented by what hash set class okay now let us understand
about hash set or set collection go back so what are the properties of
hash set let me put some points here hash set yeah so hash set is a class which is
implemented set interface hash set is a class which is implemented set interface so
what is the first point heterogeneous data allowed homogeneous data is also allowed this
is a common point between har list and hashmap hash set sorry hashmap is different okay this
is Hash set insertion order is not preserved in Array list insertion order is preserved
because it will follow indexing technique but here insertion order is not preserved because
it will not follow indexing technique it will follow hashing techniques that's the reason it
is starting with hash whenever you say hash the keyword that is following hashing technique okay
so insertion order not preserved that means what there is no indexing Concept in hash set so data
is represented with a different thing but 0 1 2 3 4 that kind of index is not there in the hash
set or collection index is not there that the reason in such order is not preserved means
what in whichever order you have inserted the data it will not store the data in the same
order that is the meaning of insertion order not pres we'll see practically duplicate
elements also not allowed but in Array list duplicate elements are allowed but in hash set
duplicate elements are not allowed that's the reason even multiple NS also not allowed only
one single null is allowed if you add one more null again it become duplicate nulls so duplicate
elements are not allowed so single null value is allowed heterogeneous data is allowed insertion
order is not preserved duplicate elements also not allowed multiple nulls also not allowed so
these are the attributes of has set collection okay now we'll see what are all operations we
can do in hash set and don't compare aist and hash set has set is having different methods
there are certain things are possible in has set but they are not possible in Array
list similarly there are certain things are possible in Array list but they are not
possible in haset remember yeah it is also Dynamic heterogeneous data you can keep adding
more data there is no limit for this yeah now let us see the Practical part has set is a class
which is implemented set collection remember the set interface inter perspective all these
concepts are most important hash set demo right so first part
declaration how to declare hash set declaration how to declare the hash set again
there are multiple ways we can do it so directly if you want to do using hash set my set equ Al
to new hash set this is the direct approach so object we created for hash set and reference
variable is also hash set you can import this hash set from java. util package this is one
so this will allow us to store heterogeneous data all kinds of data we can store and set my
set equal to new hash set is it correct or not set my set equ Al to new hash set is it correct or
not so set also interface we have to import from java. yes because this is a child class object
so we can store child class object in the parent class variable so this is also right notation
perfectly fine suppose these two notations will allow us to store heterogeneous data all kinds
of data we can store but now I want to restrict it to specific data homogeneous data I want to
store only homogeneous data then how we can do it we can do like this we can do Hash a set here
in the angular bracket you have to specify so what kind of data you want to have suppose I want to
have only strings then you can say string here and one more time here also before the bracket
also we have to specify string like this okay and even hash set also allows to create only
objects primitive types are not allowed in any of the collections okay collections I'm talking
about collections means primitive data is not at all allowed only object type of data so this is
another type of notation so there are multiple ways we can declare the has set variable depends
upon your requirement now I'm trying to enable the first one hash set myet equal to new hash set so
this will allow all kinds of data heterogeneous data is allow now so once we declare this hash
set how we can add the data into hash set so adding elements into hash set how to add same
method is available add method is available why because has set AR list comes from what list and
set list and set comes from what collection so there may be some common methods available right
chance is there so add method is available in the both the classes so by using add method we we
can add elements to the hash set now let me add something so add my set do add here I'm adding
100 similarly I will try to add some more values just observe very carefully most important point
I'm going to discuss here according to the has set duplicates not allowed multiple nulls also
not allowed duplicate number duplicate values are not allowed multiple NS also not allowed but
intentionally I'm giving some duplicate numbers and nulls also then what will happen we will
see so here I'll say decimal number and here I will see string variable here I will put a Boolean
variable because this is a heterogeneous data here I will put character type of data then here again
I'm doing duplication see observe so duplication so whatever value I have given here same value
I'm repeating but actually has a set not allowed duplicates but still I'm trying to add duplicate
we'll see what will happen and multiple nulls also not allowed but still I'm trying to give multiple
nulls this is one null there is another null okay see all kinds of data have added even duplicate
values also have added multiple nulls also added but now if you trying to print my set then what
will happen observe very carefully this this is most important see these are the elements I
have so with this we need to understand three points three point we need to understand see in
which order we have added the data what the first element 100 second element 10.5 but here what
is the first element null second element a that means what in whichever order we have added the
data it is not preserved in such an order it is not preserved in the same order it's not printed
we have added in different order it is printed in the different order that means insertion order
is not preserved second Point insertion order is not preserved next Point duplicate elements not
allowed null also not allowed multiple nulls not allowed if I look at here even though I'm trying
to add duplicate value or duplicate nulls but finally it is taken only one null and one value
100 see duplicates are automatically eliminated so this is the final set set collection so at
the time of adding even though if you added some duplicates duplicate nulls it will automatically
remove the duplicates and it will keep only the orinal values so this is how it will work okay
so three points we understood insertion order is Not presed Duplicate elements allowed duplicate
element not allowed multiple NS also not this is a default behavior of hash set collection insertion
order will not present in whichever order we have added same order you will not get okay so that is
a default Behavior set collection so now tell me when you will prefer set set collection when
you will prefer again depends upon the data suppose you have some data which is homogeneous
okay and which is also not having any duplicates and you do not have any nulls duplicate and
you no need to maintain the insertion order then you can prefer the hash set so depends upon
the nature of data we have to choose the right and appropriate collection type okay now tell me
you if you want to maintain the insertion order and which collection you will select you want to
maintain the insertion order in whichever order you have inserted in the same order I want to
maintain and which one you will prefer has set or array list array list is preferred now I have
I don't have any duplicates data I don't have any nulls then which one you will prefer has set
okay so depends upon the nature of data you have to choose the right type of collection either
ARR list or haset both are not preferable at the same time sometimes we prefer haset sometimes
we prefer haset AR list okay so now we'll see different type of operations on hash asset
so this is how just for printing has a setup okay no at the time of adding it will
not give you any error at the time of adding duplicates also it will not give
you an error but ultimately it will not take those values at the run time it will not
take those values because this value is a old value this is a new value so values are same
so it will keep only one it will compare both and will keep only one that's the automatic
mechanism okay now suppose this is my hash set I want to remove specific value from the
hash set I want to remove specific value from the hash set how we can remove it can anyone
guess how to remove the element from the set removing removing element how to
remove specific element from the haset is it possible or not how we have done in
Array list how we have done in Array list by using Remo method right remove and we pass
index number there by passing index we removed it but here we do not have a remove indexing
concept here we cannot count the values from 0 1 2 3 4 no indexing is not at all supported
here but then how we can remove the data myet do remove we directly specify the value not
an index okay suppose 10.5 is there I want to remove 10.5 so you can directly specify
that Direct Value have to specify okay so here 10.5 is the value not an index okay
it is not an index but in case of ARR list we pass index number according to the index
the values got removed but here we don't have indexing concept so we have to directly pass
the value in the remove method so this will remove the 10.5 then after removing again I'm
printing the data so this is after removing I will come to each and every point just have some
patience okay so I'm just printing my set just observe see now 10.5 is got removed so this
is the latest value in the set collection so we have to pass the value directly
not an index index is not supported now suppose I want to insert value in the
middle of something insertion inserting element inserting element is it possible or not
insertion is possible or not inserting is not possible in the hashing hashing if any collection
is using hashing technique in that insertion is not possible why it is not possible is there any
specific reason why insertion is not possible see if you have stor the values in the proper order
then insertion is possible if the elements are storing in the random order where you want to
insert right there is it's not possible so if the elements are there simple example I'll give
you guys few persons standing like this in the order in the particular order like this and the
new person can come and stand in between any of them right here here here suppose one person is
here another person is here another person here so the next person will come he can stand anywhere
there is no specific area and specific order right so that is the reason insertion is not possible
in the set collection insertion element is not possible not possible insertion is not only we
can add the elements at the end of the set but not possible to add inser retrieving the specific
value or access specific element accessing specific element how to access specific element
from the set collection accessing specific value from the set collection or retrieving the value
from the set collection so in Array list how we have done in Array list array list dot get method
we have used in the get method what we have passed index number right get method accept index number
based on the index it will capture the value but here we do not have indexing concept right so we
cannot extract specific value from the hash set so accessing specific element is also not possible
okay it is also not possible inserting element is not possible but accessing specific element
is also not possible but there is a alternative way is available indirectly or it is we can call
it as a workaround if you want to retrieve the values from 1 by one or if you want to retrieve
the specific value from the set we have another approach I'll tell you there is no direct approach
available so directly it's not possible if anybody ask you in interiew is there any is it possible
to access specific value from the hasch set no not at all possible but is there any workaround
available yes workaround is available let me show you that without using Loop only I'm not using any
looping looping statement anyway we can access but without looping statement how we can access let me
tell you direct element we cannot directly access okay that is sure but after converting this set
into ARR list we can access we can convert a set collection into array list collection array list
is a totally different collection we can convert a set collection to array list collection once you
converted we can access the values from array list easily right yes so let us see how to convert so
first part convert has set to array list let us do how to convert so to do this what we need to do
is we need to First create array list variable so I'm creating array list variable first array list
Al equal to new array list I have created an array list object in this we have to pass has set object
that's it so this array list we have to from java now what exactly we are doing is we are converting
the set collection into array list collection we are converting the set collection into ARR list
array list so how we can do it we just create an array list object and just pass set collection
object whatever set we created that object we need to pass now the AL is having same content or not
whatever is there in my set the same content is having in Al also right now if you just print Al
and see what it contains so when I write Al when I print array list now you can see these are the
elements so in Array list index possible or not in Array list indexing is possible or not yes so
obviously we can retrieve the specific value Al off which value you want to retrieve 100 I want
to retrieve so what's the index of 100 0 1 2 two is a index of 100 so Al do get of two because we
don't have we have a get method so a. get of two this is a work around for us so now we can extract
the specific value but we cannot directly access value directly from the set collection once you
convert it into array list then we can extra okay clear everyone so this is the alternative approach
if you're not able to retrieve the specific value from set collection is not possible that is sure
but once you convert this into array list we can get it this is indirect process and we haven't
used any looping statement also okay remember this fine now the next thing suppose uh I want to
read all the values using looping statement then how we can do this looping statement read all the
elements using looping statement again there are multiple ways we can use normal for Loop okay so
normal for Loop is not possible here let me tell you why read all the elements using enhanced for
Loop only for each Loop it is possible so why it is not not possible by using normal for Loop can
anyone tell me see I want to read all the data from the set collection using looping statement
what I'm saying is normal for Loop through the normal for Loop we cannot read the data from the
set why because there is no index in the normal for Loop we specify an index number right I equal
to z i less than or equal to size i++ basically what we are doing there we are referring the index
right but here we do not have an index so normal for Loop we cannot use when you read data from
the set collection and another two approaches are possible so by using for each Loop we can read
the data from the set collection now let me show you how we get very simple so we can just specify
my set and from the myet get each and every value in the variable create this as an object and just
print it that's it so this will print or this will read all the values from has set so this will
print all the values individually from has set you understood my point so so why classic for
Loop is not possible because classic for Loop in the sense we have to follow indexing starting
point ending point condition everything we have to specify but here we are not following any
indexing set collection doesn't support index so that is not possible okay so this is how we
can read data from set collection using for each Lo yes once you converted into array list in
the previous concept so once you converted set into array list once you have this array
list you can insert also once you get an array list you can put all the you can do
all the operations whatever operations we have done earlier in the array list all kinds
of operations you can do in Array list because I already converted but in the scri collection
directly we cannot do okay now let me show you another for Loop okay not for Loop by using
iterator how we can read the data from the set collection using iterator yeah now tell me
what is the first point how we can do iterator using iterator so what we need to do in the set
collection also we have an iterator method in the set collection also we have a iterator method we
have to call it now what type of variable it will return what type of variable it should be it
should be iterator type of variable okay and if the DAT contains if set contains a different
type of data that also you can specify in the angular bracket you can say object optional okay
and this iterator again we have to import from java. in right now by using this it we can get
the data and we need to write a one while loop again same process and what is a method available
in it ID dot has a next then ID dot next it will get the value system do do pintel and id. next
it do next that's it so this will get all the data from the hash set so by using iterator also
we can read the data from the hasset only thing is indexing because of indexing not possible
for Loop normal for Loop we cannot use okay this is how we can read the data from the set
collection and in the set collection we cannot remove multiple values okay we cannot remove
multiple values so that is the reason if you want to remove single value you can remove
directly by specifying the value but if you want to remove multiple values you need to write
multiple methods there is no shortcut way but you can clear all the values at a time so clearing
all the elements in hash set how we can do this simple myet dot clear so after clearing you can
just check elements are there or not I'm just printing my set after clearing I'm printing my
set now just If You observe there is no values in the set empty bracket it is returning and also
you can use e mty method also myet do e Mt it will return return true or false e empty yes or no
yes because we already removed all the values e empty method will return true so there are no
values in the has set so this is how we can work with the has set ARR list and has set so these
are all have uh some kind of similarities like heterogeneous data is allowed okay and methods
also some common methods are there add method is common remove method is a common okay get method
is not there in err list we have a get method but in has set we don't have a get method but some
similarities are there some different methods also there in the both the collection okay
so can we find size of an hash set hash set size size myet dot size so this
will give you size of an ass set okay this will give you the size of a hash set
how many values are there in the hash set will be return so this is the six elements are there clear everyone so this is all about hash
set ARR list and hash set so now we have another concept called hash map we'll discuss about
this now so before going to hashmap both are clear everyone list and set collections what
is list collection what is set collection when to use list when to use set what are
the differences what are the similarities and what is possible in the list collection
what is possible in set collection so you have to be more clear about these topics
okay now let us move on to the next one hashmap so let's finish this concept guys just
another 10 minutes I will take and we'll finish the hashmap so we cannot split this only one
Topic in the next session okay because we'll finish the Java today itself so this is one
small topic another 10 minutes I will finish it hashmap so hashmap is a totally different type
of thing so we cannot compare hashmap with list and set collection because hashmap will derived
from or implemented class of map interface map is an independent interface but other than map
and hashmap rest of all type of collections are derived from the collection interface but only
the hashmap is D from the map interface okay so in the map interface or map H map collection we
can store data in the form of key and value pairs key and value pairs the combination of key and
value is called as a pair okay the key uh and value combination is called as a pair so now
we'll see what kind of data you can store in the hashmap just try to observe this let's say
I have an employee ID and name let's say 101 ID some name 102 x 103 J so if you have data like
this these are the IDS of employee these are the names of employee so if you have this data
so this is combination of one single set ID and his name so these IDs are called keys and the
names are called values and the combination of one key and value is called as one where okay the
combination of the ID and the name the combination of one key and value is called one pair okay and
uh this is one pair of data this is another pair of data this is another pair of dat if you want
to store this kind of data we have to use hashm let me give another thing suppose I have a name or
I have a phone number and the name of the person phone number and person phone number and person
so this type of data also we can store in hashmap same thing again these are all keys and these
are all values and the combination of key and value is called one pair so this type of data we
can store in the hashmap another example let us say country and capital so so there are multiple
countries each country is having some Capital so key and value pair so country names are the keys
and capitals are the values okay these kind of data we can store in hashmap and one condition is
there whatever I'm calling as a keys those keys should not be duplicated that means one1 should
not be repeat again one2 should not repeat again the phone number of one person should not repeat
again the country here should not repeat again so the keys should not repeat again but the values
can be repeated the values can be duplicated yeah the username and passwords also we can store in
hash multiple usernames multiple passwords but you need to make sure Keys should be different
but the values can be duplicated so the X name can again repeat another employees also can have
same name right but IDs cannot be same IDs can be different the values can be duplicated
but but ID should be different the values can be duplicated so this is the rule we have to
remember when you're working with the hashmap so hashmap in the hashm we can store data in the
form of key and value pairs only key and value tily Associated we cannot insert only key or we
cannot add only key we cannot add only value if you want to add data in the hashmap we should
add key and value along with them similarly if you want to remove the data from hashmap Key and
value together it will remove and at the time of printing the hashmap also it will print together
key and value we cannot split them into two so this type of data we can store in the hash map
only key and value it is not table format it is not table table in the sense we have n number of
columns but here we don't have rows and columns one key associated with one single value another
key associated with one single value so this type of data we can store in the hashmap so when do
you use hashmap if your data is having key value page this kind of data you have then you need to
prefer hashmap so now let us see the some of the attributes of the hashmap how hashmap will work
what are the attributes so the first point hashmap is a class which is implemented map interface
hashmap is implemented map interface and data can be stored in the form of key and value pirs in
the hashmap data can be stored in the form of key and value PS and the key is always unique but we
can have duplicate values and because it is also following hashing technique insertion order is not
preserved indexing is not allowed okay so these are the points you need to remember and when you
come to the duplicates values can be duplicated but the keys cannot be duplicated okay these are
the points so now let us try to implement M this practically next class and here this is hashmap demo hashmap demo so first of all
declaration part how to declare the hashmap Declaration so declare the hashmap
we can simply do like asmap hashmap hm equal to new hashmap I'm just taking one variable
hm is a object variable so hashmap we have to import from java.util package okay now
observe very carefully so hashmap hm equal to new hashmap what is giving an error
here type java. do map cannot be resolved it is indirectly referenced from required
type java. do hashmap so what what I will do is hashmap hm equal to new hashmap let me
save it and try to import hashmap first from java.util okay so whenever you create an ash
normally this syntax should allow why it is not allowed okay uh the type java. u. map cannot be resolved
it is indirectly referenced from required type java. okay let me check this later so let me just
create hashmap like this hashmap hm equal to new hashmap okay so one thing we have to specify
guys whenever you create an ash map first we will identify the data right keys and values so
for example I have some data like this so I have some data like this employee ID and the name is
called employee ID some other name employee ID some other name I have data something like this
now if you look at here these are the keys and these are the values so what is the type of
all the keys what is the type number integer right so what we have to do is first we need to
identify the type of the the keys so here we have to put angular bracket and specify integer so the
type of the keys are integer now what is the type of the values type of the values are string so
comma string we have to put like this and same thing we have to put at the time of object
creation also this is mandatory step okay so this is how we need to specify it is giving
something called build path error let me fix [Music] this just a second Library yeah just a second guys I don't see any issue here uh new hashmap integer string bracket so just try to import the packages properly so
at run time my eclipse is not recognizing these packages properly okay so you can declare the
hashmap uh by using like this directly and it is allowed all types of keys all types of values
okay different type of keys different type of values allowed and if your data is very specific
like all the keys are integers all the values are string then you can explicitly specify specify
the integer and string and inte is what what keys type string is what values type same thing you can
specify here okay these are the multiple options which we have and here also in this declaration
also we can specify if you want so instead of hashmap you can simply say map also so for example
because map is a parent interface of hashmap right so simply you can say only map map equal to my
map map my map equal to new hashmap or you can say hashmap hm any variable name is fine so these
are all corrected definitions you can use any of the definitions okay so this is my declaration I
have created because I know what are the types of keys what are the types of the values integer
and string I'm taking so now let us see how to add pairs so here we have to call them as a pairs
pair is nothing but a combination of key and value adding PIR P so how to add PS to the hashmap so
take this hm and we have a method called put okay we don't have ADD method in the hashmap so we have
only put method so hm. putut and here two values we need to add one is key another one is value
let's say I'm adding one1 and here I'll say John so one is a Val one is a key first one is a key
second one is a value so similarly I will try to add some more PS in the hashmap so 102 103 104 I'm
also trying to add duplicate key so what I said at the first point duplicate keys are not allowed
unique key is a unique it's not duplicated values can be duplicated so what I'm doing here is I'm
trying to make keys a duplicate okay we will see what will happen so this is the way we can add
page now how we can print this let us print h hm let's say hm now we'll see how it is going to put
the values in the hashmap just observe see 101 is John 102 is a David 103 is Mary 104 is a squad
sometimes you may not get the same order because insertion order is not preserved in hashmap but as
of now we got the same order but sometimes we may get the same order so if I look at here this is an
output I got so what about the duplicate key see initially one1 value is what John one2 value
is what Scott and here one2 value is what to David what exactly we are doing initially one2
value is a Scot but here we are replacing one2 value to David so the old value is replaced with
the new value but here you have only one one Z2 entry there is only one one Z to entry so that's
the reason duplicate keys are not allowed if you put duplicate key the old value of is got replaced
with the new value so that's how it will work okay now if you want to find size of a hashmap how we
can find size so we can say size of hashmap sorry yeah hashmap so we can simply say size method is
available hm do size so this will give you size of a hashmap that means how many paids are there
totally four paids are there one because one is duplicated right so if I look at here 1 2 3 4 so
totally four pairs are there four pairs are there okay and suppose I want to remove one pair I want
to remove one pair one of the pair so how we can remove this so remove pair we can remove only pair
we cannot remove one key specific key one specific value if you want to remove the entire page should
be removed so hm dot remove of here we have to specify key based on the key the entire pair will
be removed okay for example uh let us say I want to remove Mary this this pair I want to remove
so if you want to remove this pair then I have to specify the key of that pair based on the key
everything is operated so this is a remove so here 103 is key of the pair and after removing again
I'm printing the hash map after removing pair sorry after removing pair I'm just printing
hm so when I want to remove the pair we have to pass the key so based on the key the pair will
be removed now we can see this is the latest pair in the hashmap so we can remove it suppose I want
to retrieve the I want to retrieve the value from the key value of the key how we can retrieve
access value of the particular key then how can we access hm. get method is there get off
here we have to specify again key suppose here David I want to access so here we have to pass
one Z 2 so here one2 is also a key one is also a key now after accessing we have to print this
right system do print Ln hm. get of12 so in 102 what is the value which we have is David so
when you run this it will give you David is an output so if you want to extract the specific
value we can pass the key in the get option okay and the next one suppose uh if you want to get
all the key Keys all the keys not only single key so when you pass key it will get the value
of the key now I want to retrieve all the keys from the hashmap only Keys get all the keys from
hashmap get all the keys from the hashmap how we can get system do pin hm dot there is a method
called key set key set so key set is a method it will return return all the keys in the form of
set so let me just print so you can see these are the keys so it will print all it will capture all
the keys in the form of set and similarly can we get all the values all the values from hashmap
hm dot values this is the method available so this will return only values from the hashmap
see we got only values so key set is a method which will return return only keys from the
hashmap values is a method which will return return only values from the hash now I want to
retrieve key and value both how we can retrieve I want to retrieve key and value in the key set
method return only Keys values method written only values now I want to get keys along with
values how we can get HM dot key set is a method okay sorry not key set entry set dot entry set
key set means it will return return only Keys values means it will return return only values
entry set means what it will return return Keys along with the values so when I execute this will
return everything see here this is an output Keys along with the values everything is got returned
okay these are the few operations we can do it REM operation we can do we can access a specific
value by using key and we can access all the keys as a list we can access all the values and
we can also all access keys and values by using entry set okay if you want to change the value
of the specific key then you can change by using put method itself see you already one2 Squad we
already put this into hashmap then if you want to change the how we can change again you have
to write one more put specify the same key and change the value so you will keep the latest value
that means it is basically called replacement is happening okay can we use different data type in
place key no here whatever the data we specified all the keys should have integer all the data all
the values should be string okay suppose if you create a hashmap like this you are not specified
any specific type for keys and values then you can store any type of data you can store okay
31 and 39 31 what we are doing after removing the pair we are just printing here but here we are
just uh getting the data so there is a difference actually here it is printing everything as a list
and here it is printing everything as a set there is a difference actually you can just notice here
curly bracet are representing this uh list this bracket is representing the set so there is a
difference okay now by using looping statement how we can retrieve the data from the hashmap So
reading data from hashmap and here normal F will not work because there is no indexing supported
in hashmap also so normal F will not work so there are only two ways we have one is by using
enhanced for Loop other one is by using iterator so now we'll see by using for each Loop how we can
read the data from the hashmap observe carefully most important see in the hashmap we have a key
and value pairs both just observe this is the data I have so this is one pair this is another
pair this is another pair now what we need to do is we have to read each and every individual pair
so let me put some something here hm do key set hm dot key set method what the key set method will
return key set it will return only keys right if you know the key can we get the value by using
that key yes or no yes if you know the key we can get the value from the key so what I'm doing
is hm. key set will return these values now now I will read each individual key into a variable
here I'm taking one variable called int and k k is a some variable name what exactly we are
doing here from the key set key set in the sense these are the keys I'm reading each individual key
let's say one1 first that will store in K the next iteration one2 will store in K next iteration
one4 will store in K right now by using this key can I get the value and just printing the key
here K and just give some space concatenation and hm dot get off K I'm passing see K means what key
hm. get of K means what value so get method will return the value by using this K right so now when
I execute this it will print key keys and values like this okay all the keys and all the values so
this is the approach we follow using for each Loop so if you want to read the data by using for each
Loop this is the approach this is more simpler and this is preferable approach but we can also use
iterator but it is little complicated let me show you because we need to use so many classes I'll
tell you using iterator so using iterator how we can do this so first of all we need to declare it
we need to declare the iterator so how to declare the iterator HM is my hashmap right hm dot so
we cannot directly call iterator method from hm okay we cannot call iterator
method directly from hm so what we can do is we can get only keys from
the HM how to extract only keys from hm key set only values if you want to extract
what is the method dot values now if you want to extract keys and values then entry set told
you three methods right so here key set means it will return return only Keys values means it
will return return only values entry set means it will return entry set this is one entry this
is another entry it is under form of set so so what I'm doing is first I'm getting the entry set
from the hashmap so this entry set is nothing but what we will get this one and on the entry set we
can apply iterator so here I'm calling iterator method basically I'm getting all the entry sets
and on the entry sets are in the set format so on that I can apply the iterator here now what
this will return what is the type of this return you can just check it here if you put the cursor
on iterator it will show you the return return type is what iterator of Entry integer string
this is a return type this is a return type so that you have to specify this is a return
return type so here you have to specify like this so now we need to in import this iterator
from java.util and uh then entry also we need to import entry also predefined class that
also we need to import java. map okay this is a return return type iterator of Entry integer
and a string this is a return return type okay so now we got an iterator so from this iterator
we can capture the page so now which Loop we have to use when I use iterator always we have to
prefer while loop okay remember so while and in this I will specify it do next again methods
are same it do has next if the next record is available then what we need to do we need to
extract that record by using which method which method next method right so here I'm using it dot
next so now tell me what is this ID do next will return this will return entry set this will
return an entry set so let me explain this let's start from this okay so currently hm. entry
set what is hm. entry set contains one1 some X one2 some y one3 something right so this is a how
we have data so entry set is nothing but this one this one this one all three things are there now
I'm calling it and everything is a form of set so set means this is one record second record and
third record and iterator this are first iteration second third now when is storing this into it
what is the type of this it iterator and what this iterator contains of integer string right
we have two values so that's the reason entry of integer integer okay now come here while loop
what is this ID contains ID contains this one this is it has next is nothing but first it will
focus on the first pair first pair and again it will come here when I it do next what this will
return it will return one entry single entry it will return and the next round second entry
will return third round next entry will be return every time the it. next will return the
entry entry is nothing but what the combination of key and value is called entry so what we
need to do now here we have to store this in a variable we can call it as a entry and the type
of this variable is entry entry this is the type of this variable okay again the data is fixed
right so the First Data is integer format the second one is a string format that is optional
again if you want to keep this you can keep or us no so it do next first it will check it do has
next the first is there or not yes available so it will get that pair the pair contains again two
values that's the reason the entry type should be entry now from this entry we have to extract key
and value how to extract key and value from this entry object simple entry dot get key is the
method this will get the key of the entry and concatenated concatenated I'm just saying plus
concatenated here from the same entry I'm getting the value from the same entry get value that's
it so has an next it will check the p is exist or not yes then it will extract the pair entry
is representing one pair so one that from that pair we are extracting the key from the same
pair we are extracting the value in the next round it will check another pair then again get
that pair then again extract the key and value so this will repeat multiple times till it completes
all the pairs from the hashmap now let us execute see this is the data we got so through iterator
also we can iterate the hash map this is another approach normal for Loop is not possible either
we can use enhanced for Loop or iterator okay so now the last option suppose if we want to clear
everything from the hashmap simply you can say hm do clear so this will clear everything so again if
you want to check whether it is empty or not you can say hm dot is empty so will it return return
true or false true or false true because already we cleared the hashmap here so simple we can
execute and we can true it so this is how we can work with the hash map so most important concept
do multiple rounds of practice in automation also we will try to use this concept especially when
you do data driven testing we use this concept and when you read the data from Excel sheet and
put that into hashmap and then hashmap data we will parameters to the automation script so there
are different scenarios are there we will see as part of automation testing okay so do practicing
array list hash set hashmap so these are the major collections most of the times widely using your
Automation and apart from there there are so many other type of collections also there and at this
point of time you don't need to learn all types of collection so if you learn and familiar with these
three collections are more than enough and if you are still interested to learn all other types of
collections I have created one new playlist or in my YouTube channel you can just go to this link
and you can find all types of collections okay so you can just go to this playlist and you can find
all the different type of collection so this is the playlist I have created almost 10 videos are
there and other type of collections also I have covered in this so if you're interested you can
go through this this is optional not mandatory okay so that's all for today's session I will
stop here we will continue in the next session
Get free YouTube transcripts with timestamps, translation, and download options.
Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact