Skip to content
Advertisement

Tag: java-stream

How to realize this method only with Streams?

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

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):

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 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

Advertisement