Skip to content
Advertisement

Converting to List result in Java

I have the following method:

public MenuExpandedDTO findMenuExp(UUID menuUuid) {
    final MenuDTO menu = menuService.findByUuid(menuUuid);
    final MenuPropertiesDTO propertiesDTO = new MenuPropertiesDTO(
            menu.getUuid(),
            menu.getName()
    );

    final List<GroupExpDTO> groups = menuGroupService
            .findAllByMenuUuid(menuUuid).stream()
            .map(menuGroup -> {
                UUID groupUuid = menuGroup.getGroupUuid();
                return findGroupExp(groupUuid);
            })
            .collect(Collectors.toList());

    return new MenuExpDTO(propertiesDTO, groups, null, null);
}

In this method, I pass a single menuUuid and then get combination of a single MenuPropertiesDTO and List<GroupExpDTO>.

I want to pass a List<menuUuid> instead of a single menuUuid and then get the result according to the uuids in this list. However, I am confused if there is a proper way for this in Java. I think there is no need to use loop and it would be possible to evaluate this using stream. But have really no idea how to construct it or if it is possible. Any help would be appreciated.

Advertisement

Answer

If I understood correctly from the question and comments, you need a method, which will work in the same manner, but for a list of UUIDs.

To achieve this, you can create new method, which will call the one you’ve implemented.

public List<MenuExpandedDTO> findMenuExp(List<UUID> menuUuids) {
    return menuUuids.stream()
        // assuming this method is added to the same class, I'm referring 'this' here
        .map(this::findMenuExp)
        .collect(Collectors.toList());
}

This method will return of list having size equal to menuUuids.size() and ith element of List<MenuExpandedDTO> will correspond to ith element of menuUuids list.

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