Commit a19bc60
committed
feat: implement Comparable<Employee1> for natural ordering by age
WHAT:
- Created `Employee1` class with fields `name` and `age`.
- Implemented `Comparable<Employee1>` to define natural ordering logic.
- Overrode `compareTo(Employee1 other)` using `Integer.compare()` to compare ages.
- Implemented `toString()` for clear object representation.
- Added `TestEmployee` class to demonstrate comparison between employees.
WHY:
- Shows how to use Java’s generic interface `Comparable<T>` to enforce type-safe comparison rules.
- Provides a clean, reusable way to sort or prioritize custom objects without external comparator logic.
- Demonstrates polymorphism: comparison logic is embedded in the object itself, making sorting collections intuitive.
HOW (Logic Flow):
1. `Employee1` objects are created with names and ages.
2. When `compareTo()` is called:
- If this.age > other.age → returns positive.
- If this.age < other.age → returns negative.
- If equal → returns zero.
3. This comparison behavior integrates seamlessly with:
- `Collections.sort(List<Employee1>)`
- Priority-based data structures like `TreeSet`, `PriorityQueue`
- Java Streams `.sorted()`
BENEFITS:
- Strong compile-time type safety (`Comparable<Employee1>` ensures only Employee1 objects compared).
- Simplifies sorting logic in larger applications — no extra comparator needed for default behavior.
- Clean and maintainable — toString() makes debugging and logging easier.
REAL-WORLD APPLICATIONS:
- Sorting employees by age in HR or payroll systems.
- Prioritizing users in queue management systems (e.g., oldest first).
- Any business domain where objects require a natural ordering rule.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 4b94fe2 commit a19bc60
File tree
2 files changed
+29
-14
lines changed- Section24JavaGenerics/src/GenericInterface
2 files changed
+29
-14
lines changedLines changed: 18 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
| 3 | + | |
10 | 4 | | |
11 | 5 | | |
12 | 6 | | |
13 | | - | |
| 7 | + | |
14 | 8 | | |
15 | 9 | | |
16 | 10 | | |
17 | 11 | | |
18 | 12 | | |
19 | | - | |
| 13 | + | |
20 | 14 | | |
21 | 15 | | |
22 | | - | |
23 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
24 | 22 | | |
25 | 23 | | |
26 | | - | |
27 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
28 | 32 | | |
29 | 33 | | |
30 | 34 | | |
31 | 35 | | |
32 | 36 | | |
33 | 37 | | |
34 | 38 | | |
35 | | - | |
36 | | - | |
| 39 | + | |
| 40 | + | |
Lines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
0 commit comments