Skip to content

Instantly share code, notes, and snippets.

@viveknaskar
Created October 22, 2020 19:28
Show Gist options
  • Save viveknaskar/8715459921d92016a373a3f92b6da3f2 to your computer and use it in GitHub Desktop.
Save viveknaskar/8715459921d92016a373a3f92b6da3f2 to your computer and use it in GitHub Desktop.
Counting collection using a traditional for-each iterator
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