Learn how to print employee names in sorted order using Java 8 Stream API with a simple example. Understand filter(), map(), sorted(), and collect() step by step.
Code Explanation (Step-by-Step) :
- First, create an Employee class with id, name, and salary fields.
- Next, create a list of Employee objects.
- Then, convert the list into a stream using the stream() method.
- Use the filter() method to find employees whose salary is greater than 1000.
- After that, use the map() method to extract employee names with e -> e.getName().
- Then, use the sorted() method to sort the employee names in ascending order.
- Finally, use the collect() method to collect the sorted names into a list and print the result.
public class Employee {
private Integer id;
private String name;
private Integer salary;
public Employee(Integer id, String name, Integer salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
}
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class EmployeeTest {
public static void main(String[] args) {
List emp = Arrays.asList(
new Employee(1, "anup", 120),
new Employee(2, "pankaj", 1300),
new Employee(3, "vijay", 7230),
new Employee(4, "ravi", 678),
new Employee(5, "aman", 8230));
List name = emp.stream().filter(r -> r.getSalary() > 1000)
.map(e -> e.getName()).sorted()
.collect(Collectors.toList());
System.out.println("Employee name in sorted order :- " + name);
}
}
Output :-
Employee name in sorted order :- [aman, pankaj, vijay]
Print Employee Names in Unsorted Order Using Java 8
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class EmployeeTest {
public static void main(String[] args) {
List emp = Arrays.asList(
new Employee(1, "anup", 120),
new Employee(2, "pankaj", 1300),
new Employee(3, "vijay", 7230),
new Employee(4, "ravi", 678),
new Employee(5, "aman", 8230));
List name = emp.stream().filter(r -> r.getSalary() > 1000)
.map(Employee::getName)
.collect(Collectors.toList());
System.out.println("Employee name in Unsorted order :- " + name);
}
}
Output :
Employee Names in Unsorted Order: [pankaj, vijay, aman]
FAQ:
- How do you sort employee names in Java 8?
You can sort employee names in Java 8 by using Stream API with map(Employee::getName) and sorted(). - What does sorted() do in Java 8 Stream?
The sorted() method sorts stream elements in natural ascending order. - Why use map() in Java 8 Stream?
The map() method transforms each object into another value, such as converting an Employee object into an employee name. - How does filter() work in Java 8 Stream? The filter() method selects elements that match a given condition, such as salary greater than 1000