|
| 1 | +Important Terms & Methods in ArrayList and List API |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +## 1. getClass() |
| 6 | +- Returns the runtime class name of the object. |
| 7 | +- Helps distinguish between `java.util.ArrayList`, `java.util.Arrays$ArrayList`, `java.util.ImmutableCollections$ListN`, etc. |
| 8 | + |
| 9 | +Example: |
| 10 | +List<String> list = new ArrayList<>(); |
| 11 | +System.out.println(list.getClass().getName()); |
| 12 | +// Output: java.util.ArrayList |
| 13 | + |
| 14 | +List<String> list1 = Arrays.asList("A", "B"); |
| 15 | +System.out.println(list1.getClass().getName()); |
| 16 | +// Output: java.util.Arrays$ArrayList |
| 17 | + |
| 18 | +List<String> list2 = List.of("A", "B"); |
| 19 | +System.out.println(list2.getClass().getName()); |
| 20 | +// Output: java.util.ImmutableCollections$ListN |
| 21 | + |
| 22 | +--- |
| 23 | + |
| 24 | +## 2. List.of() (Java 9+) |
| 25 | +- Factory method to create an **immutable list**. |
| 26 | +- Cannot add, remove, or set elements. |
| 27 | +- Null values are not allowed (throws NullPointerException). |
| 28 | +- Useful for constants, read-only configs, or test data. |
| 29 | + |
| 30 | +Example: |
| 31 | +List<String> fruits = List.of("Apple", "Banana", "Cherry"); |
| 32 | +System.out.println(fruits); |
| 33 | +// fruits.add("Mango"); // UnsupportedOperationException |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## 3. Arrays.asList() |
| 38 | +- Wraps an array into a **fixed-size list**. |
| 39 | +- Backed by the original array: |
| 40 | + - Changing the list updates the array. |
| 41 | + - Changing the array updates the list. |
| 42 | +- Cannot add/remove elements, but you can set(). |
| 43 | + |
| 44 | +Example: |
| 45 | +String[] arr = {"A", "B", "C"}; |
| 46 | +List<String> list = Arrays.asList(arr); |
| 47 | + |
| 48 | +list.set(1, "Z"); |
| 49 | +System.out.println(Arrays.toString(arr)); // [A, Z, C] |
| 50 | +// list.add("D"); // UnsupportedOperationException |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## 4. ArrayList |
| 55 | +- Fully dynamic, resizable list. |
| 56 | +- Backed by an internal array (`Object[] elementData`). |
| 57 | +- Supports add, remove, set, get, resizing automatically. |
| 58 | +- Most commonly used list in Java. |
| 59 | + |
| 60 | +Example: |
| 61 | +ArrayList<String> al = new ArrayList<>(); |
| 62 | +al.add("One"); |
| 63 | +al.add("Two"); |
| 64 | +al.remove("One"); |
| 65 | +System.out.println(al); // [Two] |
| 66 | + |
| 67 | +--- |
| 68 | + |
| 69 | +## 5. toArray() |
| 70 | +- Converts a `List` into an array. |
| 71 | +- `list.toArray()` returns an Object[]. |
| 72 | +- `list.toArray(new Type[0])` returns a typed array. |
| 73 | + |
| 74 | +Example: |
| 75 | +List<Integer> list = List.of(1, 2, 3); |
| 76 | +Object[] arr1 = list.toArray(); // Object[] |
| 77 | +Integer[] arr2 = list.toArray(new Integer[0]); // Integer[] |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +## 6. unmodifiableList() |
| 82 | +- Creates a read-only wrapper around an existing list. |
| 83 | +- Any modification attempt throws `UnsupportedOperationException`. |
| 84 | +- Unlike `List.of()`, the underlying list can still be changed. |
| 85 | + |
| 86 | +Example: |
| 87 | +List<String> list = new ArrayList<>(); |
| 88 | +list.add("A"); |
| 89 | +List<String> unmod = Collections.unmodifiableList(list); |
| 90 | +// unmod.add("B"); // UnsupportedOperationException |
| 91 | +list.add("C"); |
| 92 | +System.out.println(unmod); // [A, C] |
| 93 | + |
| 94 | +--- |
| 95 | + |
| 96 | +## 7. CopyOnWriteArrayList |
| 97 | +- A **thread-safe** variant of ArrayList. |
| 98 | +- Every modification creates a fresh copy of the array. |
| 99 | +- Ideal for **concurrent, read-heavy scenarios** with infrequent writes. |
| 100 | + |
| 101 | +Example: |
| 102 | +CopyOnWriteArrayList<String> safeList = new CopyOnWriteArrayList<>(); |
| 103 | +safeList.add("A"); |
| 104 | +safeList.add("B"); |
| 105 | +System.out.println(safeList); // [A, B] |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +## 8. Arrays$ArrayList (internal class) |
| 110 | +- Returned by `Arrays.asList()`. |
| 111 | +- Fixed-size, backed by the original array. |
| 112 | +- `getClass().getName()` → `java.util.Arrays$ArrayList`. |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +## 9. ImmutableCollections$ListN (internal class) |
| 117 | +- Returned by `List.of()`. |
| 118 | +- Immutable (Java 9+). |
| 119 | +- `getClass().getName()` → `java.util.ImmutableCollections$ListN`. |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +## 10. Collections$UnmodifiableList (internal class) |
| 124 | +- Returned by `Collections.unmodifiableList(list)`. |
| 125 | +- Read-only wrapper; modifications throw exceptions. |
| 126 | +- `getClass().getName()` → `java.util.Collections$UnmodifiableList`. |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +## 11. Differences Between List Factories |
| 131 | +- `new ArrayList<>()` → full-featured, resizable. |
| 132 | +- `Arrays.asList()` → fixed-size, tied to original array. |
| 133 | +- `List.of()` → immutable, nulls not allowed. |
| 134 | +- `Collections.unmodifiableList()` → wrapper, underlying list may still change. |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +# Detailed Comparison Table |
| 139 | + |
| 140 | +| Method / Class | Modifiable? | Resizable? | Backed by Array? | Nulls Allowed? | Thread-Safe? | Runtime Class Name | Notes | |
| 141 | +|---------------------------------|---------------------|------------|------------------|----------------|--------------|-----------------------------------------------|----------------------------------------------------------------------| |
| 142 | +| new ArrayList<>() | Yes (add/remove/set)| Yes | No (independent) | Yes | No | java.util.ArrayList | Most common, dynamic, resizable. | |
| 143 | +| Arrays.asList() | No (fixed-size) | No | Yes | Yes | No | java.util.Arrays$ArrayList | Changes reflect in both array and list. | |
| 144 | +| List.of() (Java 9+) | No (immutable) | No | No | No | No | java.util.ImmutableCollections$ListN | Best for constants and read-only lists. | |
| 145 | +| Collections.unmodifiableList() | No (wrapper only) | No | No | Yes | No | java.util.Collections$UnmodifiableList | Underlying list can still be changed externally. | |
| 146 | +| CopyOnWriteArrayList | Yes (safe add/remove)| Yes | No | Yes | Yes | java.util.concurrent.CopyOnWriteArrayList | Great for concurrent read-heavy use cases. | |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +# Summary |
| 151 | + |
| 152 | +- Use `ArrayList` → when you need a flexible, resizable list. |
| 153 | +- Use `Arrays.asList()` → when you want a fixed-size view backed by an array. |
| 154 | +- Use `List.of()` → when you need an immutable list (Java 9+). |
| 155 | +- Use `Collections.unmodifiableList()` → when you want a read-only wrapper. |
| 156 | +- Use `CopyOnWriteArrayList` → when working with threads, especially read-heavy scenarios. |
0 commit comments