Practice: Method References

Practice: Method References#

Question #1: Examine the code below. Look at the method runExample to see three attempts to call add. Only one will succeed? Which one works?

// Recall how the Function interface is defined
public interface Function<T, R> {
   R apply(T t);
}

public class Quiz {
    public void add(Function<Fraction, Double> op) {
        Fraction f1 = new Fraction(1, 3);
        Fraction f2 = new Fraction(2, 7);
        Double sum = op.apply(f1) + op.apply(f2);
        System.out.println(sum);
    }
    public static void runExample() {
        add(Quiz::tryTwo);
        add(Quiz::threePee);
        add(Quiz::toDouble);
    }

    public static void tryTwo(Fraction f1, Fraction f2) { }
    public static Fraction threePee(Double d1) { }
    public static Double toDouble(Fraction f1) { }
}

Question #2: Write code to sort a List of Integers in its Natural Order.

Don’t write the sort method. Use a library and one line of code.

Question #3: Write code to sort a list of integers in reverse. Accomplish this in three different ways:

  1. Have the hosting class implement the Comparator that does the arithmetic directly.

  2. Use a method reference to a function that calls the Integer’s Comparable.compareTo.

  3. Use Collections.reverseOrder.

Question #4: Write code to sort an integer array by its 1’s digit.
For example:

[ 15, 20, 31, 46 ] -> [ 20, 31, 15, 46 ]
// One's digits in the sorted list are: 0, 1, 5, 6

Question #5: Write code to sort an array of Strings in the following four ways:

  1. Default sort order

  2. Reversing sort order

  3. Ignoring the case of the String

  4. Using this custom order that will reverse the String and also ignore the case

Note that a regular sort is case-sensitive and will result in Upper-Case letters coming first. Here is sample output:

Original: [Too Small, One, Two, Three, four, five, six, seven, Eight, Nine, ten, bigger]
Default:  [Eight, Nine, One, Three, Too Small, Two, bigger, five, four, seven, six, ten]
Reverse:  [ten, six, seven, four, five, bigger, Two, Too Small, Three, One, Nine, Eight]
No-Case:  [bigger, Eight, five, four, Nine, One, seven, six, ten, Three, Too Small, Two]
Custom:   [Three, Nine, One, five, Too Small, ten, seven, Two, bigger, four, Eight, six]

In the custom sort, see how we sorted by the reverse of the string:  
          [eerht, enin, eno, evif, llams oot, net, neves, owt, reggib, ruof, thgie, xis] 

Question #6: Write a method to sort an integer array using primary, secondary & tertiary sorting rules as follows:

  1. Primary: All positive numbers before all negative numbers. If signs are equal, use…

  2. Secondary: 10’s digits. If the 10’s digiti is the same, use…

  3. Tertiary: Reverse Natural Order (larger first).

For example, the array below shows how all the positive values come first. Then, we see that since [7, 3, 2] all have 0 as their 10’s digit, they come first. The order of these three number is due to 7 > 3 > 2. The last three numbers [-47, -49, -49] all have 4 as their 10’s digit, and -47 > -49.

Before: [36, -23, 25, -24, 3, -47, -39, -27, -49, 7, 2, 44, 28, -49, -1, -2, 45, -4, 20, 34]
 After: [7, 3, 2, 28, 25, 20, 36, 34, 45, 44, -1, -2, -4, -23, -24, -27, -39, -47, -49, -49]