convert String list in List of object [duplicate]

This might work ,I just took the arrays initially instead of List ,you can convert that into list

import java.util.*;
import java.util.stream.Collectors;

 class Employee {

    Integer id;
    String name;
    public Employee  setName(String name)
    {
      this.name=name;

      return this;
    }
    public String getName()
    {
     return this.name;
    }
}

class Main {
  public static void main(String[] args) {
    List<String> l =  Arrays.asList("john", "mike", "jackey", "dan");

 List<Employee> collect = l.stream().map(el->new Employee().setName(el)).collect(Collectors.toList());

    System.out.println(collect.get(0).getName());

      System.out.println(collect.get(1).getName());

        System.out.println(collect.get(2).getName());
  }
}

See the link

Leave a Comment