How to get(collect) List of Properties (String, Double, Integer) from List of beans in Java 8?

UPDATED: 25 September 2015
This little excerpt shows How you can fetch List<String>, List<Double>, List<Integer>, List<Object>, etc... from List<Beans>. And it also explains the use of map method in stream.

<R> Stream<R> map(Function<? super T, ? extends R> mapper);
Returns a stream consisting of the results of applying the given function to the elements of this stream.

In Simple term: map will return the stream of elements based on method you are calling.
  • public String getFirstname() : returns stream of String
  • public Double getWeight() : returns stream of Double
  • public JSONObject getMetaData : returns stream of JSONObject
  • public Employee getEmployee: returns stream of Employee

Source code (Company.java)
public class Company {
    private Long id;
    private String CompanyName;
 
 public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCompanyName() {
        return CompanyName;
    }

    public void setCompanyName(String CompanyName) {
        this.CompanyName = CompanyName;
    }
}


Source code
The above image gives you abstract idea of following code.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ListPropertiesFromBeans {
    public static void main(String[] args) {
        /* Create an object of `Company` and set values */
        Company companyGoogle = new Company();
        companyGoogle.setId(1L);
        companyGoogle.setCompanyName("Google");
        
        Company companyMicrosoft = new Company();
        companyMicrosoft.setId(2L);
        companyMicrosoft.setCompanyName("Microsoft");
       
        /**
         * - Create `List` of `Company`
         * - Add `companyGoogle` and `companyMicrosoft` to List.
         */
        List<Company> listCompanies = new ArrayList<>();
        listCompanies.add(companyGoogle);
        listCompanies.add(companyMicrosoft);

        /**
         * JavaDoc: `map` method
         * Returns a stream consisting of the results of applying the given function to the elements of this stream.
         * 
         * Explanation: `.map(Company::getCompanyName)`
         * In this operation we are calling method `getCompanyName()` on each object of `Company` in List.
         * That returns the `Stream` of value(CompanyName) of all object in List.
         * In last we are collecting that data/values in List<String>
         */
        List<String> listCompanyNames = listCompanies.stream()
                                                     .map(Company::getCompanyName)
                                                     .collect(Collectors.toList());
        
        /* Print the company name collected using `map` method */
        listCompanyNames.stream()
                        .forEach(strCompanyName -> System.out.println(strCompanyName));
    }
}

Explanation: .map(Company::getCompanyName)
In this operation we are calling method getCompanyName() on each object of Company in List. That returns the Stream of value(CompanyName[String]) of all object in List and in the last we are collecting that data/values in List<String>


Output
Google
Microsoft

0 comments :