
Which of the following statements are true?
Icon.paintIcon method is abstract.Icon interface is a functional interface.IconTester class is coupled with the MarsIcon class.JOptionPane class is coupled with the MarsIcon class.What does this lambda expression do?
s -> s.equals(s.toUpperCase())
Consider this method:
public static <T> T largest(T[] values, Comparator<T> comp)
{
T result = null;
for (int i = 0; i < values.length; i++)
if (i == 0 || comp.compare(result, values[i]) < 0)
result = values[i];
return result;
}
What is the result of this call?
String[] words = { "Mary", "had", "a", "little", "zebra" };
String largestWord = largest(words, (s, t) -> Math.max(s.length(), t.length()));
"Mary""little""zebra"If you are totally mystified by lambdas, do this exercise. If you understand the basic idea, skip this and move on to the next slide.
JShell. Paste in the method from the preceding slide. Paste in the two lines below it. What happens?largest without a lambda expression. Make a class MyFriendlyComparator that implements an interface. Which interface?MyFriendlyComparator?largestWord?Put it all into JShell—the class that you defined, the instance of that class, and the call to largestWord.
import java.time.*; var ids = ZoneId.getAvailableZoneIds();
Etc/. Remove them by calling List.removeIf. How did you do that?<T> Collection<T> removeIf(Collection<T> original, Predicate<T> pred) {
ArrayList<T> result = new ArrayList<>();
. . .
return result;
}
and paste the code into JShell. Verify that you can call
removeIf(ZoneId.getAvailableZoneIds(), id -> id.startsWith("A"))
What is the code of your removeIf method?
Consider this call:
JOptionPane.showMessageDialog(. . ., new MarsIcon(50));
Draw a sequence diagram.
Consider this method:
public static Comparable largest(Comparable[] values)
{
Comparable result = null;
for (int i = 0; i < values.length - 1; i++)
if (i == 0 || result.compareTo(values[i]) < 0)
result = values[i];
return result;
}
and this call:
largest(countries);
where countries is defined as
Country[] countries = new Country[] {
new Country("Uruguay", 176220),
new Country("Thailand", 514000),
new Country("Belgium", 30510) };
Repeat with this method
public static T <T> largest(T[] values, Comparator<T> comp)
{
T result = null;
for (int i = 0; i < values.length - 1; i++)
if (i == 0 || comp.compare(result, values[i]) < 0)
result = values[i];
return result;
}
and this call:
largest(countries, new CountryComparatorByName());
