Arrays.sort(array) sorts the given array...Comparable interfaceComparator interface
public interface Comparator<T>
{
int compare(T a, T b);
}
compare method returns
a comes before ba comes after ba and b are equal, or if their order doesn't matterComparator was cumbersome.
Comparablecompare methodArrays.sort(words, (a, b) -> a.length() - b.length());
Complete this program to sort an array of strings by decreasing length.
Arrays.sort(words, (a, b) -> a.length() - b.length());
Arrays.sort(accounts, (a, b) -> v.getBalance() - w.getBalance());
Double.compare:
(a, b) -> Double.compare(v.getBalance(), w.getBalance())
Comparator.comparing yields such a comparator:
Arrays.sort(words, Comparator.comparing(w -> w.length()));
Complete this program to sort a collection of countries by population and by population density (people per square kilometer).
Use Comparator.comparing(...).
public static <T> Comparator<T> comparing(Measurer<T> meas)
{
return (a, b) -> Double.compare(
meas.getMeasure(a), meas.getMeasure(b));
}
Measurer but a Function
public interface Function<T, R>
{
R apply(T arg)
}
static <T,U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T,? extends U> measure)
Complete this program to provide a higher order function Comparators.reverse that reverses a comparator. For example, if comp sorts strings by increasing length, then Comparators.reverse(comp) should sort by decreasing length.
filter consumes function
public static <T> List<T> filter(List<T> values, Predicate<T> p)
{
List<T> result = new ArrayList<>();
for (T value : values)
{
if (p.test(value)) { result.add(value); }
}
return result;
}Predicate is a standard functional interface:
public interface Predicate<T>
{
boolean test(T arg);
}List<String> longWords = filter(wordList, w -> w.length() > 10);
"and"List<String> andWords = filter(wordList, w -> w.indexOf("and") >= 0);
public static Predicate<String> contains(String target)
{
return s -> s.indexOf(target) >= 0;
}
Predicate:
List<String> andWords = filter(wordList, contains("and"));contains is also a higher-order functionComplete this program to provide a higher order function Words.longerThan that yields a predicate testing whether a string has length > n.
Note the nifty methods and and negate of the Predicate interface. In Java 8, it is legal to have “default” methods in an interface. Check out their implementations here.
::methodName
String::toUpperCase
(String w) -> w.toUpperCase()
String::compareTois the same as
(String s, String t) -> s.compareTo(t)
Double::compareis the same as
(double x, double y) -> Double.compare(x, y)
:: symbol:
System.out::printlnis the same as
x -> System.out.println(x)
newBankAccount::newis equivalent to a lambda expression that invokes the
BankAccount constructor.() -> new BankAccount()or
b -> new BankAccount(b)
String[]::new
(n: int) -> new String[n]
Comparator.comparing makes a comparator from an extractor function:
Comparator<String> comp = Comparator.comparing(t -> t.length())
Comparator<String> comp = (v, w) -> v.length() - w.length();
Comparator.comparing(String::length)
thenComparing:
Collections.sort(countries,
Comparator.comparing(Country::getContinent)
.thenComparing(Country::getName));
Complete this program to sort the list of words first by the number of vowels, then in alphabetical order. Use Comparator.comparing and two method expressions.
The Collection<T> interface has two toArray methods:
Object[] toArray() <T> T[] toArray(T[] a)
The first one returns an array of type Object, which is unsatisfactory. The second one requires the caller to provide an array and uses reflection to grow it if it was too short. Until the invention of constructor expressions, there was no better way.
In this program, there is a method that allows you to pass a constructor expression. Call that method to turn a List<String> into a String[].
In this program, reimplement filter so that it works with arrays.