I have the next class:
public class ExampleClass {
private String title;
private String codeResponse;
private String fileName;
}
I need to generate a map in which the key is the filename, and the value is the list of objects that contain that filename. I did the following:
Map<String, List<ExampleClass>> mapValues = items
.stream()
.collect(Collectors.groupingBy(
item -> item.getFileName(),
Collectors.mapping(item -> item, Collectors.toList())
));
But in this case I am saving in each fileName the total list of objects, including those that do not apply.
Collectors.mapping. groupingBy + toList will do your taskExampleClassinstances with the samefileNameis returned.