what are the ways in which a list can be iterated

0 votes
I want to iterate over a list to get the values of a elements of the list.
Apr 23, 2018 in Java by developer_1
• 3,350 points
1,098 views

1 answer to this question.

0 votes

 

There are 5 ways to iterate over a List:

1.  For Loop

for (int i = 0; i < pList.size(); i++) {
System.out.println(pList.get(i));
}

2. For-each Loop

for (String temp : pList) {
System.out.println(temp);
}

3. iterate via "iterator loop"

Iterator<String> pList = pList.iterator();
while (pList.hasNext()) {
System.out.println(pList.next());
}

4.while loop

int i = 0;
while (i < pList.size()) {
System.out.println(pList.get(i));
i++;
}

5. collection stream() util: A sequential stream is returned with this collection as its source.

pList.forEach((temp) -> {
System.out.println(temp); }
answered Apr 23, 2018 by sharth
• 3,370 points

Related Questions In Java

0 votes
2 answers

What are all the different ways to create an object in Java?

There are different ways you could do this ...READ MORE

answered Aug 19, 2019 in Java by Sirajul
• 59,230 points
1,157 views
0 votes
1 answer

What are the different ways of comparing Strings in Java?

The different ways of comparing string in ...READ MORE

answered Mar 5, 2019 in Java by Wasim
979 views
0 votes
1 answer

What are the differences between a HashMap and a Hashtable in Java?

There are several differences between HashMap and Hashtable in Java: Hashtable is synchronized, whereas HashMap is ...READ MORE

answered Dec 21, 2020 in Java by Gitika
• 65,770 points
1,039 views
0 votes
0 answers

What are the differences between a HashMap and a Hashtable in Java?

What are the differences between a HashMap and a Hashtable in ...READ MORE

Apr 26, 2022 in Java by Rahul
• 3,380 points
391 views
0 votes
2 answers

How to make a new List in Java?

Initializing a List in Java The Java.util.List is a child ...READ MORE

answered Dec 28, 2020 in Java by Thomas Walenta
783 views
0 votes
2 answers

Ho do I Iterate through a HashMap which contains duplicate values

for (Map.Entry<String, String> item : params.entrySet()) { ...READ MORE

answered Jul 24, 2018 in Java by samarth295
• 2,220 points
5,278 views
0 votes
2 answers

Performing Iteration over each entry in a 'Map'

In Java 8 you can do it ...READ MORE

answered Oct 24, 2018 in Java by Sushmita
• 6,920 points
1,094 views
0 votes
2 answers
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,920 points
1,905 views
0 votes
1 answer

How to count the number of occurrences of an element in a List?

We can use the static frequency() method. int ...READ MORE

answered Aug 21, 2018 in Java by sharth
• 3,370 points
5,654 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP