List Iterator: ============== -> Can able to make the iteration on the list of objects in both forward and backward direction. /* * WAP in Java to create the ArrayList to store Employee Object. * And Display the information of Female Employees only. * And also display the employee information who are having above 5 years of experience. */ package pack1; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; class Employee{ private int empNo; private String ename; private double salary; private String gender; private double experience; // creating the constructor for initializing class data members public Employee(int empNo, String ename, double salary, String gender, double experience) { super(); this.empNo = empNo; this.ename = ename; this.salary = salary; this.gender = gender; this.experience = experience; } // defining setters and getters public int getEmpNo() { return empNo; } public void setEmpNo(int empNo) { this.empNo = empNo; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public double getExperience() { return experience; } public void setExperience(double experience) { this.experience = experience; } // generate toString() @Override public String toString() { return "Employee [empNo=" + empNo + ", ename=" + ename + ", salary=" + salary + ", gender=" + gender + ", experience=" + experience + "]"; } } public class MainClass { private static ArrayList getEmployeeList(){ ArrayList empList = new ArrayList<>(); empList.add(new Employee(7928,"Amisha",55000.0,"Female",3.5)); empList.add(new Employee(7929,"Ashok",45000.0,"Male",3.2)); empList.add(new Employee(7930,"Akshay",25000.0,"Male",1.5)); empList.add(new Employee(7931,"Bhavana",76000.0,"Female",5.5)); empList.add(new Employee(7932,"Keerthi",45000.0,"Female",3.0)); empList.add(new Employee(7933,"Vijay Gopal",105000.0,"Male",6.5)); empList.add(new Employee(7934,"Vijay",35000.0,"Male",1.5)); empList.add(new Employee(7935,"Vinitha",45000.0,"Female",3.0)); empList.add(new Employee(7936,"Vignesh",125000.0,"Male",6.7)); empList.add(new Employee(7937,"Ananya",119000.0,"Female",7.1)); return empList; } public static void main(String[] args) { List empList = getEmployeeList(); ListIterator litr = empList.listIterator(); // Traversing on Employee List in forward Direction while(litr.hasNext()) { litr.next(); } // Traversing on Employee List in reverse direction while(litr.hasPrevious()) { System.out.println(litr.previous()); } } } ======================================================================= Sorting: ======== sort(): ======= -> this can use to perform the sorting on the collections. -> sort() is the method of Collections. Syntax: Collections.sort(); comparable Interface: ========================= -> Types of interfaces: 3-types: 1) Normal Interface ==> define with more than one abstract methods 2) Marker Interface ==> define with 0 abstract methods 3) Functional Interface ==> define with Single Abstract Method also called as "sam interface" -> comparable interface is the type of functional interface because it has single abstract method (sam) called as "compareTo()". -> To perform the natural sorting (Ascending order), the class should be implements from comparable interface. Syntax: class Student implements comparable{ // variables; // constructor; // setters/getters @Override public int compareTo(Student o){ // logic; } } package package1; import java.util.ArrayList; import java.util.Collections; import java.util.List; class Employee implements Comparable{ private int empNo; private String ename; private double salary; private String gender; private double experience; // creating the constructor for initializing class data members public Employee(int empNo, String ename, double salary, String gender, double experience) { super(); this.empNo = empNo; this.ename = ename; this.salary = salary; this.gender = gender; this.experience = experience; } // defining setters and getters public int getEmpNo() { return empNo; } public void setEmpNo(int empNo) { this.empNo = empNo; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public double getExperience() { return experience; } public void setExperience(double experience) { this.experience = experience; } // generate toString() @Override public String toString() { return "Employee [empNo=" + empNo + ", ename=" + ename + ", salary=" + salary + ", gender=" + gender + ", experience=" + experience + "]"; } @Override public int compareTo(Employee o) { return this.getEmpNo()-o.getEmpNo(); } } public class MainClass { private static ArrayList getEmployeeList(){ ArrayList empList = new ArrayList<>(); empList.add(new Employee(7928,"Amisha",55000.0,"Female",3.5)); empList.add(new Employee(8029,"Ashok",45000.0,"Male",3.2)); empList.add(new Employee(7930,"Akshay",25000.0,"Male",1.5)); empList.add(new Employee(7731,"Bhavana",76000.0,"Female",5.5)); empList.add(new Employee(9732,"Keerthi",45000.0,"Female",3.0)); empList.add(new Employee(9833,"Vijay Gopal",105000.0,"Male",6.5)); empList.add(new Employee(8034,"Vijay",35000.0,"Male",1.5)); empList.add(new Employee(7635,"Vinitha",45000.0,"Female",3.0)); empList.add(new Employee(6736,"Vignesh",125000.0,"Male",6.7)); empList.add(new Employee(7937,"Ananya",119000.0,"Female",7.1)); return empList; } public static void main(String[] args) { List lstEmp = getEmployeeList(); Collections.sort(lstEmp); // for each loop System.out.println("The Employee after the sorting based on the Employee ID = "); for(Employee e:lstEmp) { System.out.println(e); } } } ====================================== If you want to do the sorting based on the employee name: ========================================================== package package1; import java.util.ArrayList; import java.util.Collections; import java.util.List; class Employee implements Comparable{ private int empNo; private String ename; private double salary; private String gender; private double experience; // creating the constructor for initializing class data members public Employee(int empNo, String ename, double salary, String gender, double experience) { super(); this.empNo = empNo; this.ename = ename; this.salary = salary; this.gender = gender; this.experience = experience; } // defining setters and getters public int getEmpNo() { return empNo; } public void setEmpNo(int empNo) { this.empNo = empNo; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public double getExperience() { return experience; } public void setExperience(double experience) { this.experience = experience; } // generate toString() @Override public String toString() { return "Employee [empNo=" + empNo + ", ename=" + ename + ", salary=" + salary + ", gender=" + gender + ", experience=" + experience + "]"; } // @Override // public int compareTo(Employee o) { // return this.getEmpNo()-o.getEmpNo(); // } @Override public int compareTo(Employee o) { return this.getEname().compareTo(o.getEname()); } } public class MainClass { private static ArrayList getEmployeeList(){ ArrayList empList = new ArrayList<>(); empList.add(new Employee(7928,"Amisha",55000.0,"Female",3.5)); empList.add(new Employee(8029,"Ashok",45000.0,"Male",3.2)); empList.add(new Employee(7930,"Akshay",25000.0,"Male",1.5)); empList.add(new Employee(7731,"Bhavana",76000.0,"Female",5.5)); empList.add(new Employee(9732,"Keerthi",45000.0,"Female",3.0)); empList.add(new Employee(9833,"Vijay Gopal",105000.0,"Male",6.5)); empList.add(new Employee(8034,"Vijay",35000.0,"Male",1.5)); empList.add(new Employee(7635,"Vinitha",45000.0,"Female",3.0)); empList.add(new Employee(6736,"Vignesh",125000.0,"Male",6.7)); empList.add(new Employee(7937,"Ananya",119000.0,"Female",7.1)); return empList; } public static void main(String[] args) { List lstEmp = getEmployeeList(); Collections.sort(lstEmp); // for each loop System.out.println("The Employee after the sorting based on the Employee ID = "); for(Employee e:lstEmp) { System.out.println(e); } } } ===================================== Limitation with Comparable Interface: ======================================== -> the sorting on the list objects possible based on the single property only. ============================================== Comparator Interface: ========================= -> when we want to sort list objects based on multiple properties, we need "Comparator interface". package package1; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; class Employee{ private int empNo; private String ename; private double salary; private String gender; private double experience; // creating the constructor for initializing class data members public Employee(int empNo, String ename, double salary, String gender, double experience) { super(); this.empNo = empNo; this.ename = ename; this.salary = salary; this.gender = gender; this.experience = experience; } // defining setters and getters public int getEmpNo() { return empNo; } public void setEmpNo(int empNo) { this.empNo = empNo; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public double getExperience() { return experience; } public void setExperience(double experience) { this.experience = experience; } // generate toString() @Override public String toString() { return "Employee [empNo=" + empNo + ", ename=" + ename + ", salary=" + salary + ", gender=" + gender + ", experience=" + experience + "]"; } } class EmpnoComparator implements Comparator{ @Override public int compare(Employee o1, Employee o2) { return o1.getEmpNo() - o2.getEmpNo(); } } class SalaryComparator implements Comparator{ @Override public int compare(Employee o1, Employee o2) { return (int)(o1.getSalary() - o2.getSalary()); } } public class MainClass { private static ArrayList getEmployeeList(){ ArrayList empList = new ArrayList<>(); empList.add(new Employee(7928,"Amisha",55000.0,"Female",3.5)); empList.add(new Employee(8029,"Ashok",45000.0,"Male",3.2)); empList.add(new Employee(7930,"Akshay",25000.0,"Male",1.5)); empList.add(new Employee(7731,"Bhavana",76000.0,"Female",5.5)); empList.add(new Employee(9732,"Keerthi",45000.0,"Female",3.0)); empList.add(new Employee(9833,"Vijay Gopal",105000.0,"Male",6.5)); empList.add(new Employee(8034,"Vijay",35000.0,"Male",1.5)); empList.add(new Employee(7635,"Vinitha",45000.0,"Female",3.0)); empList.add(new Employee(6736,"Vignesh",125000.0,"Male",6.7)); empList.add(new Employee(7937,"Ananya",119000.0,"Female",7.1)); return empList; } public static void main(String[] args) { List lstEmp = getEmployeeList(); Collections.sort(lstEmp,new EmpnoComparator()); // for each loop System.out.println("The Employee after the sorting based on the Employee ID = "); for(Employee e:lstEmp) { System.out.println(e); } Collections.sort(lstEmp,new SalaryComparator()); System.out.println("The Employee after the sorting based on the Employee Salaries = "); for(Employee e:lstEmp) { System.out.println(e); } } }