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,300 points
62 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 in Java by Sirajul
• 38,900 points
93 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 in Java by Wasim
63 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
567 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,050 points
843 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,190 points
186 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,880 points
62 views
0 votes
2 answers
0 votes
1 answer

How can I Sort an ArrayList in Java

You can sort the ArrayList in 2 ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,350 points
78 views
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,880 points
624 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
53 views