Skip to content

Commit b7317e3

Browse files
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 changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
public class ListDemo3 {
6+
public static void main(String[] args) {
7+
// Example 1: Normal ArrayList
8+
List<String> list = new ArrayList<>();
9+
System.out.println(list.getClass().getName());
10+
11+
// Example 2: Arrays.asList with varargs
12+
List<String> list1 = Arrays.asList("Monday", "Tuesday");
13+
System.out.println(list1.getClass().getName());
14+
15+
// You can modify elements (set), but cannot add/remove
16+
list1.set(1, "Wednesday");
17+
System.out.println(list1.get(1));
18+
19+
// Example 3: Arrays.asList with existing array
20+
String[] array = {"Apple", "Banana", "Cherry"};
21+
List<String> list2 = Arrays.asList(array);
22+
System.out.println(list2.getClass().getName());
23+
24+
List<Integer> list3 = List.of(1,2,3,4);
25+
list3.set(1,33);
26+
}
27+
}
28+
29+
/*
30+
1. new ArrayList<>() → Returns a true ArrayList.
31+
2. Arrays.asList("Monday", "Tuesday") → Returns a fixed-size list backed by the array.
32+
3. Arrays.asList(array) → Also a fixed-size list backed by the provided array.
33+
*/

0 commit comments

Comments
 (0)