Commit b7317e3
committed
feat(ListDemo3): add demo comparing ArrayList, Arrays.asList, and List.of
What
- Added ListDemo3 class.
- Demonstrated different ways to create lists in Java:
1. new ArrayList<>() → a fully resizable ArrayList.
2. Arrays.asList(varargs) → a fixed-size list backed by an array.
3. Arrays.asList(array) → also fixed-size, directly backed by the provided array.
4. List.of(...) → immutable list (cannot add, remove, or set).
- Printed the runtime class name of each list implementation.
- Modified elements in Arrays.asList-backed list with set().
- Attempted modification on List.of() (causes UnsupportedOperationException).
Why
- Highlights differences between various list creation methods in Java.
- Demonstrates mutability vs immutability:
- ArrayList → fully mutable (add/remove/set).
- Arrays.asList → supports set(), but fixed size (no add/remove).
- List.of → immutable (no modifications allowed).
- Useful for understanding when each method is appropriate.
How
- Example 1: new ArrayList<>().
- getClass().getName() → java.util.ArrayList.
- Example 2: Arrays.asList("Monday","Tuesday").
- getClass().getName() → java.util.Arrays$ArrayList (internal static class).
- list1.set(1,"Wednesday") → allowed.
- Example 3: Arrays.asList(array).
- Backed by same array.
- getClass().getName() → java.util.Arrays$ArrayList.
- Example 4: List.of(1,2,3,4).
- Attempt list3.set(1,33) → UnsupportedOperationException at runtime.
Logic
- Inputs: string literals, arrays, integers.
- Outputs:
- Printed class names.
- Successful element modification in Arrays.asList-backed list.
- Runtime exception when trying to modify List.of().
- Flow:
1. Create lists using different APIs.
2. Inspect class names.
3. Modify elements where allowed.
4. Show immutability enforcement with List.of().
- Edge cases:
- Arrays.asList and List.of both reject structural changes (add/remove).
- List.of also rejects element replacement (set()).
- Complexity: all operations O(1) here.
- Concurrency: none of these implementations are thread-safe by default.
Real-life applications
- Use new ArrayList<>() when you need full mutability.
- Use Arrays.asList(...) for quick fixed-size wrappers around arrays.
- Use List.of(...) for immutable constant lists (configuration, defaults).
Notes
- Arrays.asList is misleadingly named: it does not return java.util.ArrayList.
- Backing arrays for Arrays.asList stay in sync: changes to list reflect in array and vice versa.
- Immutable collections from List.of() were introduced in Java 9.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 5544a0f commit b7317e3
File tree
1 file changed
+33
-0
lines changed- Section 25 Collections Frameworks/List Interface/ArrayList/src
1 file changed
+33
-0
lines changedLines changed: 33 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 | + | |
0 commit comments