<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>Sheetal Kumar Blog</title>
        <link>https://your-docusaurus-site.example.com/java</link>
        <description>Sheetal Kumar Blog</description>
        <lastBuildDate>Tue, 17 Mar 2026 07:22:48 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>en</language>
        <item>
            <title><![CDATA[Handling External API Responses in Spring Boot: A Practical Guide]]></title>
            <link>https://your-docusaurus-site.example.com/java/handling-external-response</link>
            <guid>https://your-docusaurus-site.example.com/java/handling-external-response</guid>
            <pubDate>Tue, 17 Mar 2026 07:22:48 GMT</pubDate>
            <description><![CDATA[When building backend applications, you often need to consume external APIs. One common scenario is dealing with APIs that return either success data or errors, sometimes inconsistently. This guide will walk you step by step through handling such responses cleanly in a Spring Boot application.]]></description>
            <content:encoded><![CDATA[<p>When building backend applications, you often need to consume external APIs. One common scenario is dealing with APIs that return <strong>either success data or errors</strong>, sometimes inconsistently. This guide will walk you step by step through handling such responses cleanly in a Spring Boot application.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="1-problem-statement">1. Problem Statement<a href="https://your-docusaurus-site.example.com/java/handling-external-response#1-problem-statement" class="hash-link" aria-label="Direct link to 1. Problem Statement" title="Direct link to 1. Problem Statement">​</a></h2>
<p>Imagine you are integrating with an external KYB (Know Your Business) API. The API can return two types of responses:</p>
<p><strong>Success response:</strong></p>
<div class="language-json codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-json codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token property">"data"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token property">"businessId"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"123"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token property">"status"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"verified"</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(199, 146, 234)">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token punctuation" style="color:rgb(199, 146, 234)">}</span><br></span></code></pre></div></div>
<p><strong>Error response:</strong></p>
<div class="language-json codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-json codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token property">"errors"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token plain"> </span><span class="token property">"type"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"not_found"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token property">"message"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"Business profile not found"</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token punctuation" style="color:rgb(199, 146, 234)">}</span><br></span></code></pre></div></div>
<p>Sometimes, the API returns a mix you <strong>don’t want</strong>:</p>
<div class="language-json codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-json codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token property">"data"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token null keyword" style="font-style:italic">null</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token property">"errors"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token plain"> </span><span class="token property">"type"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"not_found"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token property">"message"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"Business profile not found"</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">}</span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token punctuation" style="color:rgb(199, 146, 234)">}</span><br></span></code></pre></div></div>
<p>Returning both <code>data</code> and <code>errors</code> together is confusing and not ideal.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="2-why-mapping-directly-to-a-single-dto-fails">2. Why Mapping Directly to a Single DTO Fails<a href="https://your-docusaurus-site.example.com/java/handling-external-response#2-why-mapping-directly-to-a-single-dto-fails" class="hash-link" aria-label="Direct link to 2. Why Mapping Directly to a Single DTO Fails" title="Direct link to 2. Why Mapping Directly to a Single DTO Fails">​</a></h2>
<p>If you map the response directly to a single DTO like <code>KybResponse</code>, you may end up with:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">public class KybResponse {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    private Data data;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    private List&lt;ErrorResponse&gt; errors;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p>If <code>data</code> is null but <code>errors</code> exist, your controller might accidentally return a response with both <code>data</code> and <code>errors</code>, which is misleading for clients.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="3-designing-clean-response-models">3. Designing Clean Response Models<a href="https://your-docusaurus-site.example.com/java/handling-external-response#3-designing-clean-response-models" class="hash-link" aria-label="Direct link to 3. Designing Clean Response Models" title="Direct link to 3. Designing Clean Response Models">​</a></h2>
<p>To solve this, create two clear models:</p>
<p><strong>Error model:</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">public class ErrorResponse {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    private String type;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    private String message;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    // getters &amp; setters</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><strong>API response model:</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">import com.fasterxml.jackson.annotation.JsonInclude;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">@JsonInclude(JsonInclude.Include.NON_NULL)</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public class KybResponse {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    private Data data;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    private List&lt;ErrorResponse&gt; errors;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    // getters &amp; setters</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><code>@JsonInclude(JsonInclude.Include.NON_NULL)</code> ensures null fields are <strong>not serialized</strong>, preventing responses from containing both <code>data</code> and <code>errors</code>.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="4-service-layer-responsibility">4. Service Layer Responsibility<a href="https://your-docusaurus-site.example.com/java/handling-external-response#4-service-layer-responsibility" class="hash-link" aria-label="Direct link to 4. Service Layer Responsibility" title="Direct link to 4. Service Layer Responsibility">​</a></h2>
<p>The service layer should:</p>
<ol>
<li>Call the external API.</li>
<li>Inspect the response.</li>
<li>Throw exceptions if errors exist.</li>
<li>Return data only if successful.</li>
</ol>
<p>Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@Service</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public class KybService {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    private final RestTemplate restTemplate;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public KybService(RestTemplate restTemplate) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        this.restTemplate = restTemplate;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public Data getBusiness(String businessId) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        KybResponse response = restTemplate.getForObject(</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">                "https://external-api.com/kyb/" + businessId,</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">                KybResponse.class</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        );</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        if (response.getErrors() != null &amp;&amp; !response.getErrors().isEmpty()) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">            throw new KybException(response.getErrors());</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        return response.getData();</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="5-custom-exception-implementation">5. Custom Exception Implementation<a href="https://your-docusaurus-site.example.com/java/handling-external-response#5-custom-exception-implementation" class="hash-link" aria-label="Direct link to 5. Custom Exception Implementation" title="Direct link to 5. Custom Exception Implementation">​</a></h2>
<p>Define a custom exception to encapsulate API errors:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">public class KybException extends RuntimeException {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    private final List&lt;ErrorResponse&gt; errors;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public KybException(List&lt;ErrorResponse&gt; errors) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        super("KYB API returned errors");</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        this.errors = errors;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public List&lt;ErrorResponse&gt; getErrors() {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        return errors;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="6-exception-handling-approaches">6. Exception Handling Approaches<a href="https://your-docusaurus-site.example.com/java/handling-external-response#6-exception-handling-approaches" class="hash-link" aria-label="Direct link to 6. Exception Handling Approaches" title="Direct link to 6. Exception Handling Approaches">​</a></h2>
<ol>
<li><strong>Try-catch in controller</strong> – works but clutters the controller, not recommended.</li>
<li><strong><code>@ExceptionHandler</code> in controller</strong> – better, but still limited.</li>
<li><strong><code>@RestControllerAdvice</code></strong> – centralized global handling, recommended.</li>
</ol>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="7-global-exception-handler">7. Global Exception Handler<a href="https://your-docusaurus-site.example.com/java/handling-external-response#7-global-exception-handler" class="hash-link" aria-label="Direct link to 7. Global Exception Handler" title="Direct link to 7. Global Exception Handler">​</a></h2>
<p>Implement a global handler for <code>KybException</code>:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@RestControllerAdvice</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public class GlobalExceptionHandler {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    @ExceptionHandler(KybException.class)</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public ResponseEntity&lt;Map&lt;String, List&lt;ErrorResponse&gt;&gt;&gt; handleKybException(KybException ex) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        Map&lt;String, List&lt;ErrorResponse&gt;&gt; errorBody = Map.of("errors", ex.getErrors());</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        return ResponseEntity.badRequest().body(errorBody);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p>This keeps controllers clean and ensures consistent error responses.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="8-final-controller">8. Final Controller<a href="https://your-docusaurus-site.example.com/java/handling-external-response#8-final-controller" class="hash-link" aria-label="Direct link to 8. Final Controller" title="Direct link to 8. Final Controller">​</a></h2>
<p>Now the controller only focuses on successful responses:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@RestController</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">@RequestMapping("/kyb")</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public class KybController {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    private final KybService kybService;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public KybController(KybService kybService) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        this.kybService = kybService;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    @GetMapping("/{businessId}")</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public Data getKyb(@PathVariable String businessId) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        return kybService.getBusiness(businessId);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p>No error handling logic is inside the controller anymore.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="9-final-api-outputs">9. Final API Outputs<a href="https://your-docusaurus-site.example.com/java/handling-external-response#9-final-api-outputs" class="hash-link" aria-label="Direct link to 9. Final API Outputs" title="Direct link to 9. Final API Outputs">​</a></h2>
<p><strong>Success response:</strong></p>
<div class="language-json codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-json codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token property">"businessId"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"123"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token property">"status"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"verified"</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token punctuation" style="color:rgb(199, 146, 234)">}</span><br></span></code></pre></div></div>
<p><strong>Error response:</strong></p>
<div class="language-json codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-json codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token property">"errors"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token plain"> </span><span class="token property">"type"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"not_found"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token property">"message"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"Business profile not found"</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token punctuation" style="color:rgb(199, 146, 234)">}</span><br></span></code></pre></div></div>
<p>Notice there’s <strong>no overlap of <code>data</code> and <code>errors</code></strong>.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="10-best-practices">10. Best Practices<a href="https://your-docusaurus-site.example.com/java/handling-external-response#10-best-practices" class="hash-link" aria-label="Direct link to 10. Best Practices" title="Direct link to 10. Best Practices">​</a></h2>
<ul>
<li>Never return <code>data</code> and <code>errors</code> together.</li>
<li>Keep controllers thin, services smart.</li>
<li>Centralize exception handling.</li>
<li>Use a consistent API response format.</li>
</ul>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="11-optional-improvements">11. Optional Improvements<a href="https://your-docusaurus-site.example.com/java/handling-external-response#11-optional-improvements" class="hash-link" aria-label="Direct link to 11. Optional Improvements" title="Direct link to 11. Optional Improvements">​</a></h2>
<ul>
<li>Add <code>timestamp</code>, <code>status</code>, and <code>path</code> in error responses.</li>
<li>Map different error types to proper HTTP status codes.</li>
<li>Introduce a generic <code>ApiResponse&lt;T&gt;</code> for more consistency.</li>
</ul>
<p>Example enhanced error response:</p>
<div class="language-json codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-json codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token property">"timestamp"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"2026-03-17T12:30:00Z"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token property">"status"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token number" style="color:rgb(247, 140, 108)">400</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token property">"errors"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token plain"> </span><span class="token property">"type"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"not_found"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token property">"message"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"Business profile not found"</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token property">"path"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"/kyb/123"</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token punctuation" style="color:rgb(199, 146, 234)">}</span><br></span></code></pre></div></div>
<hr>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Java 8 Interview Guide]]></title>
            <link>https://your-docusaurus-site.example.com/java/j8</link>
            <guid>https://your-docusaurus-site.example.com/java/j8</guid>
            <pubDate>Fri, 13 Mar 2026 08:06:49 GMT</pubDate>
            <description><![CDATA[Everything a Senior Java Interviewer Expects You to Know]]></description>
            <content:encoded><![CDATA[<h3 class="anchor anchorWithStickyNavbar_LWe7" id="everything-a-senior-java-interviewer-expects-you-to-know">Everything a Senior Java Interviewer Expects You to Know<a href="https://your-docusaurus-site.example.com/java/j8#everything-a-senior-java-interviewer-expects-you-to-know" class="hash-link" aria-label="Direct link to Everything a Senior Java Interviewer Expects You to Know" title="Direct link to Everything a Senior Java Interviewer Expects You to Know">​</a></h3>
<p>I have seen Java evolve from the early JDK days to modern cloud native architectures. But if there is <strong>one release that fundamentally changed how Java is written</strong>, it is <strong>Java 8</strong>.</p>
<p>Before Java 8, Java was powerful but verbose. Writing simple operations on collections required boilerplate loops, anonymous classes, and a lot of ceremony.</p>
<p>Then Java 8 arrived.</p>
<p>And suddenly Java developers were talking about:</p>
<ul>
<li>Functional programming</li>
<li>Lambdas</li>
<li>Streams</li>
<li>Optional</li>
<li>Immutable date APIs</li>
</ul>
<p>From an interviewer’s perspective, <strong>Java 8 became a major filter</strong>. Candidates who truly understood it stood out immediately.</p>
<p>This guide walks through <strong>everything a strong backend engineer must know about Java 8</strong>.</p>
<hr>
<h1>Why Java 8 Was a Game Changer</h1>
<p>Before Java 8, Java lacked features that other languages already had:</p>
<ul>
<li>Functional programming</li>
<li>Clean collection processing</li>
<li>Immutable date APIs</li>
<li>Asynchronous computation primitives</li>
</ul>
<p>Developers had to write verbose code for simple tasks.</p>
<p>Example: <strong>Filtering a list before Java 8</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">List&lt;Integer&gt; evenNumbers = new ArrayList&lt;&gt;();</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">for(Integer num : numbers){</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    if(num % 2 == 0){</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        evenNumbers.add(num);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p>With Java 8:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">List&lt;Integer&gt; evenNumbers =</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">numbers.stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">       .filter(n -&gt; n % 2 == 0)</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">       .collect(Collectors.toList());</span><br></span></code></pre></div></div>
<p>Less code. More expressive. Easier to parallelize.</p>
<hr>
<h1>Problems with Pre Java 8 Java</h1>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="1-boilerplate-code">1. Boilerplate Code<a href="https://your-docusaurus-site.example.com/java/j8#1-boilerplate-code" class="hash-link" aria-label="Direct link to 1. Boilerplate Code" title="Direct link to 1. Boilerplate Code">​</a></h2>
<p>Anonymous classes were everywhere.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Collections.sort(list, new Comparator&lt;Integer&gt;() {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public int compare(Integer a, Integer b){</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        return a - b;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">});</span><br></span></code></pre></div></div>
<p>Java 8 simplified this drastically.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="2-no-functional-programming-support">2. No Functional Programming Support<a href="https://your-docusaurus-site.example.com/java/j8#2-no-functional-programming-support" class="hash-link" aria-label="Direct link to 2. No Functional Programming Support" title="Direct link to 2. No Functional Programming Support">​</a></h2>
<p>Languages like Scala and JavaScript already supported:</p>
<ul>
<li>first class functions</li>
<li>lambda expressions</li>
<li>function composition</li>
</ul>
<p>Java lacked these features until Java 8.</p>
<hr>
<h1>Functional Programming in Java</h1>
<p>Functional programming focuses on:</p>
<ul>
<li>pure functions</li>
<li>immutability</li>
<li>higher order functions</li>
</ul>
<p>Java introduced functional programming through:</p>
<ul>
<li>Lambdas</li>
<li>Functional interfaces</li>
<li>Streams API</li>
</ul>
<hr>
<h1>Lambda Expressions</h1>
<p>A lambda expression represents <strong>an anonymous function</strong>.</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="syntax">Syntax<a href="https://your-docusaurus-site.example.com/java/j8#syntax" class="hash-link" aria-label="Direct link to Syntax" title="Direct link to Syntax">​</a></h3>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">(parameters) -&gt; expression</span><br></span></code></pre></div></div>
<p>Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">(a, b) -&gt; a + b</span><br></span></code></pre></div></div>
<p>Used with functional interfaces.</p>
<p>Example with comparator:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Collections.sort(list, (a, b) -&gt; a - b);</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="functional-style-programming">Functional Style Programming<a href="https://your-docusaurus-site.example.com/java/j8#functional-style-programming" class="hash-link" aria-label="Direct link to Functional Style Programming" title="Direct link to Functional Style Programming">​</a></h2>
<p>Instead of telling the computer <strong>how to do something</strong>, you describe <strong>what needs to happen</strong>.</p>
<p>Imperative:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">for(int i : list){</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">   if(i &gt; 10) result.add(i);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p>Functional:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">list.stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .filter(i -&gt; i &gt; 10)</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .collect(Collectors.toList());</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="behind-the-scenes-compilation">Behind The Scenes Compilation<a href="https://your-docusaurus-site.example.com/java/j8#behind-the-scenes-compilation" class="hash-link" aria-label="Direct link to Behind The Scenes Compilation" title="Direct link to Behind The Scenes Compilation">​</a></h2>
<p>A common interview question:</p>
<p><strong>Are lambda expressions anonymous classes?</strong></p>
<p>No.</p>
<p>Java compiles lambdas using <strong>invokedynamic bytecode instruction</strong> and <strong>LambdaMetafactory</strong>.</p>
<p>Advantages:</p>
<ul>
<li>better performance</li>
<li>reduced class generation</li>
<li>improved memory usage</li>
</ul>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="interview-question">Interview Question<a href="https://your-docusaurus-site.example.com/java/j8#interview-question" class="hash-link" aria-label="Direct link to Interview Question" title="Direct link to Interview Question">​</a></h2>
<p><strong>Q: What is the difference between lambda and anonymous class?</strong></p>
<p>Key differences:</p>
<ol>
<li>Lambdas do not create a new scope for <code>this</code></li>
<li>Lambdas are compiled using invokedynamic</li>
<li>Anonymous classes generate class files</li>
</ol>
<hr>
<h1>Functional Interfaces</h1>
<p>A <strong>functional interface contains exactly one abstract method</strong>.</p>
<p>Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">interface Calculator {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    int add(int a, int b);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="functionalinterface">@FunctionalInterface<a href="https://your-docusaurus-site.example.com/java/j8#functionalinterface" class="hash-link" aria-label="Direct link to @FunctionalInterface" title="Direct link to @FunctionalInterface">​</a></h2>
<p>Optional annotation.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@FunctionalInterface</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">interface MyFunction {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    void execute();</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p>Compiler ensures only one abstract method exists.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="built-in-functional-interfaces">Built in Functional Interfaces<a href="https://your-docusaurus-site.example.com/java/j8#built-in-functional-interfaces" class="hash-link" aria-label="Direct link to Built in Functional Interfaces" title="Direct link to Built in Functional Interfaces">​</a></h2>
<p>Java provides several interfaces in <code>java.util.function</code>.</p>
<p>Important ones interviewers expect you to know:</p>
<table><thead><tr><th>Interface</th><th>Description</th></tr></thead><tbody><tr><td><code>Predicate&lt;T&gt;</code></td><td>Represents a boolean condition</td></tr><tr><td><code>Function&lt;T, R&gt;</code></td><td>Transforms a value</td></tr><tr><td><code>Consumer&lt;T&gt;</code></td><td>Accepts a value</td></tr><tr><td><code>Supplier&lt;T&gt;</code></td><td>Provides or returns a value</td></tr><tr><td><code>UnaryOperator&lt;T&gt;</code></td><td>Operation on a single operand of the same type</td></tr><tr><td><code>BinaryOperator&lt;T&gt;</code></td><td>Operation on two operands of the same type</td></tr></tbody></table>
<p>Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Predicate&lt;Integer&gt; isEven = n -&gt; n % 2 == 0;</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="custom-functional-interface">Custom Functional Interface<a href="https://your-docusaurus-site.example.com/java/j8#custom-functional-interface" class="hash-link" aria-label="Direct link to Custom Functional Interface" title="Direct link to Custom Functional Interface">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@FunctionalInterface</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">interface Greeting {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    void sayHello(String name);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p>Usage:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Greeting g = name -&gt; System.out.println("Hello " + name);</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="interview-trap">Interview Trap<a href="https://your-docusaurus-site.example.com/java/j8#interview-trap" class="hash-link" aria-label="Direct link to Interview Trap" title="Direct link to Interview Trap">​</a></h2>
<p>Question:</p>
<p><strong>Is Runnable a functional interface?</strong></p>
<p>Yes.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Runnable r = () -&gt; System.out.println("Running");</span><br></span></code></pre></div></div>
<hr>
<h1>Method References</h1>
<p>Method references allow referencing methods directly.</p>
<p>Syntax:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">ClassName::methodName</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="static-method-reference">Static Method Reference<a href="https://your-docusaurus-site.example.com/java/j8#static-method-reference" class="hash-link" aria-label="Direct link to Static Method Reference" title="Direct link to Static Method Reference">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Integer::parseInt</span><br></span></code></pre></div></div>
<p>Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">list.stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .map(Integer::parseInt)</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .collect(Collectors.toList());</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="instance-method-reference">Instance Method Reference<a href="https://your-docusaurus-site.example.com/java/j8#instance-method-reference" class="hash-link" aria-label="Direct link to Instance Method Reference" title="Direct link to Instance Method Reference">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">String::toLowerCase</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="constructor-reference">Constructor Reference<a href="https://your-docusaurus-site.example.com/java/j8#constructor-reference" class="hash-link" aria-label="Direct link to Constructor Reference" title="Direct link to Constructor Reference">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">User::new</span><br></span></code></pre></div></div>
<p>Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Supplier&lt;User&gt; supplier = User::new;</span><br></span></code></pre></div></div>
<hr>
<h1>Streams API</h1>
<p>Streams process collections <strong>in a functional style pipeline</strong>.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="what-problem-streams-solve">What Problem Streams Solve<a href="https://your-docusaurus-site.example.com/java/j8#what-problem-streams-solve" class="hash-link" aria-label="Direct link to What Problem Streams Solve" title="Direct link to What Problem Streams Solve">​</a></h2>
<p>Before Java 8:</p>
<ul>
<li>iteration was external</li>
<li>logic mixed with data traversal</li>
</ul>
<p>Streams introduced <strong>internal iteration</strong>.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="external-vs-internal-iteration">External vs Internal Iteration<a href="https://your-docusaurus-site.example.com/java/j8#external-vs-internal-iteration" class="hash-link" aria-label="Direct link to External vs Internal Iteration" title="Direct link to External vs Internal Iteration">​</a></h2>
<p>External:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">for(int i : list)</span><br></span></code></pre></div></div>
<p>Internal:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">list.stream().filter(...)</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="lazy-evaluation">Lazy Evaluation<a href="https://your-docusaurus-site.example.com/java/j8#lazy-evaluation" class="hash-link" aria-label="Direct link to Lazy Evaluation" title="Direct link to Lazy Evaluation">​</a></h2>
<p>Intermediate operations execute <strong>only when terminal operation runs</strong>.</p>
<p>Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">list.stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .filter(x -&gt; x &gt; 5)</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .map(x -&gt; x * 2)</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .collect(Collectors.toList());</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="stream-pipeline">Stream Pipeline<a href="https://your-docusaurus-site.example.com/java/j8#stream-pipeline" class="hash-link" aria-label="Direct link to Stream Pipeline" title="Direct link to Stream Pipeline">​</a></h2>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Source -&gt; Intermediate Operations -&gt; Terminal Operation</span><br></span></code></pre></div></div>
<p>Example:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">List -&gt; filter -&gt; map -&gt; collect</span><br></span></code></pre></div></div>
<hr>
<h1>Intermediate Operations</h1>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="filter">filter<a href="https://your-docusaurus-site.example.com/java/j8#filter" class="hash-link" aria-label="Direct link to filter" title="Direct link to filter">​</a></h3>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">list.stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .filter(x -&gt; x &gt; 10)</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="map">map<a href="https://your-docusaurus-site.example.com/java/j8#map" class="hash-link" aria-label="Direct link to map" title="Direct link to map">​</a></h3>
<p>Transforms elements.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">list.stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .map(x -&gt; x * 2)</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="flatmap">flatMap<a href="https://your-docusaurus-site.example.com/java/j8#flatmap" class="hash-link" aria-label="Direct link to flatMap" title="Direct link to flatMap">​</a></h3>
<p>Flattens nested structures.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">list.stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .flatMap(List::stream)</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="distinct">distinct<a href="https://your-docusaurus-site.example.com/java/j8#distinct" class="hash-link" aria-label="Direct link to distinct" title="Direct link to distinct">​</a></h3>
<p>Removes duplicates.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">stream.distinct()</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="sorted">sorted<a href="https://your-docusaurus-site.example.com/java/j8#sorted" class="hash-link" aria-label="Direct link to sorted" title="Direct link to sorted">​</a></h3>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">stream.sorted()</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="limit">limit<a href="https://your-docusaurus-site.example.com/java/j8#limit" class="hash-link" aria-label="Direct link to limit" title="Direct link to limit">​</a></h3>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">stream.limit(5)</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="skip">skip<a href="https://your-docusaurus-site.example.com/java/j8#skip" class="hash-link" aria-label="Direct link to skip" title="Direct link to skip">​</a></h3>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">stream.skip(3)</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="peek">peek<a href="https://your-docusaurus-site.example.com/java/j8#peek" class="hash-link" aria-label="Direct link to peek" title="Direct link to peek">​</a></h3>
<p>Used for debugging.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">stream.peek(System.out::println)</span><br></span></code></pre></div></div>
<hr>
<h1>Terminal Operations</h1>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="collect">collect<a href="https://your-docusaurus-site.example.com/java/j8#collect" class="hash-link" aria-label="Direct link to collect" title="Direct link to collect">​</a></h3>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">stream.collect(Collectors.toList())</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="reduce">reduce<a href="https://your-docusaurus-site.example.com/java/j8#reduce" class="hash-link" aria-label="Direct link to reduce" title="Direct link to reduce">​</a></h3>
<p>Used for aggregation.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">int sum = list.stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">              .reduce(0, Integer::sum);</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="findfirst">findFirst<a href="https://your-docusaurus-site.example.com/java/j8#findfirst" class="hash-link" aria-label="Direct link to findFirst" title="Direct link to findFirst">​</a></h3>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">stream.findFirst()</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="anymatch">anyMatch<a href="https://your-docusaurus-site.example.com/java/j8#anymatch" class="hash-link" aria-label="Direct link to anyMatch" title="Direct link to anyMatch">​</a></h3>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">stream.anyMatch(x -&gt; x &gt; 10)</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="allmatch">allMatch<a href="https://your-docusaurus-site.example.com/java/j8#allmatch" class="hash-link" aria-label="Direct link to allMatch" title="Direct link to allMatch">​</a></h3>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">stream.allMatch(x -&gt; x &gt; 0)</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="nonematch">noneMatch<a href="https://your-docusaurus-site.example.com/java/j8#nonematch" class="hash-link" aria-label="Direct link to noneMatch" title="Direct link to noneMatch">​</a></h3>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">stream.noneMatch(x -&gt; x &lt; 0)</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="count">count<a href="https://your-docusaurus-site.example.com/java/j8#count" class="hash-link" aria-label="Direct link to count" title="Direct link to count">​</a></h3>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">stream.count()</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="min--max">min / max<a href="https://your-docusaurus-site.example.com/java/j8#min--max" class="hash-link" aria-label="Direct link to min / max" title="Direct link to min / max">​</a></h3>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">stream.max(Integer::compareTo)</span><br></span></code></pre></div></div>
<hr>
<h1>Collectors in Depth</h1>
<p>Collectors transform streams into collections.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="tolist">toList<a href="https://your-docusaurus-site.example.com/java/j8#tolist" class="hash-link" aria-label="Direct link to toList" title="Direct link to toList">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">stream.collect(Collectors.toList())</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="toset">toSet<a href="https://your-docusaurus-site.example.com/java/j8#toset" class="hash-link" aria-label="Direct link to toSet" title="Direct link to toSet">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">stream.collect(Collectors.toSet())</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="tomap">toMap<a href="https://your-docusaurus-site.example.com/java/j8#tomap" class="hash-link" aria-label="Direct link to toMap" title="Direct link to toMap">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">stream.collect(Collectors.toMap(</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    User::getId,</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    User::getName</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">));</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="groupingby">groupingBy<a href="https://your-docusaurus-site.example.com/java/j8#groupingby" class="hash-link" aria-label="Direct link to groupingBy" title="Direct link to groupingBy">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Map&lt;String, List&lt;User&gt;&gt; users =</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">users.stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">     .collect(Collectors.groupingBy(User::getDepartment));</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="partitioningby">partitioningBy<a href="https://your-docusaurus-site.example.com/java/j8#partitioningby" class="hash-link" aria-label="Direct link to partitioningBy" title="Direct link to partitioningBy">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Map&lt;Boolean, List&lt;Integer&gt;&gt; result =</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">list.stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .collect(Collectors.partitioningBy(n -&gt; n % 2 == 0));</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="joining">joining<a href="https://your-docusaurus-site.example.com/java/j8#joining" class="hash-link" aria-label="Direct link to joining" title="Direct link to joining">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">String result =</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">list.stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .collect(Collectors.joining(","));</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="counting">counting<a href="https://your-docusaurus-site.example.com/java/j8#counting" class="hash-link" aria-label="Direct link to counting" title="Direct link to counting">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Collectors.counting()</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="summarizingint">summarizingInt<a href="https://your-docusaurus-site.example.com/java/j8#summarizingint" class="hash-link" aria-label="Direct link to summarizingInt" title="Direct link to summarizingInt">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">IntSummaryStatistics stats =</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">list.stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .collect(Collectors.summarizingInt(Integer::intValue));</span><br></span></code></pre></div></div>
<hr>
<h1>Optional Class</h1>
<p>Optional represents <strong>a container that may or may not contain a value</strong>.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="why-optional-was-introduced">Why Optional Was Introduced<a href="https://your-docusaurus-site.example.com/java/j8#why-optional-was-introduced" class="hash-link" aria-label="Direct link to Why Optional Was Introduced" title="Direct link to Why Optional Was Introduced">​</a></h2>
<p>Java suffered heavily from:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">NullPointerException</span><br></span></code></pre></div></div>
<p>Optional forces developers to handle absence of value.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="example">Example<a href="https://your-docusaurus-site.example.com/java/j8#example" class="hash-link" aria-label="Direct link to Example" title="Direct link to Example">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Optional&lt;String&gt; name = Optional.of("Java");</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="map-vs-flatmap">map vs flatMap<a href="https://your-docusaurus-site.example.com/java/j8#map-vs-flatmap" class="hash-link" aria-label="Direct link to map vs flatMap" title="Direct link to map vs flatMap">​</a></h2>
<p>map transforms inside Optional.</p>
<p>flatMap avoids nested Optional.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="orelse-vs-orelseget-vs-orelsethrow">orElse vs orElseGet vs orElseThrow<a href="https://your-docusaurus-site.example.com/java/j8#orelse-vs-orelseget-vs-orelsethrow" class="hash-link" aria-label="Direct link to orElse vs orElseGet vs orElseThrow" title="Direct link to orElse vs orElseGet vs orElseThrow">​</a></h2>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">orElse(value)</span><br></span></code></pre></div></div>
<p>always evaluates.</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">orElseGet(supplier)</span><br></span></code></pre></div></div>
<p>lazy execution.</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">orElseThrow()</span><br></span></code></pre></div></div>
<p>throws exception.</p>
<hr>
<h1>Default Methods in Interfaces</h1>
<p>Java 8 allows methods with implementation inside interfaces.</p>
<p>Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">interface Vehicle {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"> default void start(){</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">   System.out.println("Starting");</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"> }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="diamond-problem">Diamond Problem<a href="https://your-docusaurus-site.example.com/java/j8#diamond-problem" class="hash-link" aria-label="Direct link to Diamond Problem" title="Direct link to Diamond Problem">​</a></h2>
<p>If two interfaces define the same default method:</p>
<p>Class must override it.</p>
<hr>
<h1>Static Methods in Interfaces</h1>
<p>Interfaces can contain static methods.</p>
<p>Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">interface MathUtil {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"> static int add(int a,int b){</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">   return a+b;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"> }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p>Call using:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">MathUtil.add(1,2);</span><br></span></code></pre></div></div>
<hr>
<h1>New Date Time API (java.time)</h1>
<p>Old APIs:</p>
<ul>
<li>Date</li>
<li>Calendar</li>
</ul>
<p>Problems:</p>
<ul>
<li>mutable</li>
<li>thread unsafe</li>
<li>confusing</li>
</ul>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="localdate">LocalDate<a href="https://your-docusaurus-site.example.com/java/j8#localdate" class="hash-link" aria-label="Direct link to LocalDate" title="Direct link to LocalDate">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">LocalDate.now()</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="localtime">LocalTime<a href="https://your-docusaurus-site.example.com/java/j8#localtime" class="hash-link" aria-label="Direct link to LocalTime" title="Direct link to LocalTime">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">LocalTime.now()</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="localdatetime">LocalDateTime<a href="https://your-docusaurus-site.example.com/java/j8#localdatetime" class="hash-link" aria-label="Direct link to LocalDateTime" title="Direct link to LocalDateTime">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">LocalDateTime.now()</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="zoneddatetime">ZonedDateTime<a href="https://your-docusaurus-site.example.com/java/j8#zoneddatetime" class="hash-link" aria-label="Direct link to ZonedDateTime" title="Direct link to ZonedDateTime">​</a></h2>
<p>Supports timezones.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">ZonedDateTime.now()</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="duration">Duration<a href="https://your-docusaurus-site.example.com/java/j8#duration" class="hash-link" aria-label="Direct link to Duration" title="Direct link to Duration">​</a></h2>
<p>Used for time based difference.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="period">Period<a href="https://your-docusaurus-site.example.com/java/j8#period" class="hash-link" aria-label="Direct link to Period" title="Direct link to Period">​</a></h2>
<p>Used for date difference.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="formatting">Formatting<a href="https://your-docusaurus-site.example.com/java/j8#formatting" class="hash-link" aria-label="Direct link to Formatting" title="Direct link to Formatting">​</a></h2>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">DateTimeFormatter formatter =</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">DateTimeFormatter.ofPattern("dd-MM-yyyy");</span><br></span></code></pre></div></div>
<hr>
<h1>Parallel Streams</h1>
<p>Parallel streams allow parallel processing.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">list.parallelStream()</span><br></span></code></pre></div></div>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="internal-working">Internal Working<a href="https://your-docusaurus-site.example.com/java/j8#internal-working" class="hash-link" aria-label="Direct link to Internal Working" title="Direct link to Internal Working">​</a></h2>
<p>Uses <strong>ForkJoinPool.commonPool()</strong>.</p>
<p>Work divided across threads.</p>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="when-not-to-use">When NOT to Use<a href="https://your-docusaurus-site.example.com/java/j8#when-not-to-use" class="hash-link" aria-label="Direct link to When NOT to Use" title="Direct link to When NOT to Use">​</a></h2>
<ul>
<li>IO operations</li>
<li>small collections</li>
<li>shared mutable state</li>
</ul>
<hr>
<h1>Nashorn JavaScript Engine</h1>
<p>Java 8 introduced Nashorn to run JavaScript inside JVM.</p>
<p>Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">ScriptEngine engine =</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">new ScriptEngineManager().getEngineByName("nashorn");</span><br></span></code></pre></div></div>
<hr>
<h1>Type Inference Improvements</h1>
<p>Java 8 improved type inference for generics and lambdas.</p>
<hr>
<h1>Java 8 forEach</h1>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">list.forEach(System.out::println);</span><br></span></code></pre></div></div>
<hr>
<h1>Base64 Encoding</h1>
<p>Java 8 added Base64 utilities.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Base64.getEncoder().encodeToString(data);</span><br></span></code></pre></div></div>
<hr>
<h1>CompletableFuture</h1>
<p>CompletableFuture enables asynchronous programming.</p>
<p>Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">CompletableFuture.supplyAsync(() -&gt; {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    return "Hello";</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}).thenApply(String::toUpperCase)</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  .thenAccept(System.out::println);</span><br></span></code></pre></div></div>
<hr>
<h1>Real Interview Coding Questions</h1>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="find-duplicate-elements">Find duplicate elements<a href="https://your-docusaurus-site.example.com/java/j8#find-duplicate-elements" class="hash-link" aria-label="Direct link to Find duplicate elements" title="Direct link to Find duplicate elements">​</a></h3>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">list.stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .collect(Collectors.groupingBy(</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        Function.identity(),</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        Collectors.counting()))</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .entrySet()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    .filter(e -&gt; e.getValue() &gt; 1)</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="convert-list-to-map">Convert List to Map<a href="https://your-docusaurus-site.example.com/java/j8#convert-list-to-map" class="hash-link" aria-label="Direct link to Convert List to Map" title="Direct link to Convert List to Map">​</a></h3>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Map&lt;Integer, User&gt; map =</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">users.stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">     .collect(Collectors.toMap(</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">         User::getId,</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">         Function.identity()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">     ));</span><br></span></code></pre></div></div>
<hr>
<h1>Top Java 8 Interview Questions</h1>
<ol>
<li>What is a functional interface</li>
<li>Difference between map and flatMap</li>
<li>Difference between stream and parallel stream</li>
<li>What is lazy evaluation</li>
<li>What happens when multiple terminal operations are used</li>
<li>Difference between Optional.of and Optional.ofNullable</li>
<li>Difference between reduce and collect</li>
<li>What is invokedynamic</li>
<li>What is method reference</li>
<li>When should you use Optional</li>
</ol>
<hr>
<h1>Common Java 8 Interview Mistakes</h1>
<ol>
<li>Overusing parallel streams</li>
<li>Using streams for simple loops</li>
<li>Misusing Optional as field variables</li>
<li>Ignoring immutability</li>
<li>Not understanding lazy evaluation</li>
</ol>
<hr>
<h1>Java 8 Interview Cheat Sheet</h1>
<p>Core concepts interviewers check:</p>
<ul>
<li>Functional interfaces</li>
<li>Lambda expressions</li>
<li>Streams pipeline</li>
<li>Optional</li>
<li>Method references</li>
<li>Default methods</li>
<li>CompletableFuture</li>
<li>Date Time API</li>
<li>Collectors</li>
<li>Parallel streams</li>
</ul>
<p>If a developer understands these deeply, they can handle <strong>nearly every Java 8 interview question</strong>.</p>
<hr>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Functional Interfaces]]></title>
            <link>https://your-docusaurus-site.example.com/java/fi</link>
            <guid>https://your-docusaurus-site.example.com/java/fi</guid>
            <pubDate>Fri, 13 Mar 2026 07:33:51 GMT</pubDate>
            <description><![CDATA[I was working on a large financial platform. The codebase had grown for nearly a decade. Every release introduced more complexity, more classes, and more boilerplate.]]></description>
            <content:encoded><![CDATA[<p>I was working on a large financial platform. The codebase had grown for nearly a decade. Every release introduced more complexity, more classes, and more boilerplate.</p>
<p>One day a junior developer asked a simple question.</p>
<p>“Why do we need a whole class just to run a small piece of logic?”</p>
<p>He was referring to this.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">class TaskRunner implements Runnable {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    @Override</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public void run() {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        System.out.println("Processing task...");</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">new Thread(new TaskRunner()).start();</span><br></span></code></pre></div></div>
<p>For experienced Java developers, this looked normal. But for someone coming from modern languages, it looked… excessive.</p>
<p>Just to execute a small block of code, we needed a full class.</p>
<p>And this pattern was everywhere.</p>
<p>Sorting collections. Running background tasks. Writing callbacks. Handling events.</p>
<p>Everything required interfaces, anonymous classes, and verbose implementations.</p>
<p>Then Java 8 arrived. And things changed.</p>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="the-real-problem-java-was-trying-to-solve">The Real Problem Java Was Trying to Solve<a href="https://your-docusaurus-site.example.com/java/fi#the-real-problem-java-was-trying-to-solve" class="hash-link" aria-label="Direct link to The Real Problem Java Was Trying to Solve" title="Direct link to The Real Problem Java Was Trying to Solve">​</a></h3>
<p>Before Java 8, Java lacked a concise way to pass behavior as data.</p>
<p>If we wanted to pass logic into a method, we usually had to create an anonymous class.</p>
<p>Example: sorting a list.</p>
<p>Before Java 8:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Collections.sort(names, new Comparator&lt;String&gt;() {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    @Override</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public int compare(String a, String b) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        return a.compareTo(b);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">});</span><br></span></code></pre></div></div>
<p>A lot of code… for a very simple comparison.</p>
<p>Java needed a simpler way to represent small pieces of behavior.</p>
<p>That is where <strong>Functional Interfaces</strong> come in.</p>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="what-is-a-functional-interface">What is a Functional Interface?<a href="https://your-docusaurus-site.example.com/java/fi#what-is-a-functional-interface" class="hash-link" aria-label="Direct link to What is a Functional Interface?" title="Direct link to What is a Functional Interface?">​</a></h3>
<p>A <strong>Functional Interface</strong> is simply an interface that contains <strong>exactly one abstract method</strong>.</p>
<p>That’s it.</p>
<p>Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@FunctionalInterface</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public interface Calculator {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    int calculate(int a, int b);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p>Because there is only one abstract method, Java can represent this interface using a <strong>lambda expression</strong>.</p>
<p>The <code>@FunctionalInterface</code> annotation is optional, but extremely useful. It tells the compiler:</p>
<p>"This interface is intended to have only one abstract method."</p>
<p>If someone accidentally adds another method, the compiler will throw an error.</p>
<p>This protects the design.</p>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="why-functional-interfaces-enabled-lambdas">Why Functional Interfaces Enabled Lambdas<a href="https://your-docusaurus-site.example.com/java/fi#why-functional-interfaces-enabled-lambdas" class="hash-link" aria-label="Direct link to Why Functional Interfaces Enabled Lambdas" title="Direct link to Why Functional Interfaces Enabled Lambdas">​</a></h3>
<p>Java needed a target type for lambda expressions.</p>
<p>That target type is a <strong>Functional Interface</strong>.</p>
<p>Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Calculator add = (a, b) -&gt; a + b;</span><br></span></code></pre></div></div>
<p>The lambda <code>(a, b) -&gt; a + b</code> becomes the implementation of the single method inside <code>Calculator</code>.</p>
<p>Without Functional Interfaces, lambdas would have no type to map to.</p>
<p>That is why these two concepts were introduced together.</p>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="some-functional-interfaces-you-use-every-day">Some Functional Interfaces You Use Every Day<a href="https://your-docusaurus-site.example.com/java/fi#some-functional-interfaces-you-use-every-day" class="hash-link" aria-label="Direct link to Some Functional Interfaces You Use Every Day" title="Direct link to Some Functional Interfaces You Use Every Day">​</a></h3>
<p>The Java standard library already provides many powerful functional interfaces.</p>
<p>Let’s look at a few.</p>
<p><strong>Runnable</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Runnable task = () -&gt; System.out.println("Running task");</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">new Thread(task).start();</span><br></span></code></pre></div></div>
<p>What used to require a full class now fits in one line.</p>
<hr>
<p><strong>Comparator</strong></p>
<p>Sorting became much cleaner.</p>
<p>Before Java 8 we saw the verbose version.</p>
<p>Now:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">names.sort((a, b) -&gt; a.compareTo(b));</span><br></span></code></pre></div></div>
<p>Or even better:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">names.sort(String::compareTo);</span><br></span></code></pre></div></div>
<hr>
<p><strong>Predicate</strong></p>
<p>Used for conditions and filtering.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Predicate&lt;Integer&gt; isEven = n -&gt; n % 2 == 0;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">System.out.println(isEven.test(10));</span><br></span></code></pre></div></div>
<hr>
<p><strong>Function</strong></p>
<p>Transforms data.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Function&lt;String, Integer&gt; length = str -&gt; str.length();</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">System.out.println(length.apply("Java"));</span><br></span></code></pre></div></div>
<hr>
<p><strong>Consumer</strong></p>
<p>Consumes a value without returning anything.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Consumer&lt;String&gt; printer = msg -&gt; System.out.println(msg);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">printer.accept("Hello Java");</span><br></span></code></pre></div></div>
<p>These simple interfaces power most of the functional style in modern Java.</p>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="where-functional-interfaces-shine-streams">Where Functional Interfaces Shine: Streams<a href="https://your-docusaurus-site.example.com/java/fi#where-functional-interfaces-shine-streams" class="hash-link" aria-label="Direct link to Where Functional Interfaces Shine: Streams" title="Direct link to Where Functional Interfaces Shine: Streams">​</a></h3>
<p>Streams would not exist without Functional Interfaces.</p>
<p>Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">List&lt;Integer&gt; numbers = List.of(1,2,3,4,5,6);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">numbers.stream()</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">       .filter(n -&gt; n % 2 == 0)</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">       .map(n -&gt; n * n)</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">       .forEach(System.out::println);</span><br></span></code></pre></div></div>
<p>Behind the scenes:</p>
<p>filter uses <strong>Predicate</strong>
map uses <strong>Function</strong>
forEach uses <strong>Consumer</strong></p>
<p>Streams are essentially pipelines built using Functional Interfaces.</p>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="why-this-matters-in-modern-backend-systems">Why This Matters in Modern Backend Systems<a href="https://your-docusaurus-site.example.com/java/fi#why-this-matters-in-modern-backend-systems" class="hash-link" aria-label="Direct link to Why This Matters in Modern Backend Systems" title="Direct link to Why This Matters in Modern Backend Systems">​</a></h3>
<p>In microservices and backend systems, we constantly deal with:</p>
<ul>
<li>event processing</li>
<li>asynchronous workflows</li>
<li>reactive pipelines</li>
<li>stream transformations</li>
<li>callback based APIs</li>
</ul>
<p>Functional Interfaces make these patterns much cleaner.</p>
<p>Instead of designing dozens of small classes, we pass behavior directly.</p>
<p>This leads to:</p>
<p>Less boilerplate
More expressive code
Easier parallel processing
Cleaner APIs</p>
<p>Many modern frameworks rely heavily on this style.</p>
<p>Spring, Reactor, CompletableFuture, and even internal enterprise platforms.</p>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="common-mistakes-developers-make">Common Mistakes Developers Make<a href="https://your-docusaurus-site.example.com/java/fi#common-mistakes-developers-make" class="hash-link" aria-label="Direct link to Common Mistakes Developers Make" title="Direct link to Common Mistakes Developers Make">​</a></h3>
<p>After reviewing hundreds of codebases, a few mistakes appear repeatedly.</p>
<p><strong>1. Creating unnecessary custom functional interfaces</strong></p>
<p>Java already provides many in <code>java.util.function</code>.</p>
<p>Always check before creating your own.</p>
<hr>
<p><strong>2. Writing overly complex lambdas</strong></p>
<p>Lambdas should stay small and readable.</p>
<p>If logic becomes large, move it to a method.</p>
<hr>
<p><strong>3. Ignoring method references</strong></p>
<p>Sometimes this:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">list.forEach(x -&gt; System.out.println(x));</span><br></span></code></pre></div></div>
<p>Can simply be:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">list.forEach(System.out::println);</span><br></span></code></pre></div></div>
<p>Cleaner and more expressive.</p>
<hr>
<p><strong>4. Forgetting functional interfaces can have default methods</strong></p>
<p>They can contain:</p>
<ul>
<li>default methods</li>
<li>static methods</li>
</ul>
<p>They just cannot have more than <strong>one abstract method</strong>.</p>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="a-small-insight-from-years-of-java-architecture">A Small Insight From Years of Java Architecture<a href="https://your-docusaurus-site.example.com/java/fi#a-small-insight-from-years-of-java-architecture" class="hash-link" aria-label="Direct link to A Small Insight From Years of Java Architecture" title="Direct link to A Small Insight From Years of Java Architecture">​</a></h3>
<p>The biggest shift Java 8 introduced was not lambdas.</p>
<p>It was a <strong>change in thinking</strong>.</p>
<p>We moved from:</p>
<p>“Everything must be an object.”</p>
<p>To:</p>
<p>“Sometimes behavior itself is the object.”</p>
<p>Functional Interfaces are the bridge that made that transition possible while keeping Java strongly typed and backward compatible.</p>
<p>It was one of the smartest design decisions the Java language team ever made.</p>
<hr>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cheat Sheet]]></title>
            <link>https://your-docusaurus-site.example.com/java/cheat-sheet</link>
            <guid>https://your-docusaurus-site.example.com/java/cheat-sheet</guid>
            <pubDate>Thu, 12 Mar 2026 07:25:33 GMT</pubDate>
            <description><![CDATA[---]]></description>
            <content:encoded><![CDATA[<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="core-java-essentials"><strong>Core Java Essentials</strong><a href="https://your-docusaurus-site.example.com/java/cheat-sheet#core-java-essentials" class="hash-link" aria-label="Direct link to core-java-essentials" title="Direct link to core-java-essentials">​</a></h2>
<p><strong>OOP Concepts</strong></p>
<ul>
<li>Encapsulation, Inheritance, Polymorphism, Abstraction</li>
<li><code>abstract class</code> vs <code>interface</code>, <code>final</code> keyword</li>
<li>Overloading vs Overriding</li>
</ul>
<p><strong>Collections Quick Reference</strong></p>
<ul>
<li><code>ArrayList</code> vs <code>LinkedList</code> (Random access vs insertion efficiency)</li>
<li><code>HashMap</code> (bucket-based, collisions via linked list/tree)</li>
<li><code>Set</code> vs <code>List</code> vs <code>Map</code></li>
<li>Concurrent: <code>ConcurrentHashMap</code>, <code>CopyOnWriteArrayList</code></li>
</ul>
<p><strong>Streams &amp; Lambdas</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">List&lt;Integer&gt; nums = Arrays.asList(1,2,3);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">nums.stream().filter(n -&gt; n%2==0).forEach(System.out::println);</span><br></span></code></pre></div></div>
<ul>
<li>Intermediate vs Terminal operations</li>
<li><code>map</code>, <code>reduce</code>, <code>collect</code>, parallel streams</li>
</ul>
<p><strong>Concurrency Utilities</strong></p>
<ul>
<li><code>synchronized</code>, <code>ReentrantLock</code>, <code>volatile</code>, <code>AtomicInteger</code></li>
<li>Executors: Thread pools (<code>Executors.newFixedThreadPool</code>)</li>
<li><code>CountDownLatch</code>, <code>CyclicBarrier</code>, <code>Semaphore</code>, <code>ThreadLocal</code></li>
</ul>
<p><strong>JVM &amp; Memory</strong></p>
<ul>
<li>Heap, Stack, Method Area, PC Register</li>
<li>GC types: Minor, Major, G1, CMS</li>
<li>References: Strong, Weak, Soft, Phantom</li>
</ul>
<p><strong>Design Patterns</strong></p>
<ul>
<li>Singleton, Factory, Builder, Observer, Strategy, Template, Command, Facade, Prototype, Adapter</li>
</ul>
<p><strong>Advanced Java</strong></p>
<ul>
<li>Reflection: <code>Class.forName()</code>, <code>Method.invoke()</code></li>
<li>Generics: <code>List&lt;String&gt; list</code></li>
<li>Optional: <code>Optional.ofNullable(x).ifPresent(...)</code></li>
<li>I/O &amp; NIO: <code>Files.readAllLines(path)</code></li>
<li>Records: <code>record User(String name, int age){}</code></li>
<li>Pattern matching: <code>if(obj instanceof String s)</code></li>
</ul>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="spring-framework-essentials"><strong>Spring Framework Essentials</strong><a href="https://your-docusaurus-site.example.com/java/cheat-sheet#spring-framework-essentials" class="hash-link" aria-label="Direct link to spring-framework-essentials" title="Direct link to spring-framework-essentials">​</a></h2>
<p><strong>Dependency Injection</strong></p>
<ul>
<li>Constructor (preferred), Setter, Field</li>
<li><code>@Component</code>, <code>@Service</code>, <code>@Repository</code>, <code>@Controller</code></li>
<li>Bean scopes: singleton, prototype, request, session, application</li>
</ul>
<p><strong>AOP</strong></p>
<ul>
<li>Cross-cutting: Logging, Transactions, Security</li>
<li>Annotations: <code>@Aspect</code>, <code>@Before</code>, <code>@After</code>, <code>@Around</code></li>
</ul>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@Around("@annotation(LogExecutionTime)")</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public Object logTime(ProceedingJoinPoint jp){...}</span><br></span></code></pre></div></div>
<p><strong>Spring MVC</strong></p>
<ul>
<li><code>@Controller</code> vs <code>@RestController</code></li>
<li>Validation: <code>@Valid</code>, <code>BindingResult</code></li>
<li>Exception handling: <code>@ControllerAdvice</code>, <code>@ExceptionHandler</code></li>
<li>Path variables: <code>@PathVariable</code>, request params: <code>@RequestParam</code></li>
</ul>
<p><strong>Spring Data JPA</strong></p>
<ul>
<li>Repositories: <code>JpaRepository</code>, <code>CrudRepository</code></li>
<li>Custom queries: <code>@Query("SELECT u FROM User u WHERE u.age&gt;:age")</code></li>
<li>Transactions: <code>@Transactional</code> (propagation, isolation, readOnly)</li>
<li>Lazy vs Eager fetching, N+1 problem</li>
</ul>
<p><strong>Spring Security</strong></p>
<ul>
<li>AuthenticationManager, UserDetailsService, PasswordEncoder</li>
<li>JWT, OAuth2, <code>@PreAuthorize</code> for method-level security</li>
</ul>
<p><strong>Event-driven</strong></p>
<ul>
<li><code>ApplicationEventPublisher</code> + <code>@EventListener</code></li>
<li>Async events: <code>@Async</code> + TaskExecutor</li>
</ul>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="spring-boot--microservices"><strong>Spring Boot &amp; Microservices</strong><a href="https://your-docusaurus-site.example.com/java/cheat-sheet#spring-boot--microservices" class="hash-link" aria-label="Direct link to spring-boot--microservices" title="Direct link to spring-boot--microservices">​</a></h2>
<p><strong>Spring Boot Basics</strong></p>
<ul>
<li>Auto-configuration, Starters, Profiles</li>
<li><code>@SpringBootApplication</code> = <code>@Configuration + @EnableAutoConfiguration + @ComponentScan</code></li>
</ul>
<p><strong>Actuator &amp; Monitoring</strong></p>
<ul>
<li>Endpoints: <code>/health</code>, <code>/metrics</code>, <code>/beans</code></li>
<li>Integration: Prometheus, Grafana</li>
</ul>
<p><strong>Testing</strong></p>
<ul>
<li>Unit: JUnit + Mockito</li>
<li>Integration: <code>@SpringBootTest</code></li>
<li>Web: <code>@WebMvcTest</code></li>
<li>Repository: <code>@DataJpaTest</code></li>
</ul>
<p><strong>REST API Best Practices</strong></p>
<ul>
<li>HTTP verbs, resource-based URIs, versioning</li>
<li>DTOs, centralized exception handling, pagination, sorting</li>
</ul>
<p><strong>Microservices</strong></p>
<ul>
<li>Service discovery: Eureka</li>
<li>Feign clients, circuit breakers (Resilience4J)</li>
<li>Event-driven messaging: Kafka, RabbitMQ</li>
<li>Stateless services, caching, configuration management</li>
</ul>
<p><strong>Security &amp; Production</strong></p>
<ul>
<li>JWT authentication, OAuth2</li>
<li>Method-level authorization</li>
<li>Thread pools, retries, rate-limiting, fallback strategies</li>
<li>Logging, monitoring, metrics</li>
</ul>
<hr>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="common-pitfalls--interview-tips"><strong>Common Pitfalls &amp; Interview Tips</strong><a href="https://your-docusaurus-site.example.com/java/cheat-sheet#common-pitfalls--interview-tips" class="hash-link" aria-label="Direct link to common-pitfalls--interview-tips" title="Direct link to common-pitfalls--interview-tips">​</a></h2>
<ul>
<li>Avoid circular dependencies; use setter injection if necessary</li>
<li>Lazy loading across services → DTOs or projections</li>
<li>Always encode passwords; never trust client-side tokens</li>
<li>Proper transactional boundaries; avoid self-invocation issues</li>
<li>Isolate unit tests; use integration tests for end-to-end verification</li>
<li>Use thread pools wisely; avoid unbounded threads</li>
<li>Explain trade-offs in design: sync vs async, caching, DB fetch strategies</li>
</ul>
<hr>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Get Started - Java & Spring Boot]]></title>
            <link>https://your-docusaurus-site.example.com/java/gs</link>
            <guid>https://your-docusaurus-site.example.com/java/gs</guid>
            <pubDate>Thu, 12 Mar 2026 07:25:33 GMT</pubDate>
            <description><![CDATA[Alt Text]]></description>
            <content:encoded><![CDATA[<p><img decoding="async" loading="lazy" alt="Alt Text" src="https://your-docusaurus-site.example.com/assets/images/img-c40bd3c18e3ee861688b1f1da8685b62.png" width="1536" height="1024" class="img_ev3q"></p>
<p><strong>Part 1: Core Java Deep Dive</strong></p>
<ol>
<li>Java Basics Refresher – Data Types, Loops, Strings, Exception Handling</li>
<li>Object-Oriented Programming – Inheritance, Polymorphism, Encapsulation, Abstraction</li>
<li>Collections Framework – Lists, Sets, Maps, Queues, Advanced usage, Performance considerations</li>
<li>Streams &amp; Functional Programming – Streams, Lambdas, Method references, Parallel streams</li>
<li>Concurrency &amp; Multithreading – Thread lifecycle, Executors, Synchronization, Locks, Concurrent collections</li>
<li>JVM Internals – Class loading, Memory model, Heap vs Stack, Garbage Collection, GC tuning</li>
<li>Design Patterns – Singleton, Factory, Builder, Observer, Strategy, Dependency Injection patterns</li>
<li>Advanced Java Topics – Reflection, Generics, Annotations, Serialization, I/O &amp; NIO, Optional usage</li>
</ol>
<p><strong>Part 2: Spring Framework Mastery</strong></p>
<ol>
<li>Dependency Injection – Constructor, Setter, Field Injection, @Component, @Service, @Repository distinctions</li>
<li>Spring Bean Lifecycle &amp; Scopes – Singleton, Prototype, Request, Session</li>
<li>Aspect-Oriented Programming – @Aspect, @Before, @After, @Around, Real-world logging &amp; security</li>
<li>Spring MVC – Controllers, RequestMapping, PathVariables, REST best practices</li>
<li>Spring Data JPA – Repositories, Transactions, JPQL, Criteria API</li>
<li>Spring Security – Authentication, Authorization, JWT, OAuth2, Role-based access control</li>
<li>Event-driven Spring – ApplicationEventPublisher, Async events, Messaging</li>
</ol>
<p><strong>Part 3: Spring Boot &amp; Microservices</strong></p>
<ol>
<li>Spring Boot Fundamentals – Auto-configuration, Starters, Properties, Profiles</li>
<li>Spring Boot Actuator &amp; Monitoring – Health, Metrics, Logging, Micrometer integration</li>
<li>Spring Boot Testing – Unit tests, Integration tests, @MockBean, @WebMvcTest</li>
<li>REST API Design – Exception handling, HATEOAS, versioning, pagination, performance</li>
<li>Spring Boot Microservices – Service discovery (Eureka), REST clients (Feign, RestTemplate), Circuit breakers, Resilience4J</li>
<li>Event-driven Microservices – Kafka/RabbitMQ integration, Async processing</li>
<li>Security &amp; Production Hardening – JWT, OAuth2, rate-limiting, request validation, logging</li>
</ol>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Part 1: Core Java Deep Dive]]></title>
            <link>https://your-docusaurus-site.example.com/java/part-1</link>
            <guid>https://your-docusaurus-site.example.com/java/part-1</guid>
            <pubDate>Thu, 12 Mar 2026 07:25:33 GMT</pubDate>
            <description><![CDATA[---]]></description>
            <content:encoded><![CDATA[<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-1-java-basics"><strong>Section 1: Java Basics</strong><a href="https://your-docusaurus-site.example.com/java/part-1#section-1-java-basics" class="hash-link" aria-label="Direct link to section-1-java-basics" title="Direct link to section-1-java-basics">​</a></h3>
<p><strong>Q1: What are the main features of Java?</strong>
<strong>Answer:</strong>
Java is platform-independent, object-oriented, secure, robust, multi-threaded, and supports automatic memory management. It runs on the JVM, making it “write once, run anywhere.”
<em>Tip:</em> Always mention strong typing and backward compatibility when discussing features in interviews.</p>
<p><strong>Q2: Difference between JDK, JRE, and JVM?</strong>
<strong>Answer:</strong></p>
<ul>
<li><strong>JDK</strong>: Java Development Kit – tools for development (javac, jar).</li>
<li><strong>JRE</strong>: Java Runtime Environment – JVM + standard libraries, for running Java apps.</li>
<li><strong>JVM</strong>: Java Virtual Machine – runs bytecode, handles memory, class loading, and garbage collection.</li>
</ul>
<p><strong>Q3: How does Java achieve platform independence?</strong>
<strong>Answer:</strong>
Java compiles source code into <strong>bytecode</strong>, which the JVM interprets or JIT-compiles on any platform.</p>
<p><strong>Q4: Explain the difference between primitive and reference types.</strong>
<strong>Answer:</strong>
Primitives store actual values, have fixed memory, and are faster. References point to objects in the heap. Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">int x = 10;          // primitive</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">String s = "Java";   // reference</span><br></span></code></pre></div></div>
<p><strong>Q5: What are wrapper classes and autoboxing?</strong>
<strong>Answer:</strong>
Wrapper classes (<code>Integer</code>, <code>Double</code>) allow primitives to be used as objects. Autoboxing automatically converts primitives to wrapper objects and vice versa.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Integer x = 10;      // autoboxing</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">int y = x;           // unboxing</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-2-object-oriented-programming-oop"><strong>Section 2: Object-Oriented Programming (OOP)</strong><a href="https://your-docusaurus-site.example.com/java/part-1#section-2-object-oriented-programming-oop" class="hash-link" aria-label="Direct link to section-2-object-oriented-programming-oop" title="Direct link to section-2-object-oriented-programming-oop">​</a></h3>
<p><strong>Q6: Explain the four pillars of OOP in Java.</strong>
<strong>Answer:</strong></p>
<ul>
<li><strong>Encapsulation:</strong> Hide data using private fields and getters/setters.</li>
<li><strong>Inheritance:</strong> Reuse and extend classes.</li>
<li><strong>Polymorphism:</strong> Ability to take multiple forms (method overloading &amp; overriding).</li>
<li><strong>Abstraction:</strong> Hide implementation details using abstract classes or interfaces.</li>
</ul>
<p><strong>Q7: Difference between <code>abstract class</code> and <code>interface</code>?</strong>
<strong>Answer:</strong></p>
<ul>
<li>Abstract class: Can have fields, concrete &amp; abstract methods, single inheritance.</li>
<li>Interface: Only method signatures (Java 8+ supports default/static methods), multiple inheritance allowed.
<em>Tip:</em> Mention interfaces for designing flexible APIs.</li>
</ul>
<p><strong>Q8: Explain method overloading vs overriding.</strong>
<strong>Answer:</strong></p>
<ul>
<li><strong>Overloading:</strong> Same method name, different parameters. Compile-time polymorphism.</li>
<li><strong>Overriding:</strong> Subclass provides specific implementation for parent method. Runtime polymorphism.</li>
</ul>
<p><strong>Q9: What is <code>final</code> keyword in Java?</strong>
<strong>Answer:</strong>
Used to declare constants, prevent method overriding, or class inheritance.</p>
<p><strong>Q10: Difference between <code>==</code> and <code>.equals()</code>?</strong>
<strong>Answer:</strong>
<code>==</code> compares references; <code>.equals()</code> compares object content (if overridden).</p>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-3-collections-framework"><strong>Section 3: Collections Framework</strong><a href="https://your-docusaurus-site.example.com/java/part-1#section-3-collections-framework" class="hash-link" aria-label="Direct link to section-3-collections-framework" title="Direct link to section-3-collections-framework">​</a></h3>
<p><strong>Q11: Difference between List, Set, Map?</strong>
<strong>Answer:</strong></p>
<ul>
<li>List: Ordered, allows duplicates (<code>ArrayList</code>, <code>LinkedList</code>).</li>
<li>Set: No duplicates, unordered (<code>HashSet</code>, <code>LinkedHashSet</code>, <code>TreeSet</code>).</li>
<li>Map: Key-value pairs, keys unique (<code>HashMap</code>, <code>TreeMap</code>).</li>
</ul>
<p><strong>Q12: Difference between ArrayList and LinkedList?</strong>
<strong>Answer:</strong></p>
<ul>
<li>ArrayList: Fast random access, slower insert/delete.</li>
<li>LinkedList: Fast insert/delete, slower random access.</li>
</ul>
<p><strong>Q13: Explain HashMap internal working.</strong>
<strong>Answer:</strong></p>
<ul>
<li>Uses array of buckets + linked lists or tree nodes (Java 8+).</li>
<li>Key’s hashCode determines bucket. Collisions handled via linked list or red-black tree.</li>
</ul>
<p><strong>Q14: Difference between HashMap and ConcurrentHashMap?</strong>
<strong>Answer:</strong></p>
<ul>
<li>HashMap: Not thread-safe.</li>
<li>ConcurrentHashMap: Thread-safe, uses lock striping.</li>
</ul>
<p><strong>Q15: Difference between Comparable and Comparator?</strong>
<strong>Answer:</strong></p>
<ul>
<li>Comparable: Implemented by class, natural order (<code>compareTo</code>).</li>
<li>Comparator: External class, custom ordering (<code>compare</code>).</li>
</ul>
<p><strong>Q16: Explain fail-fast and fail-safe iterators.</strong>
<strong>Answer:</strong></p>
<ul>
<li>Fail-fast: Detects concurrent modification, throws <code>ConcurrentModificationException</code> (ArrayList iterator).</li>
<li>Fail-safe: Works on copy, doesn’t throw exception (ConcurrentHashMap iterator).</li>
</ul>
<p><strong>Q17: When to use LinkedHashMap?</strong>
<strong>Answer:</strong>
When insertion order or access order must be preserved, e.g., LRU cache.</p>
<p><strong>Q18: Difference between TreeMap and HashMap?</strong>
<strong>Answer:</strong></p>
<ul>
<li>TreeMap: Sorted by keys, log(n) access, implements <code>NavigableMap</code>.</li>
<li>HashMap: Unordered, O(1) access.</li>
</ul>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-4-streams--functional-programming"><strong>Section 4: Streams &amp; Functional Programming</strong><a href="https://your-docusaurus-site.example.com/java/part-1#section-4-streams--functional-programming" class="hash-link" aria-label="Direct link to section-4-streams--functional-programming" title="Direct link to section-4-streams--functional-programming">​</a></h3>
<p><strong>Q19: What is a Stream in Java 8?</strong>
<strong>Answer:</strong>
A sequence of elements supporting aggregate operations (filter, map, reduce) in a functional style. Streams are lazy and can be parallelized.</p>
<p><strong>Q20: Difference between intermediate and terminal operations?</strong>
<strong>Answer:</strong></p>
<ul>
<li>Intermediate: Returns another stream (<code>filter</code>, <code>map</code>) – lazy evaluation.</li>
<li>Terminal: Produces result (<code>collect</code>, <code>forEach</code>) – triggers processing.</li>
</ul>
<p><strong>Q21: Example of parallel stream.</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">List&lt;Integer&gt; nums = Arrays.asList(1,2,3,4,5);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">int sum = nums.parallelStream().mapToInt(Integer::intValue).sum();</span><br></span></code></pre></div></div>
<p><strong>Q22: Functional interfaces in Java 8?</strong>
<strong>Answer:</strong>
<code>Predicate</code>, <code>Function</code>, <code>Consumer</code>, <code>Supplier</code>, <code>UnaryOperator</code>, <code>BinaryOperator</code>.
<em>Tip:</em> Prepare examples of lambda usage in interviews.</p>
<p><strong>Q23: Method references example.</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">List&lt;String&gt; names = Arrays.asList("Alice","Bob");</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">names.forEach(System.out::println);</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-5-concurrency--multithreading"><strong>Section 5: Concurrency &amp; Multithreading</strong><a href="https://your-docusaurus-site.example.com/java/part-1#section-5-concurrency--multithreading" class="hash-link" aria-label="Direct link to section-5-concurrency--multithreading" title="Direct link to section-5-concurrency--multithreading">​</a></h3>
<p><strong>Q24: Difference between process and thread?</strong>
<strong>Answer:</strong></p>
<ul>
<li>Process: Independent, own memory, heavyweight.</li>
<li>Thread: Lightweight, shares process memory, faster communication.</li>
</ul>
<p><strong>Q25: How do you create a thread in Java?</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">// Extend Thread</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">class MyThread extends Thread {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public void run() { System.out.println("Running"); }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">// Implement Runnable</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">class MyTask implements Runnable {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public void run() { System.out.println("Task"); }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><strong>Q26: What is thread safety?</strong>
<strong>Answer:</strong>
Multiple threads accessing shared resources without causing inconsistent state. Achieved via synchronized blocks, locks, atomic classes.</p>
<p><strong>Q27: Explain <code>volatile</code> keyword.</strong>
<strong>Answer:</strong>
Ensures visibility of changes to variables across threads; doesn’t guarantee atomicity.</p>
<p><strong>Q28: Difference between <code>synchronized</code> and <code>ReentrantLock</code>?</strong>
<strong>Answer:</strong></p>
<ul>
<li><code>synchronized</code>: Simpler, intrinsic lock, no tryLock.</li>
<li><code>ReentrantLock</code>: More flexible, supports tryLock, fairness, multiple conditions.</li>
</ul>
<p><strong>Q29: Explain ExecutorService usage.</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">ExecutorService executor = Executors.newFixedThreadPool(5);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">executor.submit(() -&gt; System.out.println("Task executed"));</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">executor.shutdown();</span><br></span></code></pre></div></div>
<p><strong>Q30: Difference between <code>Callable</code> and <code>Runnable</code>?</strong>
<strong>Answer:</strong></p>
<ul>
<li>Runnable: Returns nothing, cannot throw checked exceptions.</li>
<li>Callable: Returns value, can throw exception, used with <code>Future</code>.</li>
</ul>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-6-jvm-internals--memory-management"><strong>Section 6: JVM Internals &amp; Memory Management</strong><a href="https://your-docusaurus-site.example.com/java/part-1#section-6-jvm-internals--memory-management" class="hash-link" aria-label="Direct link to section-6-jvm-internals--memory-management" title="Direct link to section-6-jvm-internals--memory-management">​</a></h3>
<p><strong>Q31: Explain JVM memory model.</strong>
<strong>Answer:</strong></p>
<ul>
<li>Heap: Objects, GC-managed.</li>
<li>Stack: Method calls, local primitives.</li>
<li>Method area: Class metadata, constants.</li>
<li>PC Register &amp; Native method stack: Thread execution and native code.</li>
</ul>
<p><strong>Q32: What is garbage collection (GC)?</strong>
<strong>Answer:</strong>
Automatic memory cleanup for unreachable objects. Main collectors: Serial, Parallel, CMS, G1.</p>
<p><strong>Q33: Difference between minor and major GC?</strong>
<strong>Answer:</strong></p>
<ul>
<li>Minor GC: Cleans young generation. Fast.</li>
<li>Major/Full GC: Cleans old generation. Expensive.</li>
</ul>
<p><strong>Q34: Explain strong, weak, soft, phantom references.</strong>
<strong>Answer:</strong>
Used to control object reachability and GC behavior. Example: WeakHashMap uses weak keys.</p>
<p><strong>Q35: What is JVM tuning?</strong>
<strong>Answer:</strong>
Adjusting heap size, GC strategy, thread stack size for performance-critical applications.</p>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-7-design-patterns"><strong>Section 7: Design Patterns</strong><a href="https://your-docusaurus-site.example.com/java/part-1#section-7-design-patterns" class="hash-link" aria-label="Direct link to section-7-design-patterns" title="Direct link to section-7-design-patterns">​</a></h3>
<p><strong>Q36: Explain Singleton pattern and its thread-safe implementation.</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">public class Singleton {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    private static volatile Singleton instance;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    private Singleton() {}</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public static Singleton getInstance() {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        if (instance == null) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">            synchronized(Singleton.class) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">                if (instance == null) instance = new Singleton();</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">            }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        return instance;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><strong>Q37: Builder pattern use-case.</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">public class User {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    private String name; private int age;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    private User(Builder b) { name = b.name; age = b.age; }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public static class Builder {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        private String name; private int age;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        public Builder setName(String name){ this.name=name; return this; }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        public Builder setAge(int age){ this.age=age; return this; }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        public User build(){ return new User(this); }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><strong>Q38: Factory pattern example.</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">interface Shape { void draw(); }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">class Circle implements Shape { public void draw() { System.out.println("Circle"); } }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">class ShapeFactory {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public static Shape getShape(String type) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        if(type.equals("circle")) return new Circle();</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        return null;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><strong>Q39: Observer pattern scenario.</strong>
Used in event-driven systems: listeners notify subscribers of changes (e.g., stock price updates).</p>
<p><strong>Q40: Strategy pattern use-case.</strong>
Used to define algorithms that can be swapped at runtime (e.g., payment gateway selection).</p>
<p><strong>Section 8: Advanced Java Concepts</strong></p>
<p><strong>Q41: What is Reflection in Java and when is it used?</strong>
<strong>Answer:</strong>
Reflection allows inspection and manipulation of classes, methods, fields, and constructors at runtime. Useful in frameworks like Spring for dependency injection and testing.
Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Class&lt;?&gt; clazz = Class.forName("com.example.MyClass");</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">Method m = clazz.getMethod("myMethod");</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">m.invoke(clazz.getDeclaredConstructor().newInstance());</span><br></span></code></pre></div></div>
<p><em>Pitfall:</em> Reflection bypasses compile-time checks and can impact performance.</p>
<p><strong>Q42: Explain Generics in Java. Why are they important?</strong>
<strong>Answer:</strong>
Generics provide type safety at compile-time and reduce casting.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">List&lt;String&gt; names = new ArrayList&lt;&gt;();</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">names.add("Alice"); // type-safe</span><br></span></code></pre></div></div>
<p>Without generics, you’d need casts which are error-prone.</p>
<p><strong>Q43: Difference between <code>checked</code> and <code>unchecked</code> exceptions?</strong>
<strong>Answer:</strong></p>
<ul>
<li>Checked: Must be declared or caught (<code>IOException</code>).</li>
<li>Unchecked: Runtime exceptions, can be ignored (<code>NullPointerException</code>).</li>
</ul>
<p><strong>Q44: Explain Java Annotations and custom annotation example.</strong>
<strong>Answer:</strong>
Annotations provide metadata for classes, methods, or fields.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@Retention(RetentionPolicy.RUNTIME)</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">@Target(ElementType.METHOD)</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public @interface LogExecutionTime {}</span><br></span></code></pre></div></div>
<p><em>Tip:</em> Explain usage in Spring AOP and validation.</p>
<p><strong>Q45: Serialization and deserialization in Java?</strong>
<strong>Answer:</strong>
Serialization converts objects to bytes for storage or network transfer. Must implement <code>Serializable</code>. Deserialization reconstructs the object.
<em>Pitfall:</em> Always manage <code>serialVersionUID</code> to avoid version mismatch.</p>
<p><strong>Q46: Difference between <code>==</code> and <code>equals()</code> in Collections context?</strong>
<strong>Answer:</strong>
Hash-based collections use <code>hashCode()</code> and <code>equals()</code> to store and retrieve objects. Implement both properly to avoid incorrect behavior in HashMap or HashSet.</p>
<p><strong>Q47: Explain Optional in Java 8+.</strong>
<strong>Answer:</strong>
Optional prevents <code>NullPointerException</code> by explicitly representing absence of value.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Optional&lt;String&gt; name = Optional.ofNullable(getName());</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">name.ifPresent(System.out::println);</span><br></span></code></pre></div></div>
<p><strong>Q48: Difference between String, StringBuilder, and StringBuffer?</strong>
<strong>Answer:</strong></p>
<ul>
<li>String: Immutable, slower for concatenation.</li>
<li>StringBuilder: Mutable, not thread-safe, fast.</li>
<li>StringBuffer: Mutable, thread-safe, slower than StringBuilder.</li>
</ul>
<p><strong>Q49: What are lambda expressions and why are they used?</strong>
<strong>Answer:</strong>
Provide a concise way to represent functional interfaces. Reduce boilerplate and enhance readability.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">List&lt;Integer&gt; nums = Arrays.asList(1,2,3);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">nums.forEach(n -&gt; System.out.println(n));</span><br></span></code></pre></div></div>
<p><strong>Q50: Explain method references and constructor references.</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">// Method reference</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">List&lt;String&gt; names = Arrays.asList("Alice","Bob");</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">names.forEach(System.out::println);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">// Constructor reference</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">Supplier&lt;List&lt;String&gt;&gt; listSupplier = ArrayList::new;</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-9-io--nio"><strong>Section 9: I/O &amp; NIO</strong><a href="https://your-docusaurus-site.example.com/java/part-1#section-9-io--nio" class="hash-link" aria-label="Direct link to section-9-io--nio" title="Direct link to section-9-io--nio">​</a></h3>
<p><strong>Q51: Difference between <code>FileReader/FileWriter</code> and <code>BufferedReader/BufferedWriter</code>?</strong>
<strong>Answer:</strong>
Buffered classes wrap basic readers/writers to improve performance with internal buffers.</p>
<p><strong>Q52: What is Java NIO?</strong>
<strong>Answer:</strong>
New I/O provides non-blocking, channel-based API. Useful for high-performance network/server applications.</p>
<p><strong>Q53: Explain Channels and Buffers.</strong>
<strong>Answer:</strong></p>
<ul>
<li>Buffer: Container for data.</li>
<li>Channel: Connection to I/O device, reads/writes buffers efficiently.</li>
</ul>
<p><strong>Q54: Example of reading a file using NIO.</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Path path = Paths.get("file.txt");</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">List&lt;String&gt; lines = Files.readAllLines(path);</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-10-exception-handling"><strong>Section 10: Exception Handling</strong><a href="https://your-docusaurus-site.example.com/java/part-1#section-10-exception-handling" class="hash-link" aria-label="Direct link to section-10-exception-handling" title="Direct link to section-10-exception-handling">​</a></h3>
<p><strong>Q55: Difference between throw and throws?</strong>
<strong>Answer:</strong></p>
<ul>
<li><code>throw</code>: Used to throw an exception object.</li>
<li><code>throws</code>: Declares exceptions a method may throw.</li>
</ul>
<p><strong>Q56: How does try-with-resources work?</strong>
<strong>Answer:</strong>
Automatically closes resources implementing <code>AutoCloseable</code>.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    System.out.println(br.readLine());</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><strong>Q57: Explain custom exception creation.</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">class MyException extends Exception {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public MyException(String message) { super(message); }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><em>Tip:</em> Useful to convey domain-specific errors in enterprise applications.</p>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-11-advanced-concurrency"><strong>Section 11: Advanced Concurrency</strong><a href="https://your-docusaurus-site.example.com/java/part-1#section-11-advanced-concurrency" class="hash-link" aria-label="Direct link to section-11-advanced-concurrency" title="Direct link to section-11-advanced-concurrency">​</a></h3>
<p><strong>Q58: Explain <code>CountDownLatch</code> and <code>CyclicBarrier</code>.</strong></p>
<ul>
<li><code>CountDownLatch</code>: One-time countdown to start or end threads.</li>
<li><code>CyclicBarrier</code>: Reusable barrier for threads to wait until all reach a point.</li>
</ul>
<p><strong>Q59: Difference between <code>Semaphore</code> and <code>ReentrantLock</code>.</strong></p>
<ul>
<li>Semaphore controls permits for resources.</li>
<li>ReentrantLock is exclusive lock with more flexibility.</li>
</ul>
<p><strong>Q60: Explain ThreadLocal.</strong>
Stores thread-specific variables, useful for per-thread DB connections or user context.</p>
<p><strong>Q61: What are atomic classes?</strong>
Classes like <code>AtomicInteger</code> provide lock-free thread-safe operations. Example:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">AtomicInteger counter = new AtomicInteger(0);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">counter.incrementAndGet();</span><br></span></code></pre></div></div>
<p><strong>Q62: Executors vs Thread creation?</strong>
Executors manage thread pool, avoid creating excessive threads manually, control lifecycle, and allow scheduling.</p>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-12-remaining-design-patterns"><strong>Section 12: Remaining Design Patterns</strong><a href="https://your-docusaurus-site.example.com/java/part-1#section-12-remaining-design-patterns" class="hash-link" aria-label="Direct link to section-12-remaining-design-patterns" title="Direct link to section-12-remaining-design-patterns">​</a></h3>
<p><strong>Q63: Prototype pattern example.</strong>
Used to clone objects efficiently.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">public class Prototype implements Cloneable {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public Prototype clone() throws CloneNotSupportedException {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        return (Prototype) super.clone();</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><strong>Q64: Adapter pattern use-case.</strong>
Converts interface of one class to another expected by client.
Example: Integrating legacy payment system with modern interface.</p>
<p><strong>Q65: Facade pattern example.</strong>
Provides simplified interface over complex subsystem.
Example: Spring’s <code>JdbcTemplate</code> abstracts JDBC complexity.</p>
<p><strong>Q66: Decorator pattern scenario.</strong>
Enhances functionality dynamically (e.g., InputStream decorators like BufferedInputStream, DataInputStream).</p>
<p><strong>Q67: Chain of Responsibility pattern.</strong>
Used in request processing chains, e.g., Spring MVC HandlerInterceptor chain.</p>
<p><strong>Q68: Command pattern example.</strong>
Encapsulates request as object. Used in task scheduling and undo operations.</p>
<p><strong>Q69: Observer vs EventListener in Java.</strong>
Observer: classic pattern. EventListener: Java-specific interface for GUI or event-driven programming.</p>
<p><strong>Q70: Strategy vs Template Method.</strong>
Strategy: runtime behavior swapping.
Template: fixed steps with customizable hooks.</p>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-13-java-8-features"><strong>Section 13: Java 8+ Features</strong><a href="https://your-docusaurus-site.example.com/java/part-1#section-13-java-8-features" class="hash-link" aria-label="Direct link to section-13-java-8-features" title="Direct link to section-13-java-8-features">​</a></h3>
<p><strong>Q71: Stream reduction and collect example.</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">List&lt;Integer&gt; nums = Arrays.asList(1,2,3,4);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">int sum = nums.stream().reduce(0, Integer::sum);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">List&lt;String&gt; names = Stream.of("Alice","Bob").collect(Collectors.toList());</span><br></span></code></pre></div></div>
<p><strong>Q72: Optional chaining example.</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Optional&lt;User&gt; user = findUser();</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">String email = user.map(User::getEmail).orElse("notfound@example.com");</span><br></span></code></pre></div></div>
<p><strong>Q73: Default methods in interfaces.</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">interface Logger {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    default void log(String msg) { System.out.println(msg); }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><strong>Q74: Java 9 Modules – purpose?</strong>
Encapsulation of packages and better dependency control for large applications.</p>
<p><strong>Q75: Var keyword in Java 10.</strong>
Type inference for local variables.</p>
<p><strong>Q76: Record classes in Java 16+.</strong>
Compact immutable data carriers.</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">record User(String name, int age) {}</span><br></span></code></pre></div></div>
<p><strong>Q77: Sealed classes (Java 17).</strong>
Restrict subclassing for better maintainability.</p>
<p><strong>Q78: Pattern matching with instanceof.</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">if(obj instanceof String s) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    System.out.println(s.length());</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><strong>Q79: Text blocks (Java 15).</strong>
Multi-line string literals:</p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">String html = """</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    &lt;html&gt;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        &lt;body&gt;Hello&lt;/body&gt;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    &lt;/html&gt;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">""";</span><br></span></code></pre></div></div>
<p><strong>Q80: Records + Streams combined example.</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">List&lt;User&gt; users = List.of(new User("Alice",25), new User("Bob",30));</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">List&lt;String&gt; names = users.stream().map(User::name).collect(Collectors.toList());</span><br></span></code></pre></div></div>
<hr>
<hr>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Part 2: Spring Framework Mastery]]></title>
            <link>https://your-docusaurus-site.example.com/java/part-2</link>
            <guid>https://your-docusaurus-site.example.com/java/part-2</guid>
            <pubDate>Thu, 12 Mar 2026 07:25:33 GMT</pubDate>
            <description><![CDATA[---]]></description>
            <content:encoded><![CDATA[<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-1-dependency-injection-di"><strong>Section 1: Dependency Injection (DI)</strong><a href="https://your-docusaurus-site.example.com/java/part-2#section-1-dependency-injection-di" class="hash-link" aria-label="Direct link to section-1-dependency-injection-di" title="Direct link to section-1-dependency-injection-di">​</a></h3>
<p><strong>Q1: What is Dependency Injection and why is it important?</strong>
<strong>Answer:</strong>
DI decouples object creation from usage. It promotes loose coupling, easier testing, and maintainability. Spring supports:</p>
<ul>
<li><strong>Constructor injection</strong>: Preferred for mandatory dependencies.</li>
<li><strong>Setter injection</strong>: Optional dependencies.</li>
<li><strong>Field injection</strong>: Quick, but harder to test.</li>
</ul>
<p><strong>Example:</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@Service</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public class OrderService {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    private final PaymentService paymentService;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    @Autowired</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public OrderService(PaymentService paymentService) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        this.paymentService = paymentService;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><em>Tip:</em> Always explain trade-offs and testing implications.</p>
<p><strong>Q2: Difference between <code>@Component</code>, <code>@Service</code>, <code>@Repository</code>, <code>@Controller</code>?</strong></p>
<ul>
<li><code>@Component</code>: Generic stereotype.</li>
<li><code>@Service</code>: Business logic layer.</li>
<li><code>@Repository</code>: Data access, converts SQL exceptions to Spring exceptions.</li>
<li><code>@Controller</code>: Presentation layer for MVC web apps.</li>
</ul>
<p><strong>Q3: Bean scopes in Spring?</strong></p>
<ul>
<li>Singleton: One instance per container (default).</li>
<li>Prototype: New instance each injection.</li>
<li>Request: Per HTTP request.</li>
<li>Session: Per HTTP session.</li>
<li>Application: Global per ServletContext.</li>
</ul>
<p><strong>Q4: Lazy vs Eager initialization?</strong></p>
<ul>
<li>Eager: Instantiated at startup.</li>
<li>Lazy: Instantiated when first requested.
Useful for optimizing startup time.</li>
</ul>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-2-spring-bean-lifecycle"><strong>Section 2: Spring Bean Lifecycle</strong><a href="https://your-docusaurus-site.example.com/java/part-2#section-2-spring-bean-lifecycle" class="hash-link" aria-label="Direct link to section-2-spring-bean-lifecycle" title="Direct link to section-2-spring-bean-lifecycle">​</a></h3>
<p><strong>Q5: How does Spring manage bean lifecycle?</strong></p>
<ul>
<li>Instantiation → Dependency Injection → Post-construct initialization → Ready → Pre-destroy → Destruction.</li>
<li>Key interfaces: <code>InitializingBean</code>, <code>DisposableBean</code>, <code>BeanPostProcessor</code>, <code>@PostConstruct</code>, <code>@PreDestroy</code>.</li>
</ul>
<p><strong>Q6: Explain circular dependency resolution in Spring.</strong></p>
<ul>
<li>Spring can resolve circular dependencies for singleton beans via setter injection but not with constructor injection.</li>
<li>Real-world scenario: Two services referencing each other must use setter injection to avoid <code>BeanCurrentlyInCreationException</code>.</li>
</ul>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-3-aspect-oriented-programming-aop"><strong>Section 3: Aspect-Oriented Programming (AOP)</strong><a href="https://your-docusaurus-site.example.com/java/part-2#section-3-aspect-oriented-programming-aop" class="hash-link" aria-label="Direct link to section-3-aspect-oriented-programming-aop" title="Direct link to section-3-aspect-oriented-programming-aop">​</a></h3>
<p><strong>Q7: What is AOP and why is it used?</strong></p>
<ul>
<li>Separates cross-cutting concerns like logging, security, and transactions.</li>
<li>Key annotations: <code>@Aspect</code>, <code>@Before</code>, <code>@After</code>, <code>@Around</code>, <code>@AfterReturning</code>, <code>@AfterThrowing</code>.</li>
</ul>
<p><strong>Q8: Example: Logging method execution time</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@Aspect</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">@Component</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public class LoggingAspect {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    @Around("@annotation(LogExecutionTime)")</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public Object logTime(ProceedingJoinPoint joinPoint) throws Throwable {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        long start = System.currentTimeMillis();</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        Object result = joinPoint.proceed();</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        System.out.println("Execution time: " + (System.currentTimeMillis() - start) + "ms");</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        return result;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><em>Tip:</em> In interviews, explain proxy-based implementation (JDK vs CGLIB).</p>
<p><strong>Q9: Difference between <code>@Aspect</code> and <code>BeanPostProcessor</code></strong></p>
<ul>
<li>AOP: Dynamic proxies for method interception.</li>
<li>BeanPostProcessor: Hook into bean initialization lifecycle. Both can handle cross-cutting logic, but use-case differs.</li>
</ul>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-4-spring-mvc"><strong>Section 4: Spring MVC</strong><a href="https://your-docusaurus-site.example.com/java/part-2#section-4-spring-mvc" class="hash-link" aria-label="Direct link to section-4-spring-mvc" title="Direct link to section-4-spring-mvc">​</a></h3>
<p><strong>Q10: Explain Spring MVC architecture.</strong></p>
<ul>
<li>DispatcherServlet → Controllers → Services → Repositories → View Resolver.</li>
<li>Handles request mapping, validation, and response rendering.</li>
</ul>
<p><strong>Q11: Difference between <code>@Controller</code> and <code>@RestController</code></strong></p>
<ul>
<li><code>@Controller</code>: Returns view names, requires <code>@ResponseBody</code> for JSON.</li>
<li><code>@RestController</code>: Implicitly adds <code>@ResponseBody</code>, used for REST APIs.</li>
</ul>
<p><strong>Q12: How does Spring handle form validation?</strong></p>
<ul>
<li>Use <code>@Valid</code> or <code>@Validated</code> on request objects with JSR-303 annotations like <code>@NotNull</code>, <code>@Size</code>.</li>
<li>Example:</li>
</ul>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@PostMapping("/create")</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public ResponseEntity&lt;?&gt; createUser(@Valid @RequestBody UserDto user, BindingResult result) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    if(result.hasErrors()) return ResponseEntity.badRequest().body(result.getAllErrors());</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    return ResponseEntity.ok(userService.create(user));</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><strong>Q13: Explain path variables and request params.</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@GetMapping("/users/{id}")</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public User getUser(@PathVariable Long id, @RequestParam(required=false) String filter) { }</span><br></span></code></pre></div></div>
<p><strong>Q14: Exception handling in Spring MVC</strong></p>
<ul>
<li>Use <code>@ControllerAdvice</code> with <code>@ExceptionHandler</code> to centralize exception handling.</li>
</ul>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-5-spring-data-jpa"><strong>Section 5: Spring Data JPA</strong><a href="https://your-docusaurus-site.example.com/java/part-2#section-5-spring-data-jpa" class="hash-link" aria-label="Direct link to section-5-spring-data-jpa" title="Direct link to section-5-spring-data-jpa">​</a></h3>
<p><strong>Q15: What is Spring Data JPA?</strong></p>
<ul>
<li>Simplifies repository layer using <code>JpaRepository</code> or <code>CrudRepository</code>.</li>
<li>Provides methods like <code>save()</code>, <code>findAll()</code>, <code>deleteById()</code> automatically.</li>
</ul>
<p><strong>Q16: Custom queries using <code>@Query</code></strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@Query("SELECT u FROM User u WHERE u.age &gt; :age")</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">List&lt;User&gt; findUsersOlderThan(@Param("age") int age);</span><br></span></code></pre></div></div>
<p><strong>Q17: Transaction management</strong></p>
<ul>
<li>Use <code>@Transactional</code>. Key attributes: propagation, isolation, readOnly.</li>
<li>Pitfall: Avoid transactional methods calling private methods (self-invocation bypasses proxy).</li>
</ul>
<p><strong>Q18: Difference between <code>save()</code> and <code>saveAndFlush()</code></strong></p>
<ul>
<li><code>save()</code>: Persist entity, may delay flush.</li>
<li><code>saveAndFlush()</code>: Immediately flush to DB.</li>
</ul>
<p><strong>Q19: Lazy vs Eager fetching</strong></p>
<ul>
<li>Lazy: Loaded on demand, avoids unnecessary queries.</li>
<li>Eager: Loaded immediately, may lead to performance issues.</li>
<li>Interview Tip: Explain N+1 problem and solution with <code>@EntityGraph</code>.</li>
</ul>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-6-spring-security"><strong>Section 6: Spring Security</strong><a href="https://your-docusaurus-site.example.com/java/part-2#section-6-spring-security" class="hash-link" aria-label="Direct link to section-6-spring-security" title="Direct link to section-6-spring-security">​</a></h3>
<p><strong>Q20: How does Spring Security work?</strong></p>
<ul>
<li>Filters authenticate requests, authorize based on roles, secure endpoints, support OAuth2/JWT.</li>
<li>Core components: AuthenticationManager, UserDetailsService, PasswordEncoder.</li>
</ul>
<p><strong>Q21: Example: Securing REST APIs with JWT</strong></p>
<ul>
<li>Authenticate → generate JWT → send to client → validate JWT in filter for each request.</li>
</ul>
<p><strong>Q22: Password encoding</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@Bean</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public PasswordEncoder passwordEncoder() {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    return new BCryptPasswordEncoder();</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><strong>Q23: Role-based vs Method-level security</strong></p>
<ul>
<li><code>@PreAuthorize("hasRole('ADMIN')")</code> for method-level access control.</li>
<li>Pitfall: Always encode passwords, avoid storing plain text.</li>
</ul>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-7-event-driven-design-in-spring"><strong>Section 7: Event-Driven Design in Spring</strong><a href="https://your-docusaurus-site.example.com/java/part-2#section-7-event-driven-design-in-spring" class="hash-link" aria-label="Direct link to section-7-event-driven-design-in-spring" title="Direct link to section-7-event-driven-design-in-spring">​</a></h3>
<p><strong>Q24: How does Spring handle events?</strong></p>
<ul>
<li>Use <code>ApplicationEventPublisher</code> and <code>@EventListener</code>.</li>
<li>Example:</li>
</ul>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@Component</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public class OrderPublisher {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    @Autowired ApplicationEventPublisher publisher;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public void publish(Order order) {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        publisher.publishEvent(new OrderCreatedEvent(this, order));</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">@Component</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public class OrderListener {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    @EventListener</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    public void handle(OrderCreatedEvent event) { /* send notification */ }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><strong>Q25: Asynchronous events</strong></p>
<ul>
<li>Annotate listener with <code>@Async</code> and configure <code>TaskExecutor</code> for non-blocking processing.</li>
</ul>
<p><strong>Q26: Difference between synchronous and asynchronous events</strong></p>
<ul>
<li>Synchronous: Listener executes in same thread.</li>
<li>Asynchronous: Executes in separate thread, better for decoupling and performance.</li>
</ul>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-8-spring-best-practices--pitfalls"><strong>Section 8: Spring Best Practices &amp; Pitfalls</strong><a href="https://your-docusaurus-site.example.com/java/part-2#section-8-spring-best-practices--pitfalls" class="hash-link" aria-label="Direct link to section-8-spring-best-practices--pitfalls" title="Direct link to section-8-spring-best-practices--pitfalls">​</a></h3>
<ol>
<li>Prefer constructor injection over field injection.</li>
<li>Avoid circular dependencies; use setter injection or <code>@Lazy</code>.</li>
<li>Keep services stateless for thread safety.</li>
<li>Use <code>@Transactional</code> carefully; understand propagation and rollback rules.</li>
<li>Always handle exceptions centrally with <code>@ControllerAdvice</code>.</li>
<li>Avoid eager loading large associations in entities; prefer DTOs or projections.</li>
</ol>
<hr>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Part 3: Spring Boot & Microservices]]></title>
            <link>https://your-docusaurus-site.example.com/java/part-3</link>
            <guid>https://your-docusaurus-site.example.com/java/part-3</guid>
            <pubDate>Thu, 12 Mar 2026 07:25:33 GMT</pubDate>
            <description><![CDATA[---]]></description>
            <content:encoded><![CDATA[<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-1-spring-boot-fundamentals"><strong>Section 1: Spring Boot Fundamentals</strong><a href="https://your-docusaurus-site.example.com/java/part-3#section-1-spring-boot-fundamentals" class="hash-link" aria-label="Direct link to section-1-spring-boot-fundamentals" title="Direct link to section-1-spring-boot-fundamentals">​</a></h3>
<p><strong>Q1: What is Spring Boot and why use it?</strong>
<strong>Answer:</strong>
Spring Boot simplifies Spring application setup by providing auto-configuration, embedded servers, opinionated defaults, and production-ready features. It reduces boilerplate and accelerates microservice development.</p>
<p><strong>Q2: Explain auto-configuration in Spring Boot.</strong></p>
<ul>
<li>Spring Boot detects classes on the classpath and configures beans automatically.</li>
<li>Example: <code>spring-boot-starter-web</code> automatically sets up <code>DispatcherServlet</code>, <code>Jackson</code>, and embedded Tomcat.</li>
<li>Pitfall: Conflicting dependencies can lead to unexpected bean creation.</li>
</ul>
<p><strong>Q3: What are Spring Boot Starters?</strong></p>
<ul>
<li>Predefined dependencies that simplify setup.</li>
<li>Examples: <code>spring-boot-starter-web</code>, <code>spring-boot-starter-data-jpa</code>, <code>spring-boot-starter-security</code>.</li>
</ul>
<p><strong>Q4: Profiles in Spring Boot</strong></p>
<ul>
<li>Support environment-specific configuration (<code>application-dev.yml</code>, <code>application-prod.yml</code>).</li>
<li>Activate via <code>spring.profiles.active=dev</code>.</li>
</ul>
<p><strong>Q5: Difference between <code>@SpringBootApplication</code> and <code>@EnableAutoConfiguration</code></strong></p>
<ul>
<li><code>@SpringBootApplication</code> combines: <code>@Configuration</code>, <code>@EnableAutoConfiguration</code>, <code>@ComponentScan</code>.</li>
</ul>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-2-spring-boot-actuator--monitoring"><strong>Section 2: Spring Boot Actuator &amp; Monitoring</strong><a href="https://your-docusaurus-site.example.com/java/part-3#section-2-spring-boot-actuator--monitoring" class="hash-link" aria-label="Direct link to section-2-spring-boot-actuator--monitoring" title="Direct link to section-2-spring-boot-actuator--monitoring">​</a></h3>
<p><strong>Q6: What is Spring Boot Actuator?</strong></p>
<ul>
<li>Provides production-ready endpoints to monitor and manage applications.</li>
<li>Common endpoints: <code>/health</code>, <code>/metrics</code>, <code>/env</code>, <code>/loggers</code>.</li>
</ul>
<p><strong>Q7: How to customize actuator endpoints?</strong></p>
<div class="language-yaml codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-yaml codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token key atrule">management</span><span class="token punctuation" style="color:rgb(199, 146, 234)">:</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token key atrule">endpoints</span><span class="token punctuation" style="color:rgb(199, 146, 234)">:</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token key atrule">web</span><span class="token punctuation" style="color:rgb(199, 146, 234)">:</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">      </span><span class="token key atrule">exposure</span><span class="token punctuation" style="color:rgb(199, 146, 234)">:</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        </span><span class="token key atrule">include</span><span class="token punctuation" style="color:rgb(199, 146, 234)">:</span><span class="token plain"> health</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> metrics</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> beans</span><br></span></code></pre></div></div>
<p><strong>Q8: Integrating with Prometheus &amp; Grafana</strong></p>
<ul>
<li>Expose <code>/actuator/prometheus</code> and configure Prometheus to scrape metrics.</li>
<li>Pitfall: Avoid exposing sensitive endpoints in production.</li>
</ul>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-3-spring-boot-testing"><strong>Section 3: Spring Boot Testing</strong><a href="https://your-docusaurus-site.example.com/java/part-3#section-3-spring-boot-testing" class="hash-link" aria-label="Direct link to section-3-spring-boot-testing" title="Direct link to section-3-spring-boot-testing">​</a></h3>
<p><strong>Q9: Types of Spring Boot tests</strong></p>
<ul>
<li>Unit Tests: Test classes in isolation using JUnit + Mockito.</li>
<li>Integration Tests: <code>@SpringBootTest</code> loads context for real interactions.</li>
<li>Web Layer Tests: <code>@WebMvcTest</code> for controller testing.</li>
</ul>
<p><strong>Q10: Example – Unit Test with MockBean</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@SpringBootTest</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">class OrderServiceTest {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    @MockBean</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    PaymentService paymentService;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    @Autowired</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    OrderService orderService;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    @Test</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    void processOrder() {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        Mockito.when(paymentService.pay()).thenReturn(true);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        assertTrue(orderService.processOrder());</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><strong>Q11: Test tips for interviews</strong></p>
<ul>
<li>Always isolate external dependencies.</li>
<li>Use <code>@DataJpaTest</code> for repository testing.</li>
<li>Cover both positive and negative scenarios.</li>
</ul>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-4-rest-api-design--best-practices"><strong>Section 4: REST API Design &amp; Best Practices</strong><a href="https://your-docusaurus-site.example.com/java/part-3#section-4-rest-api-design--best-practices" class="hash-link" aria-label="Direct link to section-4-rest-api-design--best-practices" title="Direct link to section-4-rest-api-design--best-practices">​</a></h3>
<p><strong>Q12: Key principles</strong></p>
<ul>
<li>Use proper HTTP verbs (<code>GET</code>, <code>POST</code>, <code>PUT</code>, <code>DELETE</code>).</li>
<li>Resource-based URIs: <code>/users/{id}/orders</code>.</li>
<li>Versioning: <code>/api/v1/users</code>.</li>
<li>Consistent error handling with <code>@ControllerAdvice</code>.</li>
</ul>
<p><strong>Q13: Pagination &amp; Sorting</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Page&lt;User&gt; findAll(Pageable pageable);</span><br></span></code></pre></div></div>
<p><strong>Q14: HATEOAS (optional)</strong></p>
<ul>
<li>Add hypermedia links to REST responses to guide clients.</li>
</ul>
<p><strong>Q15: Security considerations</strong></p>
<ul>
<li>Validate input, use DTOs instead of entities, encode passwords, avoid exposing sensitive fields.</li>
</ul>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-5-spring-boot-microservices"><strong>Section 5: Spring Boot Microservices</strong><a href="https://your-docusaurus-site.example.com/java/part-3#section-5-spring-boot-microservices" class="hash-link" aria-label="Direct link to section-5-spring-boot-microservices" title="Direct link to section-5-spring-boot-microservices">​</a></h3>
<p><strong>Q16: What is a microservice?</strong></p>
<ul>
<li>Small, independently deployable service, focused on a single business capability.</li>
<li>Benefits: Scalability, decoupling, maintainability.</li>
</ul>
<p><strong>Q17: Key Spring Cloud tools</strong></p>
<ul>
<li>Service Discovery: Eureka</li>
<li>Configuration: Spring Cloud Config</li>
<li>API Gateway: Spring Cloud Gateway</li>
<li>Circuit Breaker: Resilience4J</li>
</ul>
<p><strong>Q18: Example – Service Discovery with Eureka</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@SpringBootApplication</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">@EnableEurekaServer</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public class EurekaServerApplication { }</span><br></span></code></pre></div></div>
<ul>
<li>Microservice registers itself with Eureka using <code>@EnableEurekaClient</code>.</li>
</ul>
<p><strong>Q19: Inter-service communication</strong></p>
<ul>
<li>REST (RestTemplate, WebClient)</li>
<li>Feign Client: Declarative REST</li>
</ul>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@FeignClient(name="payment-service")</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public interface PaymentClient {</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    @GetMapping("/pay")</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    boolean pay();</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">}</span><br></span></code></pre></div></div>
<p><strong>Q20: Circuit breaker example (Resilience4J)</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">@CircuitBreaker(name="paymentService", fallbackMethod="fallbackPay")</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public boolean pay() { ... }</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public boolean fallbackPay(Throwable t) { return false; }</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-6-event-driven-microservices"><strong>Section 6: Event-Driven Microservices</strong><a href="https://your-docusaurus-site.example.com/java/part-3#section-6-event-driven-microservices" class="hash-link" aria-label="Direct link to section-6-event-driven-microservices" title="Direct link to section-6-event-driven-microservices">​</a></h3>
<p><strong>Q21: Why event-driven?</strong></p>
<ul>
<li>Decouples services, improves scalability, ensures eventual consistency.</li>
</ul>
<p><strong>Q22: Spring + Kafka example</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">// Producer</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">@Autowired KafkaTemplate&lt;String, Order&gt; kafkaTemplate;</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">kafkaTemplate.send("orders", order);</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">// Consumer</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">@KafkaListener(topics="orders")</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">public void consume(Order order) { process(order); }</span><br></span></code></pre></div></div>
<p><strong>Q23: Async processing</strong></p>
<ul>
<li>Use <code>@Async</code> with ThreadPoolTaskExecutor for non-blocking execution.</li>
<li>Pitfall: Thread pools must be tuned for load; unbounded threads can crash services.</li>
</ul>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-7-security-in-microservices"><strong>Section 7: Security in Microservices</strong><a href="https://your-docusaurus-site.example.com/java/part-3#section-7-security-in-microservices" class="hash-link" aria-label="Direct link to section-7-security-in-microservices" title="Direct link to section-7-security-in-microservices">​</a></h3>
<p><strong>Q24: JWT Authentication</strong></p>
<ul>
<li>Stateless, signed token containing user info and roles.</li>
<li>Example flow: Login → generate JWT → client sends in <code>Authorization: Bearer</code> header → validate JWT in filter.</li>
</ul>
<p><strong>Q25: OAuth2 &amp; OpenID Connect</strong></p>
<ul>
<li>Used for SSO and token-based authentication.</li>
<li>Pitfall: Never trust client-side tokens without validation.</li>
</ul>
<p><strong>Q26: Securing endpoints</strong></p>
<div class="language-java codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-java codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">http</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  .authorizeHttpRequests(auth -&gt; auth</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">      .requestMatchers("/admin/**").hasRole("ADMIN")</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">      .anyRequest().authenticated())</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  .oauth2ResourceServer().jwt();</span><br></span></code></pre></div></div>
<hr>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="section-8-production--performance-best-practices"><strong>Section 8: Production &amp; Performance Best Practices</strong><a href="https://your-docusaurus-site.example.com/java/part-3#section-8-production--performance-best-practices" class="hash-link" aria-label="Direct link to section-8-production--performance-best-practices" title="Direct link to section-8-production--performance-best-practices">​</a></h3>
<ol>
<li>Keep services stateless.</li>
<li>Externalize configuration using Spring Cloud Config.</li>
<li>Enable logging and monitoring (Actuator + Prometheus).</li>
<li>Tune JVM and thread pools for high throughput.</li>
<li>Use DTOs, avoid lazy-loading pitfalls across microservice boundaries.</li>
<li>Apply caching (Redis, Caffeine) for frequently accessed data.</li>
<li>Implement retries, rate-limiting, and fallback strategies for resilience.</li>
</ol>
<hr>]]></content:encoded>
        </item>
    </channel>
</rss>