way to find the List of all ids in a list of entities from inner object with java 8

Er KK Chopra :

I have an entity:

public class Entity
{
    private long id;    
    private InnerEnity data;

    public long getId() {
        return id;
    }

    public InnerEnity getData() {
        return data;
    }
}

InnerEnity class

public class InnerEnity 
{
    private long id;    
    private String data;

    public long getId() {
        return id;
    }

    public String getData() {
        return data;
    }
}

what i need is list of InnerEnity ids. TO resolve this i tried something like that :-

List innerEnityIds = listOfEnity.stream().map(sys -> sys.getData().stream().map(obj->obj.getId().collect(Collectors.toList())));

Naman :

You just need to map the entity to its inner entity's(data) id as :

List<Long> innerEnityIds = listOfEnity.stream()
        .map(entity -> entity.getData().getId()) // <<< this
        .collect(Collectors.toList());

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=93474&siteId=1