Created
October 22, 2020 19:28
-
-
Save viveknaskar/8715459921d92016a373a3f92b6da3f2 to your computer and use it in GitHub Desktop.
Counting collection using a traditional for-each iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.viveknaskar; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class CountingCollections { | |
public static void main(String[] args) { | |
List<String> list = new ArrayList<>(); | |
list.add("Bruce"); | |
list.add("Tony"); | |
list.add("Clark"); | |
list.add("Natasha"); | |
list.add("Sachin"); | |
int count = 0; | |
//Iterating over the whole list to find the name having more than 5 characters | |
for (String num : list) { | |
if (num.length() > 5) { | |
count++; | |
} | |
} | |
System.out.println("Total number of people whose names are more than 5 characters: " + count); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment