Remove actual object from a list in Java
Description
The following code shows how to remove actual object from a list.
Example
/* w w w . ja v a2s . c o m*/
import java.util.ArrayList;
public class Main {
public static void main(String[] a) {
ArrayList<Employee> emps = new ArrayList<Employee>();
Employee emp1 = new Employee("A", "G");
Employee emp2 = new Employee("T", "A");
Employee emp3 = new Employee("K", "J");
emps.add(emp1);
emps.add(emp2);
emps.add(emp3);
System.out.println(emps);
emps.remove(emp2);
System.out.println(emps);
}
}
class Address {
}
class Employee {
private String lastName;
private String firstName;
private Double salary;
public Address address;
public Employee(String lastName, String firstName) {
this.lastName = lastName;
this.firstName = firstName;
this.address = new Address();
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public double getSalary() {
return this.salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
The code above generates the following result.