Commit 8e5df9c
committed
feat(ComparatorDemo3): add demo of Arrays utilities and lexicographic comparison
What
- Added ComparatorDemo3 class:
- Demonstrates usage of java.util.Arrays methods for array comparison and manipulation.
- Created two int[] arrays a and b.
- Compared arrays with Arrays.compare(a, b) → lexicographic comparison.
- Copied array a into c using Arrays.copyOf(a, a.length).
- Printed all elements of c.
- Converted primitive int[] to Integer[] using streams and boxing for deep comparison.
- Checked equality with Arrays.deepEquals(objA, objC).
- Left comments suggesting use of other methods: Arrays.mismatch(), Arrays.sort(), Arrays.parallelPrefix().
Why
- Teaches how Arrays utilities simplify operations like comparison, copying, and equality checks.
- Highlights difference between compare() (lexicographic) and deepEquals() (content-based).
- Provides a foundation for exploring more advanced Arrays methods.
How
- Arrays.compare(a, b):
- Compares arrays element by element.
- Returns negative if first mismatching element in a < b.
- Returns positive if a > b at mismatch.
- Returns 0 if arrays are equal.
- Arrays.copyOf(a, a.length): duplicates array a fully.
- Arrays.stream(a).boxed(): converts int[] → Stream<Integer>.
- toArray(Integer[]::new): collects stream into Integer[].
- Arrays.deepEquals(objA, objC): checks equality element by element for object arrays.
Logic
- Inputs:
- a = [2,4,6,8,1,3,5,11].
- b = [2,4,6,8,1,3,5,7].
- Outputs:
- Arrays.compare(a, b) → positive result (11 > 7 at mismatch).
- Copy of a into c → identical to a.
- Printed elements of c.
- deepEquals(objA, objC) → true, since a and c are equal element by element.
- Edge cases:
- Arrays.compare handles different lengths: shorter is "less" if all common elements match.
- deepEquals requires object arrays, hence boxing needed for int[].
- Complexity / performance:
- compare() and deepEquals() run in O(n).
- Boxing adds overhead; only required for object-level comparisons.
- Concurrency / thread-safety: Not thread-safe; operations on local arrays are safe in demo.
- Error handling: Not applicable.
Real-life applications
- Comparing arrays for equality or lexicographic order (e.g., version numbers).
- Copying arrays for immutability or manipulation without altering originals.
- Using deepEquals() for nested or object arrays.
- Arrays utilities simplify common operations in algorithms, testing, or data processing.
Notes
- For primitive arrays, prefer Arrays.equals(a, c) instead of deepEquals after boxing.
- Arrays.mismatch(a, b) can find the first differing index.
- Arrays.parallelPrefix() supports efficient prefix-sum style computations in parallel.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent de566bb commit 8e5df9c
File tree
1 file changed
+16
-16
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/Comparator/src
1 file changed
+16
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | 1 | | |
4 | 2 | | |
5 | 3 | | |
6 | 4 | | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
10 | | - | |
11 | | - | |
12 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
13 | 11 | | |
14 | 12 | | |
| 13 | + | |
15 | 14 | | |
16 | | - | |
17 | | - | |
| 15 | + | |
| 16 | + | |
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
21 | 20 | | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | | - | |
38 | | - | |
39 | | - | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
40 | 40 | | |
41 | | - | |
| 41 | + | |
0 commit comments