There is a class

public class Entity { private String firstName; public String getFirstName() { return firstName; } } 

There is an Item class that contains an Entity entity field:

 public class Item { private Entity entity; public Entity getEntity() { return entity; } 

There are ArraList items. I want to get a list of all firstName from it using stream. I am writing this code, but AndroidStudio swears at it:

 List<String> items = Item.items.stream().map(item -> item.getEntity().getFirstName().toString()).toArray(); 

    1 answer 1

    You have a strange naming system, it is not clear where in the Item class items are taken and it is not clear why you convert the stream into an array, and try to assign the result to the list. Suppose items are List<Item> , then:

     List<String> firstNames = items.stream() .map(Item::getEntity) .map(Entity::getFirstName) .collect(Collectors.toList());