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.
All the necessary util and stream classes have been imported from you.
This exercise is part of the course
Input/Output and Streams in Java
Exercise instructions
- Convert each city name to an uppercase letter using
.map(). - Collect the result using
.collect(). - Collect the result in a
Setusing a predefined collect strategy.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
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 in a Set
.____(____);
System.out.println(uniqueUppercaseCities);
}
}