The following is the Employee bean class.
public class Employee {
public String name;
public int age;
public Employee()
{
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}
I have other EmployeeTest class and inside it I create the object of the Employee class and store in a ArrayList.
import java.util.ArrayList;
public class EmployeeTest {
public static void main(String[] args)
{
ArrayList<Employee> empList = new ArrayList<Employee>();
Employee emp1 = new Employee();
emp1.setAge(15);
emp1.setName("Employee1");
Employee emp2 = new Employee();
emp2.setAge(10);
emp2.setName("Employee1");
empList.add(emp1);
empList.add(emp2);
for(Employee emp : empList)
{
System.out.println("employee name : " + emp.getName());
System.out.println("employee age : " + emp.getAge());
}
}
}
Now I have one question regarding it is that I want to sort ArrayList on the basis of Employee class age property. So please explain how can I sort it.