Skip to content

Commit 1f95cb1

Browse files
authored
Update collections.md
1 parent 0d0b9ed commit 1f95cb1

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

collections.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ nav_order: 2
55
layout: default
66
---
77

8-
## 🃏 Generics: Bounded Wildcards (PECS Rule)
8+
## 🃏 Generics: Bounded Wildcards (READ-WRITE Rule)
99

10-
**PECS Rule:** **P**roducer **E**xtends, **C**onsumer **S**uper
10+
**READ-WRITE Rule:** **R**ead **E**xtends, **W**rite **S**uper
1111

1212
- `? extends T`: **READ-ONLY** - Can read items of type T or its subtypes. Cannot add anything (except `null`)
1313
- `? super T`: **WRITE-ONLY** - Can write T or its subtypes. Cannot safely read (except as `Object`)
1414

15-
### Basic PECS Example
15+
### Basic READ-WRITE Example
1616
```java
17-
// Producer Extends - Reading from a collection
17+
// Read Extends - Reading from a collection
1818
List<? extends Number> numbers = List.of(1, 2.0, 3L);
1919
Number n = numbers.get(0); // ✅ OK - can read as Number
2020
// numbers.add(3); // ❌ Compile error - cannot write
2121

22-
// Consumer Super - Writing to a collection
22+
// Write Super - Writing to a collection
2323
List<? super Integer> values = new ArrayList<Number>();
2424
values.add(10); // ✅ OK - can write Integer/subtypes
2525
values.add(42); // ✅ OK - can write Integer/subtypes
@@ -41,7 +41,7 @@ public class FamilyGenericTest {
4141
Child child = new Child();
4242

4343
public void acceptOlderGenerations(List<? super Parent> familyList) {
44-
// Can ADD Parent and younger generations (Child)
44+
// Can WRITE Parent and younger generations (Child)
4545
familyList.add(new Parent()); // ✅ OK - Parent is exactly Parent
4646
familyList.add(child); // ✅ OK - Child is subtype of Parent
4747
// familyList.add(grandparent); // ❌ NO - Grandparent is not assignable to Parent
@@ -53,7 +53,7 @@ public class FamilyGenericTest {
5353
}
5454

5555
public void acceptYoungerGenerations(List<? extends Parent> familyList) {
56-
// Cannot ADD anything except null
56+
// Cannot WRITE anything except null
5757
// familyList.add(grandparent); // ❌ NO - cannot add to ? extends
5858
// familyList.add(new Parent()); // ❌ NO - cannot add to ? extends
5959
// familyList.add(child); // ❌ NO - cannot add to ? extends
@@ -77,13 +77,12 @@ public class FamilyGenericTest {
7777
- **Safe to read**: Whatever comes out is at least a Parent
7878

7979
**💡 Learning Tip:**
80-
- **`? super T`** = "I accept T and its parents" (write T, read Object)
81-
- **`? extends T`** = "I contain T and its children" (read T, write nothing)
80+
- **`? super T`** = "I can WRITE T and its children to this" (write T, read Object)
81+
- **`? extends T`** = "I can READ T and its children from this" (read T, write nothing)
8282

8383
**Q:** In `List<? super Parent> family`, why can't you read the result as `Parent`?
8484
**A:** Because the list could actually be `List<Grandparent>`, and reading a Grandparent as Parent would be unsafe downcasting.
8585

86-
8786
## 🃏 Deque Stack vs Queue Operations
8887

8988
**Rule:** Deque can act as both **Stack (LIFO)** and **Queue (FIFO)** with different method behaviors.

0 commit comments

Comments
 (0)