diff --git a/Questions.txt b/Questions.txt index 4763a57..25e1672 100644 --- a/Questions.txt +++ b/Questions.txt @@ -605,3 +605,28 @@ B) Exception at runtime C) Hello!, C-si: y is 300, P-i: y is 101, P-c: y is 401, C-i x: is 200 Goodbye! D) , P-si: x is 0, C-si: y is 300Hello!, P-i: y is 101, P-c: y is 401, C-i x: is 200 Goodbye! E) Hello!, P-si: x is 99, C-si: y is 300, P-i: y is 101, P-c: y is 100Goodbye! + +Q) Given: +interface StudentCriterion { + boolean test(Student s); +} + +and: +class Student { + public int getGpa() { return 0; } + public List getCourses() { return null; } +} + +Which are legal (individually): +A) StudentCriterion sc = Student s -> { return s.getGpa() > 3;}; +B) StudentCriterion sc = s -> { s.getGpa() > 3 }; +C) Object sc = s -> {return s.getGpa() > 3;}; +D) StudentCriterion sc = (s) -> s.getGpa() > 3; +E) StudentCriterion sc = s -> s.getCourses().size(); + +Q) Which are legal individually: +A) Function fss = s -> { System.out.println(s); }; +B) Consumer cs = s -> { System.out.println(s); }; +C) Supplier ss = (s1, s2) -> s1 + s2; +D) BiPredicate bps = (String s) -> { return s.length() > 0; }; +E) ToIntFunction tofs = s -> s.length(); diff --git a/src/main/java/lambdas/Demos.java b/src/main/java/lambdas/Demos.java new file mode 100644 index 0000000..2649844 --- /dev/null +++ b/src/main/java/lambdas/Demos.java @@ -0,0 +1,76 @@ +package lambdas; + +import java.util.List; +import java.util.function.ToIntFunction; + +interface StudentCriterion { + boolean test(Student s); +} + +class Student { + public int getGpa() { return 0; } + public List getCourses() { return null; } +} + +class SmartStudent implements StudentCriterion { + @Override + public boolean test(Student s) { + return s.getGpa() > 3; + } +} + +public class Demos { + public static void main(String[] args) { + StudentCriterion sc = new SmartStudent(); + // lambda MUST implement an interface + // ** context MUST make that interface clear + // and that interface MUST have ONE abstract method + // we MUST ONLY desire to provide implementation of THAT ONE method + // compiler will: + // write the class using this method + // instantiate it :) +// StudentCriterion sc2 = (Student s) -> { return s.getGpa() > 3; }; + // if the argument type is also inferrable, we can leave it out +// StudentCriterion sc2 = (s) -> { return s.getGpa() > 3; }; + // sometimes, we want to annotate the formal parameter +// StudentCriterion sc2 = (@Deprecated Student s) -> { return s.getGpa() > 3; }; + // this does NOT work (annotation goes with the TYPE not the formal param +// StudentCriterion sc2 = (@Deprecated s) -> { return s.getGpa() > 3; }; +// StudentCriterion sc2 = (@Deprecated var s) -> { return s.getGpa() > 3; }; +// StudentCriterion sc2 = (var s) -> { return s.getGpa() > 3; }; + // BUT, you must choose: + // All formal params, with no type, with explicit type, or with var + + // IF (but only if) you have a SINGLE formal param, with NO TYPE specifier + // we can leave off the parens (there's nothing to "group") +// StudentCriterion sc2 = s -> { return s.getGpa() > 3; }; + + // OFTEN, our method body simply returns an expression + // In which case we can leave out "clutter" + // curlies "group" stuff, but we have only one thing-- leave out + // return is unnecessary + // must do both changes at once :) +// StudentCriterion sc2 = s -> s.getGpa() > 3 ; +// StudentCriterion sc2 = s -> Boolean.valueOf(s.getGpa() > 3) ; + StudentCriterion sc2 = s -> { + Boolean b = null; + return b; + }; + + // CANNOT DO THIS! +// Object obj = (Student s) -> { +// return s.getGpa() > 3; +// }; + + + // statement expression, value we can ignore +// boolean b = List.of().add(0); +// List.of().add(0); +// 3 + 2;\ + + ToIntFunction tofs = s -> Integer.valueOf(s.length()); +// Long lng = 99; // Nope + Byte b = null; + long lng = b; + } +}