import java.util.
ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class GenericCounter
{
// Utility method to check if a number is even
public static boolean isEven(Integer num)
{
return num % 2 == 0;
}
// Utility method to check if a number is odd
public static boolean isOdd(Integer num) {
return num % 2 != 0;
}
// Utility method to check if a number is prime
public static boolean isPrime(Integer num) {
if (num <= 1) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) return false;
}
return true;
}
// Utility method to check if a number is a palindrome
public static boolean isPalindrome(Integer num) {
String str = num.toString();
return str.equals(new StringBuilder(str).reverse().toString());
}
// Generic method to count elements based on a specific property
public static <T> int countElements(List<T> collection, Predicate<T> property) {
int count = 0;
for (T element : collection) {
if (property.test(element)) {
count++;
}
}
return count;
}
public static void main(String[] args) {
// Example collection of numbers
List<Integer> numbers = new ArrayList<>();
numbers.add(27);
numbers.add(252);
numbers.add(131);
numbers.add(44);
numbers.add(333);
numbers.add(111);
numbers.add(985);
numbers.add(05);
numbers.add(13);
numbers.add(87);
numbers.add(78);
numbers.add(78);
numbers.add(99);
// Count elements based on different properties
int evenCount = countElements(numbers, GenericCounter::isEven);
int oddCount = countElements(numbers, GenericCounter::isOdd);
int primeCount = countElements(numbers, GenericCounter::isPrime);
int palindromeCount = countElements(numbers, GenericCounter::isPalindrome);
// Output the results
System.out.println("Even numbers: " + evenCount);
System.out.println("Odd numbers: " + oddCount);
System.out.println("Prime numbers: " + primeCount);
System.out.println("Palindromes: " + palindromeCount);
}
}
/*Output
Even numbers: 4
Odd numbers: 9
Prime numbers: 3
Palindromes: 7