0

Can I use linkedlist when I create Instances?

Example:

I have Main class and a class Employee.

Main.class:

 Employee a1 = new Employee();

When we create Instances, usually do the code like this.

But I'm wondering if there's any way to create an instance using linked list.

Like this:

LinkedList<String> alist = new LinkedList<String>();  
Employee alist(index) = new Employee();
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • Yes, a linked list (or any other Java collection) can hold a collection of `Employee` objects. Check out the Oracle trail on collections. – Tim Biegeleisen Nov 05 '19 at 08:49
  • Be aware that in almost all cases in Java, using `ArrayList` makes more sense than using `LinkedList`. `ArrayList` uses less memory and is generally faster. See also [When to use LinkedList over ArrayList in Java?](https://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist-in-java) – Mark Rotteveel Nov 05 '19 at 08:53

3 Answers3

1

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()));
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
0

You just instantiate the element when adding it to the list

alist.add(new employee());

Given your code, you should also change your declaration. So the result would be:

LinkedList<Employee> alist = new LinkedList<>();
 alist.add(new Employee());
Ssr1369
  • 348
  • 3
  • 16
  • You're welcome! Please make sure to mark the answer as correct if you think it is ^^ – Ssr1369 Nov 05 '19 at 08:50
  • You cannot add an instance of `Employee` to any `List`... – deHaar Nov 05 '19 at 08:54
  • @thrillingchase Please be specific: in what way doesn't it work? The problem is probably that you are creating a `LinkedList`, which can't contain an `Employee`... – Mark Rotteveel Nov 05 '19 at 08:54
  • You declared the LinkedList as a container of . Employee is probably not a string, so either add strings to it, or change to . – Ssr1369 Nov 05 '19 at 08:55
  • Check the addition I did on the answer. You need to make the LinkedList of Employee, not of String. – Ssr1369 Nov 05 '19 at 09:07
0
List<Employee> employees = new LinkedList<>();

employees.add(new Employee(..., ..., ...));

Employee empMarta = new Employee("Marta", "Green", 24);

employees.add(empMarta);
  1. Maybe it's better to use some SET interface implementation (Set has unique list of elements)?
  2. For Employee class you should override equals() and hashCode() methods;
  3. And the main thing: if you decide to use List, then you should answer to the question "What is the most frequent operation You plan to perform (add, search, delete)?". Based on answer you should choose between ArrayList and LinkedList.