From 4f223e68d0a4d3956253850e3b17bc4d6bca89ff Mon Sep 17 00:00:00 2001 From: Wei Hung Chin Date: Wed, 15 May 2019 10:19:59 +0800 Subject: [PATCH] Add new example (#30133) Added example of using map operator to get a list of object properties from a list of objects --- guide/english/java/streams/index.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/guide/english/java/streams/index.md b/guide/english/java/streams/index.md index 3dbf4ccd4e..8efc04c08a 100644 --- a/guide/english/java/streams/index.md +++ b/guide/english/java/streams/index.md @@ -88,5 +88,22 @@ List result2 = Arrays.asList("de", "abc", "f", "abc") // result: abc de ``` +```java +// Examples of extracting properties from a list of objects +// Let's say we have a List personList +// Each Person object has a property, age of type Integer with a getter, getAge +// To get a list of age from the List personList +// In a typical foor loop: + +List ageList = new ArrayList<>(); +for(Person person: personList){ + ageList.add(person.getAge()); +} + +//Using streams to achieve the same result as above +List ageList = personList.stream().map(Person::getAge).collect(Collectors.toList()); + +``` + ### 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)