CS/SWE 332 In Class Exercise Number 16

Name(s):

This exercise investigates how Java handles functions using a pair of common interfaces. It's a good precursor to lambda expressions.

  1. Define an implementation of Comparator that reverses the natural ordering implemented by Comparable. Exercise your implementation in the context of a Treeset.

    • Use a named class for your implementation.

    • Use an anonymous class for your implementation.


    Does this implementation satisfy the Comparator contract? Exactly what does that mean?

    Can you generify your implementations?

  2. Repeat the exercise with a Comparator<Integer> that uses absolute values.

  3. Repeat the exercise with a Comparator<Integer> that orders evens before odds.