You have created a List<String>
, which cannot hold instances of Employee
, but maybe their names...
It seems like you want something like this:
Employee employee = new Employee();
// maybe set some attributes (the next line is a guess due to a lack of information)
employee.setName("E. M. Ployee");
// create a list of employees
List<Employee> employees = new LinkedList<>();
// and add the employee
employees.add(employee);
If you have a List<String>
and a class Employee
that looks similar to this one
class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
then you can create an instance of Employee
for each name in the list of names:
public static void main(String[] args) {
// create a list of names (String)
List<String> names = new LinkedList<>();
names.add("E. M. Ployee");
names.add("U. R. Fired");
// create a list of employees (Employee)
List<Employee> employees = new LinkedList<>();
// go through the names and create an Employee for each one
names.forEach(name -> employees.add(new Employee(name)));
// then print the names from the employee objects that are in the list of employees
employees.forEach(employee -> System.out.println(employee.getName()));
}