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 filter method returns a stream that
Tag: java-stream
How to sort files in a stream by partial filename?
I use the following to sort files first by name, then by modification date: Question: how can I refactor the File::getName part to only compare based on partial filename, like: StringUtils.substringBefore(file.getName(), “_”)? Answer You could replace the method reference with a lambda expression on the File object, although you’ll have to specify the types (see the additional discussion here):
Compare Map Value with list using Stream API
Need to collect the common value from a List of String and from a MAP having String as KEY and List as VALUE. Snippet: In third method need to compare the List and MAP value and collect the common no in a List. Please guide using JAVA 8 Stream API Answer
How create a list of an object from two different types of lists
I have two objects that have two fields in common, code field and position field. I use two distinct lists, one of Object1 and another of Object2. Object2 was filtered before and had missing elements compared to Object1. I need to stream() on Object1 to compare code value and position value of objects that are still in the list of
Rewrite nested ternary operators in functional way
I have the following piece of code: Could you please help me simplify the above by means of the functional programming? I would like to use something similar to Answer Collectors.joining will only insert a delimiter if there are 2 or more elements. In your case, that’s when both are non-empty. If both are empty, the result is “” If
Java Stream API – Group by items of an object’s inner list
Is there a way to achieve the following example code, leveraging Java Stream API rather than having to create a HashMap and populate it inside double forEaches? I was trying to play around with groupingBy and flatMap but couldn’t find a way out. Having a list of Movies, where each one has a list of genres (Strings)… …I want to
How do I pull out fields from a custom class using streams? (Without using for/while loops)
I would like to know how to pull out field and make it become a list using streams. For example, if I have a list of Pairs, Pair[2] { Pair<3,5>,Pair<5,7>}, I would like to make it become Integer[3] {3,5,7}. (After using Stream.distinct() to remove the dupicate 5) Pair class is provided if that is needed. Answer You want to turn
How to map Iterable to its DTO?
I have the following repository: PermissionRepository: In my service, I am trying to map Permission entity to PermissionDTO, but as findAll() method returns Iterable<T> (Iterable<T> findAll();), I cannot convert as shown below as I generally use for List<T>. It throws “Cannot resolve method ‘stream’ in ‘Iterable'” error for the .stream() method. So, how can I map this Permission entity to
Flat tree of objects while keeping father and child
I have a class Employee which has a tree structure with property team Using Java lambdas I need to flat this tree into same level list while converting Employee class into the following ConvertedEmployee while storing names in dedicated lead and subordinates properties. So ConvertedEmployee is in some sense a node which keeps parent and children. And I need to
Convert iterative method to functional with Java 8 Streams
I have this algorithm right here, that gives me the cubic root of n with a loss precision of e. I need to do the same thing but using Java 8 Streams. Math2 is from a private git rep. You can use Math.pow instead; it will work too. How can I do the same algorithm with Streams? Answer Java Stream