Wednesday, April 1, 2015

ArrayList with Beans

Employee.java (Bean class)

public class Employee {

private int empId;
private String empName;
private int salary;
private String email;

public int getEmpId() {
return empId;
}
public void setEmpId(int employeeId) {
this.empId = employeeId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}

@Override
public String toString() {
String employeedetails = this.empId+","+this.empName+","+this.email+","+this.salary;
return employeedetails;
}
}



ArrayList Implementation
import java.util.ArrayList;
import java.util.Iterator;


public class ListExample {
public static void main(String args[])
{
Employee emp = new Employee();
emp.setEmpId(1);
emp.setEmpName("Murali");
emp.setEmail("murali@gmail.com");
emp.setSalary(60000);
Employee emp1 = new Employee();
emp1.setEmpId(2);
emp1.setEmpName("Renu");
emp1.setEmail("renu@gmail.com");
emp1.setSalary(45000);
ArrayList<Employee> employeeList = new ArrayList<Employee>();
employeeList.add(emp);
employeeList.add(emp1);
Iterator<Employee> itr = employeeList.iterator();
while(itr.hasNext())
{
Employee employee = itr.next();
System.out.println(employee.toString());
System.out.println("Employee id is "+employee.getEmpId());
System.out.println("Employee name is "+employee.getEmpName());
System.out.println("Employee email is "+employee.getEmail());
System.out.println("Employee salary is "+employee.getSalary());
}
}

}

No comments:

Post a Comment