I have a class Person which has the field String name. In another class I have a list of Persons. Is it possible to get a list of the person's names using the Java 8 streaming API?
            Asked
            
        
        
            Active
            
        
            Viewed 105 times
        
    0
            
            
         
    
    
        Stuart Marks
        
- 127,867
- 37
- 205
- 259
 
    
    
        user1406177
        
- 1,328
- 2
- 22
- 36
2 Answers
0
            
            
        From http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#approach9
yourCollection
.stream().map(p -> p.getName())//here you have stream of names
now all you have to do is convert this stream to list
.collect(Collectors.toList());
So try
List<String> names = yourCollection
                     .stream()
                     .map(p -> p.getName())
                     .collect(Collectors.toList());
 
     
    