Skip to content

How to realize this method only with Streams?

    /**
     * Get all cities near current city in radius.
     *
     * @param cityName - city
     * @param radius   - radius in kilometers for search
     * @throws IllegalArgumentException if city with cityName city doesn't exist.
     */
    public List<String> getCitiesNear(String cityName, int radius) {

        List<String> nearCities = new ArrayList<>();

        List<Integer> distances = new ArrayList<>();
        for (int i = 0; i < this.citiesNames().size(); i++) {
            distances.add(this.getDistance(cityName, this.citiesNames().get(i)));

        }

        for (int i = 0; i < distances.size(); i++) {
            if (distances.get(i) <= radius) {
                if (!this.citiesNames().get(i).equals(cityName)) {
                    nearCities.add(this.citiesNames().get(i));
                }
            }
        }

        return nearCities;
    }

Method citiesNames() returns List<“String”>. The names of cities.

Method “int getDistance(String srcCityName, String destCityName)” returns distance between srcCityName and srcCityName.

PS: It is forbidden to use loops, iterators inside this class. Only streams and methods that accept predicates can be used. You cannot declare other fields in the class.

This is my homework)

Answer

if (citiesNames().contains(cityName) {
    return citiesNames().stream()
                        .filter(city -> getDistance(cityName, city) <= radius)
                        .collect(Collectors.toList());
}
else {
    throw new IllegalArgumentException(cityName + " not found.");
}

filter method returns a stream that contains only the near cities.
collect method creates a List containing all the elements in the filtered stream.