Convert and collect unique cities
A travel agency stores city names in lowercase. Before displaying them in brochures, all city names need to be converted to uppercase and stored in a Set to ensure uniqueness.
Cet exercice fait partie du cours
Input/Output and Streams in Java
Instructions
- Convert each city name to uppercase letter using
.map(). - Collect the result using
.collect(). - Collect the result in a
Setusing predefined collect strategy.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class CityProcessor {
public static void main(String[] args) {
List cities = List.of("paris", "london", "new york", "paris");
Set uniqueUppercaseCities = cities.stream()
//Convert each city name to uppercase
.____(city -> city.toUpperCase())
// Collect the result and store into a Set
.____(____);
System.out.println(uniqueUppercaseCities);
}
}