Commit e352870
committed
feat(constructor-reference): demonstrate object creation using constructor references with streams
What
- Added `MobilePhone` class with:
- A single-argument constructor accepting `String name`.
- Overridden `toString()` method for human-readable output.
- Added `ConstructorReference` class to showcase:
- Using `MobilePhone::new` as a constructor reference in a stream pipeline.
- Mapping a list of `String` names into a list of `MobilePhone` objects.
- Printing the resulting list with readable `toString()` output.
Why
- Constructor references (`ClassName::new`) provide a concise alternative to lambdas like `s -> new MobilePhone(s)`.
- Improves code readability and avoids repetitive boilerplate when object construction aligns with functional interface signatures.
- Demonstrates synergy between Java Streams and method references for elegant data transformations.
Logic
1. Input Preparation:
- Created `List<String> names = Arrays.asList("A", "B", "C")`.
2. Stream Transformation:
- `names.stream()` → opens pipeline for processing.
- `.map(MobilePhone::new)` → for each string, invokes `new MobilePhone(String)` using constructor reference.
- `.collect(Collectors.toList())` → gathers all MobilePhone objects into a list.
3. Output:
- `System.out.println(mobilePhoneList)` → prints list contents.
- Because `toString()` was overridden, output is clear:
`[MobilePhone{name='A'}, MobilePhone{name='B'}, MobilePhone{name='C'}]`.
Real-life applications
- Converting raw data (e.g., CSV strings, JSON fields) into domain objects via constructor references.
- Creating DTOs or entity objects from IDs, names, or primitive values.
- Useful in stream-heavy data pipelines for object instantiation without verbose lambdas.
Notes
- Equivalent lambda for `.map(MobilePhone::new)` is `.map(name -> new MobilePhone(name))`.
- Overriding `toString()` is crucial for readable debugging and logging; otherwise, only memory references would appear.
- Constructor references require the functional interface signature (here, `Function<String, MobilePhone>`) to match the constructor signature.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 0f8d6a2 commit e352870
File tree
1 file changed
+72
-0
lines changed- Java 8 Crash Course/Functional Interface/Static Methods/src
1 file changed
+72
-0
lines changedLines changed: 72 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 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
0 commit comments