Add new example (#30133)
Added example of using map operator to get a list of object properties from a list of objects
This commit is contained in:
committed by
Christopher McCormack
parent
c37a4ff663
commit
4f223e68d0
@@ -88,5 +88,22 @@ List<String> result2 = Arrays.asList("de", "abc", "f", "abc")
|
|||||||
// result: abc de
|
// result: abc de
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
// Examples of extracting properties from a list of objects
|
||||||
|
// Let's say we have a List<Person> personList
|
||||||
|
// Each Person object has a property, age of type Integer with a getter, getAge
|
||||||
|
// To get a list of age from the List<Person> personList
|
||||||
|
// In a typical foor loop:
|
||||||
|
|
||||||
|
List<Integer> ageList = new ArrayList<>();
|
||||||
|
for(Person person: personList){
|
||||||
|
ageList.add(person.getAge());
|
||||||
|
}
|
||||||
|
|
||||||
|
//Using streams to achieve the same result as above
|
||||||
|
List<Integer> ageList = personList.stream().map(Person::getAge).collect(Collectors.toList());
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
### Sources
|
### Sources
|
||||||
1. [Processing Data with Java SE 8 Streams, Part 1](http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html)
|
1. [Processing Data with Java SE 8 Streams, Part 1](http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html)
|
||||||
|
Reference in New Issue
Block a user