Full detail example: May be helpful for the starter on Stream class.
public class Employee {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
int salary;
public Employee(String n, int s) {
this.name = n;
this.salary = s;
}
@Override
public String toString() {
return "[" + name + ", " + salary + "]";
}
public static void main(String[] args) {
List<Employee> list = new ArrayList<Employee>() {
{
add(new Employee("Joe", 50000));
add(new Employee("Jim", 75000));
add(new Employee("Tom", 80000));
add(new Employee("Jim", 70000));
add(new Employee("Steve", 55000));
add(new Employee("Jim", 100000));
add(new Employee("Joe", 59000));
add(new Employee("Rich", 88000));
}
};
List<Employee> listSorted = new ArrayList<Employee>();
Comparator<Employee> NameCommparator = Comparator.comparing(Employee::getName);
listSorted = list.stream().sorted(NameCommparator).collect(Collectors.toList());
System.out.println(listSorted);
//sorting on descending order
Comparator<Employee> SalaryCommparator = Comparator.comparing(Employee::getSalary);
listSorted = list.stream().sorted(SalaryCommparator.reversed()).collect(Collectors.toList());
System.out.println(listSorted);
}
}