Skip to content

Book Reading: Collections Framework

Book: Effective Java by Joshua Bloch
Relevant Items: Items on Collections, plus Core Java Volume I
Status: In Progress


Reading Goals

  • Understand collection interface design principles
  • Learn best practices for using collections
  • Master proper equals/hashCode for collection usage
  • Understand concurrent collection patterns

Effective Java: Collection Items

Item 54: Return Empty Collections or Arrays, Not Nulls

Key Takeaways

Best Practice

// Bad
public List<Cheese> getCheeses() {
    return cheesesInStock.isEmpty() ? null : new ArrayList<>(cheesesInStock);
}

// Good
public List<Cheese> getCheeses() {
    return cheesesInStock.isEmpty() 
        ? Collections.emptyList() 
        : new ArrayList<>(cheesesInStock);
}

Item 55: Return Optionals Judiciously

Key Takeaways


Item 52: Use Overloading Judiciously

Key Takeaways (Collection Context)


Item 58: Prefer For-Each Loops to Traditional For Loops

Key Takeaways

When Traditional For is Needed

  1. Filtering - removing elements
  2. Transforming - replacing elements
  3. Parallel iteration - multiple collections

Core Java Volume I: Collections Chapter

Collection Interface Design

Key Takeaways


List Implementations Deep Dive

ArrayList Internals

LinkedList Internals


Set Implementations Deep Dive

HashSet Internals

TreeSet Internals


Map Implementations Deep Dive

HashMap Deep Dive

Key Internal Details: - Initial capacity: 16 - Load factor: 0.75 - Treeification threshold: 8 - Untreeify threshold: 6 - Minimum tree capacity: 64

Hash Collision Resolution: 1. Java 7: Linked list only 2. Java 8+: Linked list → Red-Black tree when bucket size > 8

TreeMap Internals


Concurrent Collections

ConcurrentHashMap Design

CopyOnWriteArrayList


Theoretical Framework

Choosing the Right Collection

Need ordering?
├── No → Need uniqueness?
│         ├── Yes → HashSet
│         └── No → ArrayList
└── Yes → What kind of order?
          ├── Insertion order → LinkedHashSet/LinkedHashMap
          ├── Sorted order → TreeSet/TreeMap
          └── Access order → LinkedHashMap (accessOrder=true)

Performance Complexity Summary

Operation ArrayList LinkedList HashSet TreeSet HashMap TreeMap
add O(1)* O(1) O(1) O(log n) O(1) O(log n)
remove O(n) O(1)** O(1) O(log n) O(1) O(log n)
get/contains O(1)/O(n) O(n) O(1) O(log n) O(1) O(log n)

*amortized, **at known position


Reflections & Connections

Connections to Course Material

New Perspectives Gained


Summary Points


Bookmarks & Page References

Topic Reference Note
HashMap Internals Core Java Ch. 9
Empty Collections EJ Item 54
For-Each Loops EJ Item 58
Concurrent Collections Core Java Ch. 12

Last Updated: