java · level 3 · unit 2 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A points at a method that already exists.
Many lambdas only pass their value straight to one method: text -> text.toUpperCase(). A method reference says the same with less noise: String::toUpperCase. Read the double colon as "use this method here". System.out::println works the same way for printing.
Sorting is where they shine. sorts films by year. Add a tie-breaker with , which only matters when two years match, and flip an order with reversed().
Read a chain from left to right like a sentence: compare by year, then by title.
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Main {
static class Film {
private String title;
private int year;
Film(String title, int year) {
this.title = title;
this.year = year;
}
String getTitle() { return title; }
int getYear() { return year; }
}
public static void main(String[] args) {
List<Film> films = new ArrayList<>(List.of(
new Film("Up", 2009), new Film("Coco", 2017),
new Film("Cars", 2006), new Film("Avatar", 2009)));
films.sort(Comparator.comparing(Film::getYear).thenComparing(Film::getTitle));
films.forEach(film -> System.out.println(film.getYear() + " " + film.getTitle()));
List.of("Roll", "the", "credits").forEach(System.out::println);
}
} Method references work best in chains like this, where a lambda would leave Java unsure of the type.
guess first
Where does reversed() reach? Guess first.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Main {
static class Player {
private String name;
private int points;
Player(String name, int points) {
this.name = name;
this.points = points;
}
String getName() { return name; }
int getPoints() { return points; }
}
public static void main(String[] args) {
List<Player> table = new ArrayList<>(List.of(
new Player("Bea", 30), new Player("Al", 30), new Player("Cy", 10)));
table.sort(Comparator.comparing(Player::getPoints).thenComparing(Player::getName).reversed());
table.forEach(player -> System.out.println(player.getName()));
}
}
your turn
Your turn. Rank the cities.
On line 19, sort the cities by population, biggest first. Use Comparator.comparing with a method reference, then reversed().
output
press run to see what happens
fix it
The bookshelf is only half sorted. Can you fix it?
output
press run to see what happens
- The authors are in order, but their books are not. What happens when two authors match?
comparingsorts by one key only, so ties keep the order they arrived in.- Add
.thenComparing(Book::getTitle)afterComparator.comparing(Book::getAuthor)on line 19, then run.
nailed it
you can sort by anything, in any order. :D
Class::methodis a lambda that calls one existing method.comparingsorts by a key;thenComparingbreaks the ties.reversed()flips the whole comparator before it.
new words in your collection
- method reference
- Comparator.comparing
- thenComparing
This lesson isn’t saved. A free account remembers every lesson you finish.