Professional Documents
Culture Documents
Week5 26feb2024
Week5 26feb2024
Radford Burger
Agenda
Sorting an Arraylist of custom objects
GUI - Introduction to swing
Sorting Arraylist of objects
with Comparator in
separate class
We will define another class that will implement
the Comparator interface of the type of our
custom object.
Sorting Arraylist of objects
with Comparator in
separate class
This class will override the compare method of
the Comparator interface which accepts two
objects of the Employee class as parameters and
returns the comparison value according to our
requirement whether we want to sort the array in
ascending or descending order and on which
attribute, we want to sort the list.
Sorting Arraylist with Comparator class:
example
public class EmployeeListProgram {
public static void main(String[] args) {
ArrayList<Employee> employeeList = new ArrayList<>( );
// populate arraylist with hard-coded Employee objects
employeeList.add(new Employee("Kaya", "Mabula", 29, 8400.0));
employeeList.add(new Employee("Sisanda", "Bhele", 34, 3500.0));
employeeList.add(new Employee("Julian", "Lawson", 26, 6450.0));
employeeList.add(new Employee("Bongi", "Adams", 33, 5400.0));
employeeList.add(new Employee("Marie", "Botha", 23, 7450.0));
}//end main
}//end class
class MySalarySort implements Comparator<Employee>{
@Override
public int compare(Employee o1, Employee o2) {
return (int) (o1.getEmpSalary( ) – o2.getEmpSalary( )); //ascending)
}
Sorting Arraylist with Comparator class:
example
public class EmployeeListProgram {
public static void main(String[] args) {
ArrayList<Employee> employeeList = new ArrayList<>( );
// populate arraylist with hard-coded Employee objects
employeeList.add(new Employee("Kaya", "Mabula", 29, 8400.0));
employeeList.add(new Employee("Sisanda", "Bhele", 34, 3500.0));
employeeList.add(new Employee("Julian", "Lawson", 26, 6450.0));
employeeList.add(new Employee("Bongi", "Adams", 33, 5400.0));
employeeList.add(new Employee("Marie", "Botha", 23, 7450.0));
}//end main
}//end class
Sorting Arraylist with Lambda expression:
example
public class EmployeeListProgram {
public static void main(String[] args) {
ArrayList<Employee> employeeList = new ArrayList<>( );
// populate arraylist with hard-coded Employee objects
employeeList.add(new Employee("Kaya", "Mabula", 29, 8400.0));
employeeList.add(new Employee("Sisanda", "Bhele", 34, 3500.0));
employeeList.add(new Employee("Julian", "Lawson", 26, 6450.0));
employeeList.add(new Employee("Bongi", "Adams", 33, 5400.0));
employeeList.add(new Employee("Marie", "Botha", 23, 7450.0));
Collections.sort(employeeList, (o1, o2) -> (int) (o1.getEmpSalary( ) –
o2.getEmpSalary( )); //ascending)
}//end main
}//end class
Sorting Arraylist with Comparator class:
example with Strings
public class EmployeeListProgram {
public static void main(String[] args) {
ArrayList<Employee> employeeList = new ArrayList<>( );
// populate arraylist with hard-coded Employee objects
employeeList.add(new Employee("Kaya", "Mabula", 29, 8400.0));
employeeList.add(new Employee("Sisanda", "Bhele", 34, 3500.0));
employeeList.add(new Employee("Julian", "Lawson", 26, 6450.0));
employeeList.add(new Employee("Bongi", "Adams", 33, 5400.0));
employeeList.add(new Employee("Marie", "Botha", 23, 7450.0));
}//end main
}//end class
class MySurnameSort implements Comparator<Employee>{
@Override
public int compare(Employee o1, Employee o2) {
return (int)
(o1.getEmpSurname( ).compareTo(o2.getEmpSurname( ))); //ascending)
}
Practical exercise for this
week
Watch video: Java 8 Lambda - Sort List (ArrayList) in
Ascending and Descending Order Comparator Examples
https://www.youtube.com/watch?v=2DOzWaVen4Q