Java Collections Interview Questions for 5 Years Experience

Java Collections Interview Questions for 5 Years Experience Work through these 12 questions out loud. Open any question for the full model answer, the trap to avoid, and the follow-up a panel usually asks.

  1. When should you use CopyOnWriteArrayList?

    Use for subscriber lists in pub/sub where adds are rare and iteration is constant. Avoid for high-frequency add/remove — use ConcurrentLinkedQueue or synchronized collections....

    Full answer

  2. What are weak references and WeakHashMap use cases?

    Attach metadata to objects without extending their lifetime — canonical mapping while object lives. When key GCs, entry drops. Do not rely on timing. For API response caching...

    Full answer

  3. Lazy vs eager fetching in JPA — trade-offs?

    Default associations LAZY. Service method needing orders+items uses JOIN FETCH in repository query. Disable spring.jpa.open-in-view=false — forces explicit fetch in service...

    Full answer

  4. Load shedding and backpressure in microservices?

    Tomcat max queue + reject policy returns 503 with Retry-After. WebClient timeout + circuit breaker stops calling slow dependency. Kafka consumer pause partitions when...

    Full answer

  5. Explain the difference between == and .equals() in Java.

    For primitives, == compares values. For objects, == asks if two references point to the same instance. equals() compares content when overridden — String overrides it for...

    Full answer

  6. What is the difference between ArrayList and LinkedList?

    ArrayList: dynamic array, great for get(i) and iteration, amortized append. LinkedList: node chain, costly random access, only wins for frequent insert/remove in middle with...

    Full answer

  7. What is the difference between Comparable and Comparator?

    Comparable built into the type — Employee implements compareTo by id. Comparator external — sort by name or salary without changing Employee. Use...

    Full answer

  8. Compare BlockingQueue implementations for producer-consumer workloads.

    For order processing, use ArrayBlockingQueue with capacity matching SLA — slow consumer blocks producer instead of OOM. ThreadPoolExecutor with SynchronousQueue +...

    Full answer

  9. IdentityHashMap vs HashMap — when would you use IdentityHashMap?

    Use when identity matters not value — tracking object visitation in deep graph walk. Business keys (userId, email) belong in HashMap with proper equals/hashCode....

    Full answer

  10. What is the N+1 query problem and how do you fix it?

    Loading 100 orders then accessing each order's items triggers 101 queries. Fix with JOIN FETCH in JPQL, @EntityGraph on repository method, or a DTO query selecting only...

    Full answer

  11. HashMap vs ConcurrentHashMap — when and why?

    HashMap for single-threaded or external synchronization. ConcurrentHashMap for shared concurrent maps — finer-grained locking than Hashtable. Cannot store null keys/values in...

    Full answer

  12. Explain garbage collection basics in the JVM.

    New objects allocate in Eden; minor GC copies survivors to survivor spaces, then old gen. When old gen fills, major/full GC runs — longer pauses. Tune -Xmx, choose G1 for...

    Full answer

Java Collections Interview Questions and Answers · Top 50 Java Collections Interview Questions and Answers (2026 Edition) · Top 40 Java Collections Interview Questions | Must-Prepare before interview. · Java Collections Interview Questions Every Fresher Must Know

All interview question lists · Interview guides · Home · Support the platform