Commit de566bb
committed
feat(ComparatorDemo2): add demo of custom comparators for integers and strings
What
- Added StringLengthComparator:
- Implements Comparator<String>.
- Compares two strings by their length.
- Returns negative if first is shorter, positive if longer, 0 if equal.
- Added MyComparator:
- Implements Comparator<Integer>.
- Compares integers by subtraction (ascending order).
- Added ComparatorDemo2 main class:
- Example 1: Creates a list of integers [2,3,9,7,1], sorts using MyComparator, prints result.
- Example 2: Creates a list of strings ["banana","apple","date"], sorts using StringLengthComparator, prints result.
Why
- Demonstrates how to implement and use custom comparators for flexible sorting.
- Highlights the power of Comparator to define multiple external orderings for the same data type.
- Educational example showing comparator usage beyond natural ordering.
How
- Defined two comparator classes implementing Comparator interface.
- In main():
- list.sort(new MyComparator()) sorts integers ascending.
- words.sort(new StringLengthComparator()) sorts strings by length ascending.
- Printed results to verify.
Logic
- Inputs:
- Integers: [2,3,9,7,1].
- Strings: ["banana","apple","date"].
- Outputs:
- Integers sorted ascending: [1,2,3,7,9].
- Strings sorted by length ascending: ["date","apple","banana"].
- Flow:
1. list.sort() calls compare() from MyComparator → orders numbers.
2. words.sort() calls compare() from StringLengthComparator → orders by string length.
- Edge cases:
- MyComparator’s subtraction could overflow for large values; safer to use Integer.compare(o1,o2).
- StringLengthComparator’s subtraction is safe, but Integer.compare(o1.length(), o2.length()) is clearer.
- Complexity / performance: O(n log n).
- Concurrency / thread-safety: Local lists only; not thread-safe in multi-thread contexts.
- Error handling: None required.
Real-life applications
- Sorting by string length (useful for formatting, UI, word games).
- Sorting integers or custom objects with specific rules (ranking, thresholds).
- Comparators allow different ordering without modifying the class.
Notes
- Comparator.comparingInt(String::length) is a cleaner functional alternative to StringLengthComparator.
- Integer::compare avoids overflow issues.
- This demo illustrates the verbose, explicit class-based comparator approach for clarity.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 55041b0 commit de566bb
File tree
1 file changed
+62
-0
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/Comparator/src
1 file changed
+62
-0
lines changedLines changed: 62 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
0 commit comments