Partha Partha - 5 months ago 23
Java Question

Converting a List of Strings to a List of custom made objects in Java using Lambda Expression

So I have something like this :

List<String> persons = Arrays.asList("Tom","Harry", "Steve");

I want to convert it to a
using Lambda Expressions.

Assume Person is the following class :

class Person {
private String name;
public void setName(String name) { = name;

public String getName() {

I have been trying various things with forEach, map, etc, but not able to get the code to work.



If you add a constructor to your Person class that takes a string, you can just use:

List<Person> people =

If you don't have a constructor, then you will need a lambda:

List<Person> people =
    .map(s -> { Person p = new Person(); p.setName(s); return p; })