Learnitweb

Category: Java tutorial

  • java.util.Collections

    The java.util.Collections class is a utility class in Java that provides static methods for operating on or returning collections such as List, Set, and Map. It is part of the java.util package. Unlike java.util.Collection (an interface for actual collection objects), Collections contains helper methods to manipulate collection objects, such as sorting, searching, reversing, synchronizing, and…

  • Collection Hierarchy – Diagram

    Here’s a detailed textual diagram of the Java Collection Framework hierarchy with explanations at each level. I’ve included interfaces, abstract classes, and commonly used implementations.

  • How do you choose thread pool sizes?

    Choosing the right thread pool size is critical for building efficient, scalable, and responsive applications in Java. Setting the pool size too small leads to underutilization of system resources and slow performance. Setting it too large can exhaust memory, overwhelm the CPU, or cause context-switching overhead. There is no one-size-fits-all value, but there are well-established…

  • Why might CompletableFuture.get() be dangerous in production?

    Calling CompletableFuture.get() can be dangerous in production environments if used incorrectly or in the wrong context. While get() is a convenient way to retrieve the result of an asynchronous computation, it comes with several risks that can hurt performance, responsiveness, and reliability of your application, especially in highly concurrent or reactive systems. Below is a…

  • What’s the difference between submit() and execute() in ExecutorService?

    In Java, the ExecutorService interface provides two commonly used methods to run tasks asynchronously: submit() and execute(). Although both are used to schedule and run tasks in a thread pool, they differ in return type, error handling, and use cases. Below is a detailed explanation of the differences between submit() and execute() in ExecutorService. 1.…

  • CompletableFuture in Java

    Introduction to CompletableFuture CompletableFuture is a class in the java.util.concurrent package that extends the Future interface and implements the CompletionStage interface. It represents a future result of an asynchronous computation and allows you to write non-blocking, callback-based, and event-driven code. The key features that make CompletableFuture powerful include: Why CompletableFuture? Before Java 8, asynchronous programming…

  • Future in Java

    In modern Java programming, particularly in concurrent and multithreaded applications, it is important to execute tasks asynchronously so that the main thread can continue doing other work without waiting for long-running operations to complete. Java provides the Future interface as part of the java.util.concurrent package to help with this kind of asynchronous task execution. The…

  • Aggregation vs Composition in Java

    In Java and other object-oriented programming languages, understanding how objects relate to each other is essential to writing effective and maintainable code. Two commonly used object-oriented design principles are Aggregation and Composition. These are both types of association that define a “has-a” relationship between classes. However, they differ significantly in how they manage object lifecycles,…

  • How HashSet Internally Works in Java?

    1. Overview of HashSet The HashSet class is part of Java’s Collection Framework and implements the Set interface, which means: However, HashSet is not backed by its own custom data structure. Instead, it uses a HashMap internally to store its elements. 2. Internal Data Structure The HashSet internally uses a HashMap<E, Object> where: For example:…

  • How HashMap Internally Works in Java?

    1. Key Components of HashMap 1.1 Array of Buckets A HashMap uses an internal array, often referred to as a bucket array, to store the key-value mappings. Each element in this array is called a bucket, and each bucket can hold multiple entries due to collisions. This array is the backbone of the HashMap. 1.2…