Question: Here it is. thanks.////// interface UnaryPredicate { public boolean test(T obj); } final class Algorithm { public static int countIf(Collection c, UnaryPredicate p) { int

 Here it is. thanks.////// interface UnaryPredicate { public boolean test(T obj);
Here it is. thanks.//////
interface UnaryPredicate {
public boolean test(T obj);
}
final class Algorithm {
public static int countIf(Collection c, UnaryPredicate p) {
int count = 0;
for (T elem : c)
if (p.test(elem))
++count;
return count;
}
}
class OddPredicate implements UnaryPredicate {
public boolean test(Integer i) {
return i % 2 != 0;
}
}
class Test {
public static void main(String[] args) {
Collection ci = Arrays.asList(1, 2, 3, 4);
int count = Algorithm.countIf(ci, new OddPredicate());
System.out.println("Number of odd integers = " + count);
}
}

EXERCISE Change this line: int count = Algorithm.countIf(ci, new OddPredicate()); 1. To use Anonymous class, and use it. 2. To use Lambda Expression, and use it. class OddPredicate implements UnaryPredicate { public boolean test(Integer i) { return i % 2 != 0; class Test { public static void main(String[] args) { Collections Integer> ci = Arrays.asList(1, 2, 3, 4); int count = Algorithm.countIf(ci, new OddPredicate()); System.out.println("Number of odd integers = " + count); EXERCISE Change this line: int count = Algorithm.countIf(ci, new OddPredicate()); 1. To use Anonymous class, and use it. 2. To use Lambda Expression, and use it. class OddPredicate implements UnaryPredicate { public boolean test(Integer i) { return i % 2 != 0; class Test { public static void main(String[] args) { Collections Integer> ci = Arrays.asList(1, 2, 3, 4); int count = Algorithm.countIf(ci, new OddPredicate()); System.out.println("Number of odd integers = " + count)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!