public static double average(BankAccount[] objects)
{
double sum = 0;
for (BankAccount obj : objects)
{
sum = sum + obj.getBalance();
}
if (objects.length > 0) { return sum / objects.length; }
else { return 0; }
}Country objects?public interface Measurer
{
double getMeasure(Object obj);
}public static double average(Object[] objects, Measurer meas)
{
double sum = 0;
for (Object obj : objects)
{
sum = sum + meas.getMeasure(obj);
}
if (objects.length > 0) { return sum / objects.length; }
else { return 0; }
}
public class BankAccountMeasurer implements Measurer
{
public double average(Object obj)
{
BankAccount account = (BankAccount) obj;
return account.getBalance();
}
}BankAccountMeasurer accountMeas = new BankAccountMeasurer(); double averageBalance = average(accounts, accountMeas);
Complete this program to compute the average length of the words that are entered in the input line. For example, if the input was
Bonjour le monde!
the program should print 5.0 (since (7 + 2 + 6)/3 = 5).
Make a class that implements the Measurable interface. Make an object of that class. Pass it to the average method.
average is too much work(Object obj) -> ((BankAccount) obj).getBalance()
-> symbolMeasurer accountMeas = (Object obj) -> ((BankAccount) obj).getBalance();
double averageBalance = average(accounts, (Object obj) –> ((BankAccount) obj).getBalance());Then the parameter variable is initialized with the object
Complete this program to compute the average length of the words that are entered in the input line. This time, use a lambda expression.
You'll need to cast obj to the appropriate type and apply a method.
Object with Measurablepublic interface Measurer<T>
{
public double getMeasure(T obj);
}public static <T> double average(T[] objects, Measurer<T> meas)
{
double sum = 0;
for (T obj : objects) ...
...
}
Measurer<? super T>, but we'll ignore that for now.obj -> obj.getBalance() // Instead of (Object obj) -> ...
average method:
BankAccount[] accounts = ...; double averageBalance = average(accounts, obj –> obj.getBalance());
accounts is a BankAccount[], T must be BankAccount...T, the type of obj is inferred.(v, w) -> v.length() - w.length()
{, }, return:
(v, w) ->
{
int first = v.length();
int second = w.length();
return first - second;
}
Put type inference to work by completing this program. Use a lambda expression that doesn't have a parameter type.
The Collection<T> interface has a method removeIf that accepts a Predicate<T>, a functional interface for a function T -> boolean. All elements for which the function is true are removed.
Complete this program to remove all elements with length < 20 from a list of words.
Complete this program to keep only the words that start and end with the same letter (ignoring letter case). Use a lambda expression of the form ... -> { ... }. Hint: substring, equalsIgnoreCase.
One use for a Map<String, Integer> is to count how often a word occurs in a text. It is a bit tedious to deal with the special case of inserting the first value.
Integer count = frequencies.get(word); // Get the old frequency count
// If there was none, put 1; otherwise, increment the count
if (count == null) { count = 1; }
else { count = count + 1; }
frequencies.put(word, count);
Java 8 adds a useful merge method to the Map interface. You specify
The function receives the old map value and the “not yet present” value, and it returns the new map value.
frequencies.merge(word, 1, (oldValue, value) -> oldValue + value);
The merge method is also useful if the map values are sets or comma-separated strings.
Complete this program that tracks all lines on which a given word occurs. Remove the if statement and instead use a call to merge.
Do the same in this program. Note that it's not quite so easy. You have two choices. Your function can compute and return the union of two sets. Or you can use merge just to make sure there is a set, and then call index.get(...).add(...).