Reputation: 2462
Here's a main class Employee.java
and 2 another classes that extend main class. ArrayList<Employee>
contains objects from createFixedEmployee.java
and createPerHourEmployee
. How to sort objects in ArrayList
by salary, and if some objects got same salary then sort their names alphabetically?
I tried to use Comparator.comparing();
but doesn't work, i get an error Cannot resolve method getMonthSalary();
Here's a code:
createPerHourEmployee.java
public class createPerHourEmployee extends Employee {
private double salary;
public createPerHourEmployee() {}
public double getSalaryPerHour() { return this.salary; }
public double setSalaryPerHour(double value) {
return this.salary = value;
}
public createPerHourEmployee (int _id, String _name, double _salary) {
setEmployeeID(_id);
setEmployeeName(_name);
this.salary = _salary;
}
public double getMonthSalary() {
return salary * (20 * 0.8);
}
public String toString() {
return getEmployeeID() + ", " + getEmployeeName() + ", " + getSalaryPerHour();
}
}
createFixedEmployee.java
public class createFixedEmployee extends Employee {
private double salary;
public createFixedEmployee() {}
public double getSalaryFixed() {
return this.salary;
}
public double setSalaryFixed(double value) {
return this.salary = value;
}
public createFixedEmployee(int _id, String _name, double _salary) {
setEmployeeID(_id);
setEmployeeName(_name);
this.salary = _salary;
}
public double getMonthSalary() {
return this.salary;
}
public String toString() {
return getEmployeeID() + ", " + getEmployeeName() + ", " + getSalaryFixed();
}
}
Employee.java
import java.util.ArrayList;
import java.util.Comparator;
public abstract class Employee {
private int base_id;
private String base_name;
public int getEmployeeID () {
return this.base_id;
}
public int setEmployeeID (int value) {
return this.base_id = value;
}
public String getEmployeeName () {
return this.base_name;
}
public String setEmployeeName(String value) {
return this.base_name = value;
}
public abstract double getMonthSalary();
public static void main(String[] argc) {
ArrayList<Employee> Employee = new ArrayList<Employee>();
Employee.add(new createPerHourEmployee(1, "asd", 1300));
Employee.add(new createFixedEmployee(7, "asds", 14025));
Employee.add(new createPerHourEmployee(2, "nikan", 1230));
Employee.add(new createPerHourEmployee(3, "nikalo", 12330));
Employee.add(new createFixedEmployee(6, "aaaa", 14025));
Employee.add(new createFixedEmployee(4, "nikaq", 140210));
Employee.add(new createFixedEmployee(5, "nikas", 124000));
Employee.add(new createFixedEmployee(6, "nikab", 14025));
Employee.sort(Comparator.comparing(Employee::getMonthSalary)
.thenComparing(Employee::getEmployeeName)); // here is an error
}
}
So I have tried to use
Employee.sort(Comparator.comparing(createFixedEmployee::getMonthSalary).
thenComparing(createFixedEmployee::getEmployeeName));
It works fine, but I need to sort for all classes, not only one class.
Upvotes: 2
Views: 665
Reputation: 311808
When sorting a List<T>
, you can definitely use functional references to any of T
's ancestors methods. The issue here is with the name you give to the list - You've named the local variable Employee
, which hides the name of the type Employee
when you try to create the function references in the Comparator
. Just give it a better name, and you should be fine. E.g.:
ArrayList<Employee> employeeList = new ArrayList<Employee>();
// Or better yet: List<Employee> employeeList = new ArrayList<>();
employeeList.add(new createPerHourEmployee(1, "asd", 1300));
employeeList.add(new createFixedEmployee(7, "asds", 14025));
employeeList.add(new createPerHourEmployee(2, "nikan", 1230));
employeeList.add(new createPerHourEmployee(3, "nikalo", 12330));
employeeList.add(new createFixedEmployee(6, "aaaa", 14025));
employeeList.add(new createFixedEmployee(4, "nikaq", 140210));
employeeList.add(new createFixedEmployee(5, "nikas", 124000));
Employee.add(new createFixedEmployee(6, "nikab", 14025));
employeeList.sort(Comparator.comparing(Employee::getMonthSalary)
.thenComparing(Employee::getEmployeeName));
Upvotes: 2
Reputation: 1074959
In main
, you've given your local variable the name Employee
. That shadows the identifier of your Employee
class. Variable names shouldn't start with a capital letter anyway, should be in the plural when referring to an array or list (unless they end with "list" or similar), and ideally shouldn't be the same as any of your class names.
So change the variable to employees
. Fix that, and the version using Employee::getMonthSalary
and such works:
employees.sort(Comparator.comparing(Employee::getMonthSalary)
.thenComparing(Employee::getEmployeeName));
I would also change createPerHourEmployee
to PerHourEmployee
because the overwhelming convention in Java is for class names to start with a capital letter and be nouns, not verbs. Similarly, createFixedEmployee
would be (say) SalariedEmployee
.
Upvotes: 1