Skip to content
Snippets Groups Projects
Commit b7067cef authored by Stuart Marks's avatar Stuart Marks
Browse files

Minor updates based on comments from Maurice

parent 675bd055
No related branches found
No related tags found
No related merge requests found
...@@ -59,8 +59,12 @@ public class A_Lambdas { ...@@ -59,8 +59,12 @@ public class A_Lambdas {
} }
/** /**
* Write a method reference that is a predicate * Write an unbound method reference that is a predicate
* that tests whether a string is empty. * that tests whether a string is empty. An unbound method
* reference has a class name on the left-hand side of the ::
* operator:
*
* classname::methodname
*/ */
@Test @Test
public void a_predicate3() { public void a_predicate3() {
...@@ -150,7 +154,7 @@ public class A_Lambdas { ...@@ -150,7 +154,7 @@ public class A_Lambdas {
} }
/** /**
* Write a method reference that converts the * Write an unbound method reference that converts the
* given string to upper case. * given string to upper case.
*/ */
@Test @Test
...@@ -222,7 +226,7 @@ public class A_Lambdas { ...@@ -222,7 +226,7 @@ public class A_Lambdas {
} }
/** /**
* Write a method reference that clears the given list. * Write an unbound method reference that clears the given list.
*/ */
@Test @Test
public void c_consumer3() { public void c_consumer3() {
...@@ -287,7 +291,7 @@ public class A_Lambdas { ...@@ -287,7 +291,7 @@ public class A_Lambdas {
} }
/** /**
* Write a method reference that returns a new, empty StringBuilder. * Write an unbound method reference that returns a new, empty StringBuilder.
*/ */
@Test @Test
public void d_supplier3() { public void d_supplier3() {
...@@ -311,7 +315,7 @@ public class A_Lambdas { ...@@ -311,7 +315,7 @@ public class A_Lambdas {
BiFunction<String, String, String> bifunc = (s1, s2) -> s1 + s2 + s1; BiFunction<String, String, String> bifunc = (s1, s2) -> s1 + s2 + s1;
//ENDREMOVE //ENDREMOVE
assertEquals("abcdefabc", bifunc.apply("abc", "def")); assertEquals("FirstSecondFirst", bifunc.apply("First", "Second"));
} }
/** /**
...@@ -326,12 +330,12 @@ public class A_Lambdas { ...@@ -326,12 +330,12 @@ public class A_Lambdas {
BiFunction<String, String, Integer> bifunc = (s1, s2) -> s1.indexOf(s2); BiFunction<String, String, Integer> bifunc = (s1, s2) -> s1.indexOf(s2);
//ENDREMOVE //ENDREMOVE
assertEquals((Integer)3, bifunc.apply("abcdefghi", "def")); assertEquals(3, bifunc.apply("abcdefghi", "def").intValue());
assertEquals((Integer)(-1), bifunc.apply("abcdefghi", "xyz")); assertEquals(-1, bifunc.apply("abcdefghi", "xyz").intValue());
} }
/** /**
* Write a method reference that returns the index of * Write an unbound method reference that returns the index of
* the first occurrence of the second string within the first string, * the first occurrence of the second string within the first string,
* or -1 if the second string doesn't occur within the first string. * or -1 if the second string doesn't occur within the first string.
*/ */
...@@ -342,9 +346,18 @@ public class A_Lambdas { ...@@ -342,9 +346,18 @@ public class A_Lambdas {
BiFunction<String, String, Integer> bifunc = String::indexOf; BiFunction<String, String, Integer> bifunc = String::indexOf;
//ENDREMOVE //ENDREMOVE
assertEquals((Integer)3, bifunc.apply("abcdefghij", "def")); assertEquals(3, bifunc.apply("abcdefghij", "def").intValue());
assertEquals((Integer)(-1), bifunc.apply("abcdefghij", "xyz")); assertEquals(-1, bifunc.apply("abcdefghij", "xyz").intValue());
} }
// Hint:
// <editor-fold defaultstate="collapsed">
// The String.indexOf() method is called on the receiver and passes
// a single argument. This is like a method that has two arguments.
// The receiver becomes the first argument to the BiFunction, and
// the argument to indexOf() becomes the second argument to the
// BiFunction.
// </editor-fold>
/** /**
* Write a lambda expression that appends the 'suffix' * Write a lambda expression that appends the 'suffix'
...@@ -379,9 +392,9 @@ public class A_Lambdas { ...@@ -379,9 +392,9 @@ public class A_Lambdas {
Function<String, Integer> func = s -> "abcdefghij".indexOf(s); Function<String, Integer> func = s -> "abcdefghij".indexOf(s);
//ENDREMOVE //ENDREMOVE
assertEquals((Integer)2, func.apply("cde")); assertEquals(2, func.apply("cde").intValue());
assertEquals((Integer)4, func.apply("efg")); assertEquals(4, func.apply("efg").intValue());
assertEquals((Integer)(-1), func.apply("xyz")); assertEquals(-1, func.apply("xyz").intValue());
} }
// Hint: // Hint:
// <editor-fold defaultstate="collapsed"> // <editor-fold defaultstate="collapsed">
...@@ -392,7 +405,14 @@ public class A_Lambdas { ...@@ -392,7 +405,14 @@ public class A_Lambdas {
* Write a bound method reference that takes a string argument * Write a bound method reference that takes a string argument
* and returns the index of that argument into the string * and returns the index of that argument into the string
* "abcdefghij", or that returns -1 if the string argument * "abcdefghij", or that returns -1 if the string argument
* doesn't occur. * doesn't occur. A bound method reference has an instance,
* or an expression that evaluates to an instance, on the left-hand
* side of the :: operator:
*
* myObject::methodname
*
* This is in contrast to an unbound method reference, which has
* a classname on the left-hand side of the :: operator.
*/ */
@Test @Test
public void g_boundMethodRef2() { public void g_boundMethodRef2() {
...@@ -401,9 +421,9 @@ public class A_Lambdas { ...@@ -401,9 +421,9 @@ public class A_Lambdas {
Function<String, Integer> func = "abcdefghij"::indexOf; Function<String, Integer> func = "abcdefghij"::indexOf;
//ENDREMOVE //ENDREMOVE
assertEquals((Integer)2, func.apply("cde")); assertEquals(2, func.apply("cde").intValue());
assertEquals((Integer)4, func.apply("efg")); assertEquals(4, func.apply("efg").intValue());
assertEquals((Integer)(-1), func.apply("xyz")); assertEquals(-1, func.apply("xyz").intValue());
} }
// Hint: // Hint:
// <editor-fold defaultstate="collapsed"> // <editor-fold defaultstate="collapsed">
......
...@@ -20,7 +20,7 @@ import model.Person; ...@@ -20,7 +20,7 @@ import model.Person;
* getters. * getters.
*/ */
public class B_Comparators { public class B_Comparators {
final Person michael = new Person("Michael", "Jackson", 51); final Person michael = new Person("Michael", "Jackson", 51);
final Person rod = new Person("Rod", "Stewart", 71); final Person rod = new Person("Rod", "Stewart", 71);
final Person paul = new Person("Paul", "McCartney", 74); final Person paul = new Person("Paul", "McCartney", 74);
...@@ -118,7 +118,7 @@ public class B_Comparators { ...@@ -118,7 +118,7 @@ public class B_Comparators {
// <editor-fold defaultstate="collapsed"> // <editor-fold defaultstate="collapsed">
// Use the previous comparator and check the static methods of the Comparator interface. // Use the previous comparator and check the static methods of the Comparator interface.
// </editor-fold> // </editor-fold>
/** /**
* Write a Comparator that compares two people by age. * Write a Comparator that compares two people by age.
* Try to write the comparator so as to avoid boxing of primitives. * Try to write the comparator so as to avoid boxing of primitives.
...@@ -129,7 +129,7 @@ public class B_Comparators { ...@@ -129,7 +129,7 @@ public class B_Comparators {
//BEGINREMOVE //BEGINREMOVE
Comparator<Person> comparebyAge = Comparator.comparingInt(Person::getAge); Comparator<Person> comparebyAge = Comparator.comparingInt(Person::getAge);
//ENDREMOVE //ENDREMOVE
assertTrue(comparebyAge.compare(michael, rod) < 0); assertTrue(comparebyAge.compare(michael, rod) < 0);
assertTrue(comparebyAge.compare(paul, paul) == 0); assertTrue(comparebyAge.compare(paul, paul) == 0);
assertTrue(comparebyAge.compare(mick, jermaine) > 0); assertTrue(comparebyAge.compare(mick, jermaine) > 0);
...@@ -139,7 +139,7 @@ public class B_Comparators { ...@@ -139,7 +139,7 @@ public class B_Comparators {
// Look for static methods on the Comparator interface that // Look for static methods on the Comparator interface that
// have primitive specializations. // have primitive specializations.
// </editor-fold> // </editor-fold>
/** /**
* Write a lambda expression that compares two int values and returns an * Write a lambda expression that compares two int values and returns an
* int result that is less than, equal to, or greater than zero, like * int result that is less than, equal to, or greater than zero, like
...@@ -155,7 +155,7 @@ public class B_Comparators { ...@@ -155,7 +155,7 @@ public class B_Comparators {
// Avoid the following as it is susceptible to overflow: // Avoid the following as it is susceptible to overflow:
// IntBinaryOperator intCompare = (a, b) -> a - b; // IntBinaryOperator intCompare = (a, b) -> a - b;
//ENDREMOVE //ENDREMOVE
assertTrue(intCompare.applyAsInt(0, 1) < 0); assertTrue(intCompare.applyAsInt(0, 1) < 0);
assertTrue(intCompare.applyAsInt(1, 1) == 0); assertTrue(intCompare.applyAsInt(1, 1) == 0);
assertTrue(intCompare.applyAsInt(2, 1) > 0); assertTrue(intCompare.applyAsInt(2, 1) > 0);
...@@ -178,7 +178,7 @@ public class B_Comparators { ...@@ -178,7 +178,7 @@ public class B_Comparators {
//BEGINREMOVE //BEGINREMOVE
IntBinaryOperator intCompare = Integer::compare; IntBinaryOperator intCompare = Integer::compare;
//ENDREMOVE //ENDREMOVE
assertTrue(intCompare.applyAsInt(0, 1) < 0); assertTrue(intCompare.applyAsInt(0, 1) < 0);
assertTrue(intCompare.applyAsInt(1, 1) == 0); assertTrue(intCompare.applyAsInt(1, 1) == 0);
assertTrue(intCompare.applyAsInt(2, 1) > 0); assertTrue(intCompare.applyAsInt(2, 1) > 0);
...@@ -193,7 +193,7 @@ public class B_Comparators { ...@@ -193,7 +193,7 @@ public class B_Comparators {
interface DoubleToIntBiFunction { interface DoubleToIntBiFunction {
int applyAsInt(double a, double b); int applyAsInt(double a, double b);
} }
/** /**
* Write a method reference that compares two double values and returns an * Write a method reference that compares two double values and returns an
* int result that is less than, equal to, or greater than zero, like * int result that is less than, equal to, or greater than zero, like
...@@ -208,7 +208,7 @@ public class B_Comparators { ...@@ -208,7 +208,7 @@ public class B_Comparators {
//BEGINREMOVE //BEGINREMOVE
DoubleToIntBiFunction doubleCompare = Double::compare; DoubleToIntBiFunction doubleCompare = Double::compare;
//ENDREMOVE //ENDREMOVE
assertTrue(doubleCompare.applyAsInt(0.0, 1.0) < 0); assertTrue(doubleCompare.applyAsInt(0.0, 1.0) < 0);
assertTrue(doubleCompare.applyAsInt(1.0, 1.0) == 0); assertTrue(doubleCompare.applyAsInt(1.0, 1.0) == 0);
assertTrue(doubleCompare.applyAsInt(2.0, 1.0) > 0); assertTrue(doubleCompare.applyAsInt(2.0, 1.0) > 0);
......
...@@ -587,11 +587,11 @@ public class Exercises { ...@@ -587,11 +587,11 @@ public class Exercises {
"substantial"), "substantial"),
result); result);
} }
// Hint: // Hint 1:
// <editor-fold defaultstate="collapsed"> // <editor-fold defaultstate="collapsed">
// Use Stream.distinct(). // Use Stream.distinct().
// </editor-fold> // </editor-fold>
// Hint: // Hint 2:
// <editor-fold defaultstate="collapsed"> // <editor-fold defaultstate="collapsed">
// Use Comparator.theComparing(). // Use Comparator.theComparing().
// </editor-fold> // </editor-fold>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment