Skip to content
Advertisement

How to realize this method only with Streams?

JavaScript

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)

Advertisement

Answer

JavaScript

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

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement