44
55import java .lang .reflect .Method ;
66import java .util .ArrayList ;
7+ import java .util .Arrays ;
78import java .util .List ;
89
910interface Criterion <E > {
1011 boolean test (E s );
1112}
1213
14+ interface Something <E > {
15+ void doSomething (E e );
16+ }
17+
18+ class PrintSomething implements Something <Student > {
19+ @ Override
20+ public void doSomething (Student student ) {
21+ System .out .println ("> " + student );
22+ }
23+ }
24+
1325public class School {
14- public static <E > void showAll (List <E > ls ) {
26+ // public static <E> void showAll(Iterable<E> ls) {
27+ // for (E s : ls) {
28+ // System.out.println("> " + s);
29+ // }
30+ // System.out.println("---------------------------");
31+ // }
32+ public static <E > void withEvery (Iterable <E > ls , Something <E > someObject ) {
1533 for (E s : ls ) {
16- System . out . println ( "> " + s );
34+ someObject . doSomething ( s );
1735 }
18- System .out .println ("---------------------------" );
1936 }
37+
2038 public static <E > List <E > getByCriterion (Iterable <E > ls , Criterion <E > crit ) {
2139 List <E > res = new ArrayList <>();
2240 for (E s : ls ) {
@@ -34,15 +52,19 @@ public static void main(String[] args) {
3452 Student .of ("Sheila" , 3.9 , "Math" , "Physics" , "Quantum Mechanics" , "Astrophysics" )
3553 );
3654
37- showAll (roster );
55+ withEvery (roster , x -> System .out .println ("> " + x ));
56+ // withEvery(roster, (Student student) -> {
57+ // System.out.println("> " + student);
58+ // });
3859
3960 // show smart students
4061// showAll(getByCriterion(roster, ???));
4162
4263 // show unenthusiastic students
4364// showAll(getByCriterion(roster, ???));
4465
45- List <String > names = List .of ("Alex" , "James" , "Fred" , "Susan" , "Bill" );
66+ // List<String> names = List.of("Alex", "James", "Fred", "Susan", "Bill");
67+ List <String > names = Arrays .asList ("Alex" , "James" , "Fred" , "Susan" , "Bill" );
4668
4769 // show long names
4870// showAll(getByCriterion(names, ???));
0 commit comments