While programming array is essential to store list of fruits, list of employee, list of states and many more. Array has limitation and slow to travers through when the list of items are getting bigger and dynamically incresing.
AraryList in Java supports to store N numbers of items. After storing those items in the list, it needs iteration to find item which are available in the Array. We need to loop it one by one to fetch the required item.
Here are ways to Iterate or Loop List in Java.
1. For Loop
This is a basic for loop which we learn in Programming 101.
--Using Foor Loop--
Actron Name:-> Robert Downey, Jr.
Actron Name:-> Tom Hanks
Actron Name:-> Leonardo DiCaprio
Actron Name:-> Will Smith
Actron Name:-> Tom Cruise
Actron Name:-> Dwayne Johnson
This code uses for loop which uses size() to get the available items in the List and going one by one to get the items from the list and print on the console.
--Using Next Generation Foor Loop--
Actron Name:-> Robert Downey, Jr.
Actron Name:-> Tom Hanks
Actron Name:-> Leonardo DiCaprio
Actron Name:-> Will Smith
Actron Name:-> Tom Cruise
Actron Name:-> Dwayne Johnson
--Using While Loop--
Name:-> Robert Downey, Jr.
Name:-> Tom Hanks
Name:-> Leonardo DiCaprio
Name:-> Will Smith
Name:-> Tom Cruise
Name:-> Dwayne Johnson
--Using Iterator with While Loop--
Name:-> Robert Downey, Jr.
Name:-> Tom Hanks
Name:-> Leonardo DiCaprio
Name:-> Will Smith
Name:-> Tom Cruise
Name:-> Dwayne Johnson
--Using ListIterator with While Loop--
Name:-> Robert Downey, Jr.
Name:-> Tom Hanks
Name:-> Leonardo DiCaprio
Name:-> Will Smith
Name:-> Tom Cruise
Name:-> Dwayne Johnson
-- Using Iterable.forEach() --
Actor Name:-> Robert Downey, Jr.
Actor Name:-> Tom Hanks
Actor Name:-> Leonardo DiCaprio
Actor Name:-> Will Smith
Actor Name:-> Tom Cruise
Actor Name:-> Dwayne Johnson
-- Using Stream.forEach() --
Name:->Robert Downey, Jr.
Name:->Tom Hanks
Name:->Leonardo DiCaprio
Name:->Will Smith
Name:->Tom Cruise
Name:->Dwayne Johnson
Sharing is caring!