简体   繁体   中英

Creating List of Objects in JAVA

I am trying to create a list of objects like this :

List<Employee> employee= new ArrayList<Employee>();

Set a key and value for this like this :

Employee emp = new Employee();
emp.setKey("John");
emp.setValue("305");
emp.setKey("David");
emp.setValue("790");

And finally, I put this on a list :

employee.add(emp);

When I print, it gives me only last entry :

for(Employee em : employee){      
 System.out.println(em.getKey());
}

It only give me "David " as a result and not "John" and "David"

Can somebody tell me how should I do this ?

You have created only one object:

Employee emp = new Employee();
emp.setKey("John");
emp.setValue("305");
Employee emp1 = new Employee();
emp1.setKey("David");
emp1.setValue("790");
employee.add(emp);
employee.add(emp1);

Now run the for loop

Try this code I modified It. You have created only one object and change its status.

    List<Employee> employee= new ArrayList<Employee>();

    Employee emp = new Employee();
    emp.setKey("John");
    emp.setValue("305");
    Employee emp1 = new Employee();
    emp1.setKey("David");
    emp1.setValue("790");

    employee.add(emp);
    employee.add(emp1);


    for(Employee em : employee){      
     System.out.println(em.getKey());
    }

You should create separate objects . Your are modifying same object hence previous object state is lost.

List<Employee> employee= new ArrayList<Employee>();
// create employee 1
Employee emp1 = new Employee();
emp1.setKey("John");
emp1.setValue("305");
// create employee 2
Employee emp2 = new Employee();
emp2.setKey("David");
emp2.setValue("790");
// add both employees to list
employee.add(emp1);
employee.add(emp2);
for(Employee em : employee){      
     System.out.println(em.getKey());
}

Your changing the key and the value of the employee without adding it to the list, so it will show only the last key and the last value.

Here's how to do it:

Employee emp = new Employee();
emp.setKey("John");
emp.setValue("305");
employee.add(emp);
emp = new Employee();
emp.setKey("David");
emp.setValue("790");
employee.add(emp);
List<Employee> emploee = new ArrayList<Employee>();

Employee emp1 = new Employee();
emp1.setKey("John");
emp1.setValue("305");
employee.add(emp1);

Employee emp2 = new Employee();
emp2.setKey("David");
emp2.setValue("790");
employee.add(emp2);

for(Employee em : employee){
System.out.println(em.getKey());
}

// or if you want to set the values via constructor

List<Employee> emploee = new ArrayList<Employee>();

employee.add(new Employee("John", "305"));
employee.add(new Employee("David", "790"));

for(Employee em : employee){
System.out.println(em.getKey());
}

When you are setting values to emp object you are overriding John's data with David

Employee emp = new Employee();
emp.setKey("John");
emp.setValue("305");

In same object "emp" you are adding new entry.

emp.setKey("David");
emp.setValue("790");

You need to create new object for David

  Employee emp1 = new Employee();
  emp1.setKey("David");
  emp1.setValue("790");

then add both object to List. This will work.

Your problem is pretty simple. In your attempt, you modify the same object two times. You have to create two separated object to correct your problem.

Employee emp1 = new Employee();
emp1.setKey("John");
emp1.setValue("305");
employee.add(emp1);

Employee emp2 = new Employee();
emp2.setKey("David");
emp2.setValue("790");    
employee.add(emp2);

But in your case, i think, you doesn't need to modify key and value of employee in the future. It could be better for you to move key and value setter to constructor to create your employees like this.

employee.add(new Employee("John", "305"));
employee.add(new Employee("David", "790"));

Also, your value field seem to be an int and not a String . I think it could be better for you to switch your value type.

employee.add(new Employee("John", 305));
employee.add(new Employee("David", 790));

A little example of Employee class with all modifications

public class Employee {
    private final String key;
    private final int value;

    public Employee(String key, int value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

    public int getValue() {
        return value;
    }
}

Also, i think, you could change key and value fields name to more explicit name. Hope this could help you in your code learning. Have Fun !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM