Short Answer:
We can sort Employee objects using: 1) Comparable or Comparator (without Stream API) 2) Stream API using sorted() method.
Employee Class
class Employee {
int id;
String name;
double salary;
public Employee(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public int getId() { return id; }
public String getName() { return name; }
public double getSalary() { return salary; }
public String toString() {
return id + " " + name + " " + salary;
}
}
1️⃣ Without Stream API
A) Using Comparator (Recommended)
Short: Use Collections.sort() with Comparator.
Listlist = new ArrayList<>(); list.add(new Employee(1,"Aftab",50000)); list.add(new Employee(2,"Zara",30000)); list.add(new Employee(3,"John",70000)); // Sort by salary Collections.sort(list, (e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()) ); System.out.println(list);
B) Using Comparable (Natural Sorting)
Short: Implement Comparable inside Employee class.
class Employee implements Comparable{ @Override public int compareTo(Employee e) { return Double.compare(this.salary, e.salary); } }
Then:
Collections.sort(list);
2️⃣ With Stream API (Java 8+)
Sort by Salary
list.stream()
.sorted(Comparator.comparing(Employee::getSalary))
.forEach(System.out::println);
Sort by Name
list.stream()
.sorted(Comparator.comparing(Employee::getName))
.forEach(System.out::println);
Sort by Salary Descending
list.stream()
.sorted(Comparator.comparing(Employee::getSalary).reversed())
.forEach(System.out::println);
Multiple Field Sorting (Salary then Name)
list.stream()
.sorted(Comparator.comparing(Employee::getSalary)
.thenComparing(Employee::getName))
.forEach(System.out::println);
Important Interview Points
- Comparable → Single natural sorting logic.
- Comparator → Multiple sorting logic possible.
- Stream sorted() does not modify original list.
- Collections.sort() modifies original list.
- Time Complexity → O(n log n).
- Comparator.comparing() is preferred in Java 8+.
Tricky Interview Question
What if salary is same and name is null? → Use Comparator.nullsFirst() or nullsLast().
Find Highest Paid Employee
Short Answer:
Use Collections.max() or Stream max() with Comparator.
Using Stream
Employee highest = list.stream()
.max(Comparator.comparing(Employee::getSalary))
.orElse(null);
System.out.println(highest);
Without Stream
Employee highest = Collections.max(list,
Comparator.comparing(Employee::getSalary));
Find Second Highest Salary (Very Common)
Short Answer:
Sort in descending order and skip first element.
Using Stream
Employee secondHighest = list.stream()
.sorted(Comparator.comparing(Employee::getSalary).reversed())
.skip(1)
.findFirst()
.orElse(null);
System.out.println(secondHighest);
Alternative (Distinct Salaries)
Double secondSalary = list.stream()
.map(Employee::getSalary)
.distinct()
.sorted(Comparator.reverseOrder())
.skip(1)
.findFirst()
.orElse(null);
Group Employees by Salary Range
Short Answer:
Use Collectors.groupingBy().
Map> grouped = list.stream()
.collect(Collectors.groupingBy(e -> {
if(e.getSalary() < 30000) return "Low";
else if(e.getSalary() < 60000) return "Medium";
else return "High";
}));
System.out.println(grouped);
Convert List to Map<Id, Name>
Short Answer:
Use Collectors.toMap().
Map map = list.stream()
.collect(Collectors.toMap(
Employee::getId,
Employee::getName
));
System.out.println(map);
Sorting Performance Comparison
| Method | Modifies Original List | Performance | Best Use Case |
| Collections.sort() | Yes | O(n log n) | Simple sorting |
| List.sort() | Yes | O(n log n) | Java 8 preferred |
| Stream.sorted() | No | O(n log n) | Functional style |
Important:
- All use TimSort internally (Hybrid of Merge + Insertion sort).
- Time Complexity = O(n log n).
- Stream does not change original list.
- Collections.sort() modifies list.
0 comments