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.
This exercise is part of the course
Input/Output and Streams in Java
Exercise instructions
- Convert each city name to uppercase letter using
.map()
. - Collect the result using
.collect()
. - Collect the result in a
Set
using predefined collect strategy.
Hands-on interactive exercise
Have a go at this exercise by completing this sample 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);
}
}