I am trying to iterate over nested java object and from nested object I'm trying to populate a map using Java 8.
Here is the list and structure of objects
IssueTypeDto.java
public class IssueTypeDto {
private String id;
private String name;
private List<CustomFieldDto> customFields;
}
CustomFieldDto.java
private String id;
private TypeDto type;
I am getting List of IssueTypeDto as response. Each IssueTypeDto object can have a list of CustomFieldDto objects. My requirement is I need to iterate over a list of IssueTypeDto objects and for each CustomFieldDto object I need to get id and the corresponding TypeDto object and insert it into a map as key and value. Here is what I am trying to do currently
Map<String, TypeDto> map = issueTypes.stream()
.flatMap(issueType->issueType.getCustomFields().stream()
.collect(Collectors.toMap(
CustomFieldDto::getId,
CustomFieldDto::getType)));
But i am getting compile time error as Type mismatch:
cannot convert from Stream<Object> to Map<String,TypeDto>"
I am new to Java 8 streams. So I am not able to figure out the issue. Any help would be appreciated.