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,310 points
252 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,350 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,030 points
281 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
220 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,870 points
119 views
0 votes
1 answer

How can a war file be deployed in Tomcat 7?

You can access your application from: http://localhost:8080/sample Deploying ...READ MORE

answered Apr 20, 2018 in Java by sophia
• 1,400 points
1,306 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
57 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
1,424 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,890 points
204 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,890 points
882 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,350 points
3,557 views