<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nikhil Sharma on Medium]]></title>
        <description><![CDATA[Stories by Nikhil Sharma on Medium]]></description>
        <link>https://medium.com/@nikhil8.jsg?source=rss-1f544615f9f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*zkJnLlbY8-h3lFcn</url>
            <title>Stories by Nikhil Sharma on Medium</title>
            <link>https://medium.com/@nikhil8.jsg?source=rss-1f544615f9f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 04 Jun 2026 10:19:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nikhil8.jsg/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Java 8: Lambda Functions]]></title>
            <link>https://medium.com/@nikhil8.jsg/java-8-lambda-functions-055f1447acd1?source=rss-1f544615f9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/055f1447acd1</guid>
            <category><![CDATA[java-8-feature]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Nikhil Sharma]]></dc:creator>
            <pubDate>Tue, 21 Jan 2025 09:40:46 GMT</pubDate>
            <atom:updated>2025-01-21T09:40:46.008Z</atom:updated>
            <content:encoded><![CDATA[<p>In Java SE 8, <strong>Lambda Expressions</strong> were introduced as a way to simplify coding and enable functional programming. They provide a concise way to write the implementation of functional interfaces (interfaces with a single abstract method), allowing developers to represent functionality as code blocks that can be passed around and executed.</p><h3>What Are Lambda Expressions?</h3><p>Lambda expressions eliminate the need for verbose anonymous classes when implementing functional interfaces. With Lambda, you can write cleaner and more expressive code.</p><p>For example, before Java 8, implementing a functional interface required writing a separate class:</p><h4>Code Without Lambda Expressions:</h4><pre>interface FunctionalInterfaceExample {<br>    void abstractMethod(String str);<br>}<br>class InterfaceImplementation implements FunctionalInterfaceExample {<br>    @Override<br>    public void abstractMethod(String str) {<br>        System.out.println(str.length());<br>    }<br>}<br>public class Main {<br>    public static void main(String[] args) {<br>        FunctionalInterfaceExample pie = new InterfaceImplementation();<br>        pie.abstractMethod(&quot;String Example&quot;);<br>    }<br>}</pre><p>With Lambda Expressions, the same functionality can be achieved concisely:</p><h4>Code With Lambda Expressions:</h4><pre>interface FunctionalInterfaceExample {<br>    void abstractMethod(String str);<br>}<br>public class Main {<br>    public static void main(String[] args) {<br>        FunctionalInterfaceExample pie = (x) -&gt; System.out.println(x.length());<br>        pie.abstractMethod(&quot;String Example&quot;);<br>    }<br>}</pre><h3>Syntax of Lambda Expressions</h3><p>The general syntax of a lambda expression is:</p><pre>(arguments) -&gt; {body of the expression}Components of Lambda Expressions:</pre><ol><li><strong>Arguments</strong>: Parameters passed to the method.</li><li><strong>Arrow Token (</strong><strong>-&gt;)</strong>: Separates the arguments from the body.</li><li><strong>Body</strong>: Logic to be executed.</li></ol><p>Example:</p><pre>(int a) -&gt; System.out.println(a * 2);</pre><h3>Types of Lambda Expressions</h3><h4>Based on Lines of Code:</h4><ol><li><strong>Single-line Lambda</strong>: Contains a single statement.<br>Example:</li></ol><pre>() -&gt; System.out.println(&quot;Hello World&quot;);</pre><p><strong>2. Block Lambda</strong>: Contains multiple statements enclosed in braces {}.<br>Example:</p><pre>() -&gt; {     <br>  System.out.println(&quot;Hello&quot;);     <br>  System.out.println(&quot;World&quot;); <br>};</pre><h4>Based on Parameters:</h4><ol><li><strong>Zero Parameter</strong>: Lambda expression with no arguments.</li></ol><pre>() -&gt; System.out.println(&quot;Zero parameter lambda&quot;);</pre><p><strong>2. Single Parameter</strong>: Lambda expression with one argument.</p><pre>(x) -&gt; System.out.println(&quot;Single parameter: &quot; + x); <br>// Parentheses can be omitted if there&#39;s one parameter <br>x -&gt; System.out.println(&quot;Single parameter: &quot; + x);</pre><p><strong>3. Multiple Parameters</strong>: Lambda expression with more than one argument.</p><pre>(x, y) -&gt; System.out.println(&quot;Multiple parameters: &quot; + x + y);</pre><h3>Examples Using Lambda Expressions</h3><p>Below is an example demonstrating the use of zero, single, and multiple-parameter Lambda expressions:</p><pre>interface FunctionalInterfaceNoParam {<br>    void noParamMethod();<br>}<br>interface FunctionalInterfaceOneParam {<br>    void oneParam(String str);<br>}<br>interface FunctionalInterfaceMultipleParam {<br>    void multiParamMethod(String a, String b);<br>}<br>public class Main {<br>    public static void main(String[] args) {<br>        FunctionalInterfaceNoParam noParam = () -&gt; System.out.println(&quot;No Parameter Lambda Example&quot;);<br>        noParam.noParamMethod();<br>        <br>        FunctionalInterfaceOneParam singleParam = (str) -&gt; System.out.println(str);<br>        singleParam.oneParam(&quot;Single Parameter Lambda Example&quot;);<br>        <br>        FunctionalInterfaceOneParam singleParam1 = str -&gt; System.out.println(str);<br>        singleParam1.oneParam(&quot;Single Parameter Lambda Example&quot;);<br>        <br>        FunctionalInterfaceMultipleParam multiParam = (a, b) -&gt; System.out.println(a + b);<br>        multiParam.multiParamMethod(&quot;Multi&quot;, &quot; Param&quot;);<br>    }<br>}</pre><pre>Output:<br><br>No Parameter Lambda Example<br>Single Parameter Lambda Example<br>Single Parameter Lambda Example<br>Multi Param</pre><h3>Using Predefined Functional Interfaces</h3><p>Instead of creating custom functional interfaces, we can use <strong>predefined functional interfaces</strong> like Runnable, Consumer, and BiConsumer.</p><h4>Example:</h4><pre>import java.util.function.Consumer;<br>import java.util.function.BiConsumer;<br>public class Main {<br>    public static void main(String[] args) {<br>        Runnable noParamPredefined = () -&gt; System.out.println(&quot;No Parameter Predefined Example&quot;);<br>        noParamPredefined.run();<br>        <br>        Consumer&lt;String&gt; singleParamPredefined = str -&gt; System.out.println(str);<br>        singleParamPredefined.accept(&quot;Single Parameter Predefined Example&quot;);<br>        <br>        BiConsumer&lt;String, String&gt; multiParamPredefined = (a, b) -&gt; System.out.println(a + b);<br>        multiParamPredefined.accept(&quot;Multi&quot;, &quot; Param Predefined&quot;);<br>    }<br>}</pre><h4>Output:</h4><pre>No Parameter Predefined Example  <br>Single Parameter Predefined Example  <br>Multi Param Predefined</pre><h3>Lambda Expressions With Collections</h3><p>Lambda expressions can be seamlessly used with <strong>Collections</strong> for operations like iteration.</p><h4>1. Using List:</h4><pre>import java.util.ArrayList;<br>import java.util.List;<br>public class Main {<br>    public static void main(String[] args) {<br>        List&lt;Integer&gt; list = new ArrayList&lt;&gt;();<br>        list.add(1);<br>        list.add(2);<br>        list.add(3);<br>        list.forEach(x -&gt; System.out.println(&quot;The value is &quot; + x));<br>    }<br>}</pre><h4>2. Using Set:</h4><pre>import java.util.HashSet;<br>import java.util.Set;<br>public class Main {<br>    public static void main(String[] args) {<br>        Set&lt;Integer&gt; set = new HashSet&lt;&gt;();<br>        set.add(1);<br>        set.add(2);<br>        set.forEach(x -&gt; System.out.println(&quot;The value is &quot; + x));<br>    }<br>}</pre><h4>3. Using Map:</h4><pre>import java.util.HashMap;<br>import java.util.Map;<br>public class Main {<br>    public static void main(String[] args) {<br>        Map&lt;Integer, Character&gt; map = new HashMap&lt;&gt;();<br>        map.put(1, &#39;a&#39;);<br>        map.put(2, &#39;b&#39;);<br>        map.forEach((key, value) -&gt; {<br>            System.out.println(&quot;Key: &quot; + key + &quot;, Value: &quot; + value);<br>        });<br>    }<br>}</pre><h3>Scope of Variables</h3><ol><li>We cannot modify a variable directly inside a lambda expression unless it is effectively final, the code is an example.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/852/1*4dDrBYIH5jI49u9YyBJR9Q.png" /><figcaption>It can be seen that the code while trying to access count, throws an unresolved compilation problem.</figcaption></figure><p>2. The variable can be accessed by just creating a local variable and providing our actual output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*pq_NXvTI07mVxXnALHICag.png" /><figcaption>In this image, we can see that the value of count can be printed</figcaption></figure><p>3. The variable can be modified but it has to be assigned to a local variable of lambda body, We can perform the task but it won’t modify the count variable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*brOxASlegKLV6cTj6A56qw.png" /><figcaption>The value of count is getting printed as 2, with the help of a local variable to lambda expression.</figcaption></figure><p>4. Now to increment the value of count, we’ll have to use multithreading concepts. Like here we can try to use AtomicInteger.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/1*HTZLTh8VfeObvNxZ6VaQeA.png" /><figcaption>The value of count is getting increased on each iteration</figcaption></figure><h3>Conclusion</h3><p>Lambda Expressions in Java 8 have revolutionized the way functional programming is approached. By introducing a concise and readable syntax, they enable developers to simplify the implementation of functional interfaces. Whether you’re working with single-line expressions or handling multiple parameters, Lambdas empower you to write cleaner, more expressive code. They are especially useful when combined with Java’s predefined functional interfaces and Collections API, making tasks like iteration and data processing seamless.</p><p>Furthermore, understanding the scope of variables in Lambda expressions is key to effectively utilizing them, especially when dealing with multithreading or shared state.</p><p>As we move forward, exploring the <strong>scope of variables in Lambda expressions</strong> will enhance your understanding of how to manage variables within functional programming paradigms.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=055f1447acd1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java 8: Functional Interfaces]]></title>
            <link>https://medium.com/@nikhil8.jsg/java-8-functional-interfaces-fbc77c3ef1e7?source=rss-1f544615f9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/fbc77c3ef1e7</guid>
            <category><![CDATA[java-8-feature]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[java-functional-interface]]></category>
            <category><![CDATA[java8]]></category>
            <dc:creator><![CDATA[Nikhil Sharma]]></dc:creator>
            <pubDate>Tue, 21 Jan 2025 05:05:10 GMT</pubDate>
            <atom:updated>2025-01-21T05:05:10.796Z</atom:updated>
            <content:encoded><![CDATA[<h3>Functional Interface</h3><ul><li>A <strong>Functional Interface</strong> is a type of interface that contains <strong>only one abstract method</strong>.</li><li>It was introduced in <strong>Java 8</strong> to support functional programming.</li><li>Functional Interfaces are designed to <strong>fulfil a single purpose</strong>.</li></ul><h4>Defining a Functional Interface</h4><p>There are multiple ways to define a Functional Interface:</p><ol><li><strong>Creating an Interface with One Abstract Method</strong></li></ol><ul><li>Create an interface with one abstract method.</li><li><strong>Problem</strong>: Without additional safeguards, adding more than one abstract method is possible, violating the core definition of a Functional Interface.</li><li>To prevent this issue, the @FunctionalInterface annotation was introduced.</li></ul><pre>interface Person {<br>    void hello();<br>}</pre><p><strong>2. Using the </strong><strong>@FunctionalInterface Annotation</strong></p><ul><li>When you use the @FunctionalInterface annotation, it enforces that the interface contains only <strong>one abstract method</strong>.</li><li>This helps ensure the correct use of the Functional Interface and prevents accidental additions of more abstract methods.</li></ul><pre>@FunctionalInterface<br>interface Person {<br>    void hello();<br>    <br>    static String helloFromString() {<br>        return &quot;Hello from interface String&quot;;<br>    }<br>}</pre><p><strong>3. Functional Interfaces with Return Values</strong></p><ul><li>A Functional Interface can have an abstract method that returns a value based on requirements.</li></ul><pre>@FunctionalInterface<br>interface Person {<br>    String hello();<br>    <br>    static String helloFromString() {<br>        return &quot;Hello from interface String&quot;;<br>    }<br>}</pre><p><strong>4. Functional Interfaces with Parameters</strong></p><ul><li>A Functional Interface can also have an abstract method that accepts parameters.</li></ul><pre>@FunctionalInterface<br>interface Person {<br>    String hello(String str);<br>    <br>    static String helloFromString() {<br>        return &quot;Hello from interface String&quot;;<br>    }<br>}</pre><h3>Functional Interfaces: Static, Default Methods, and Extensions</h3><p>As the name suggests, a <strong>Functional Interface</strong> can only have <strong>one abstract method</strong>. However, it is allowed to have multiple <strong>static</strong> or <strong>default</strong> methods without violating its definition.</p><h4>Example with Static and Default Methods</h4><pre>@FunctionalInterface<br>interface Person {<br>    void hello(); // abstract method<br>    static String helloFromString1() {<br>        return &quot;Hello from interface String 1&quot;;<br>    }<br>    static String helloFromString2() {<br>        return &quot;Hello from interface String 2&quot;;<br>    }<br>    default void defaultExample1() {<br>        System.out.println(&quot;Default Example 1&quot;);<br>    }<br>    default void defaultExample2() {<br>        System.out.println(&quot;Default Example 2&quot;);<br>    }<br>}</pre><p>In the example above, the Person interface is still a valid <strong>Functional Interface</strong> because it has only one abstract method, hello(), even though it includes multiple <strong>static</strong> and <strong>default</strong> methods.</p><h3>Extending a Functional Interface</h3><p>A Functional Interface can be extended by another interface without violating its definition. However, the child interface must ensure it doesn’t add additional abstract methods if it wants to remain a Functional Interface.</p><h4>Example: Extending a Functional Interface</h4><pre>@FunctionalInterface<br>interface Person {<br>    void hello();  // abstract method<br>    static String helloFromString1() {<br>        return &quot;Hello from interface String 1&quot;;<br>    }<br>    static String helloFromString2() {<br>        return &quot;Hello from interface String 2&quot;;<br>    }<br>    default void defaultExample1() {<br>        System.out.println(&quot;Default Example 1&quot;);<br>    }<br>    default void defaultExample2() {<br>        System.out.println(&quot;Default Example 2&quot;);<br>    }<br>}</pre><pre>interface Male extends Person {<br>    void world(); // abstract method<br>    default void defaultMale() {<br>        System.out.println(&quot;Default Male Example&quot;);<br>    }<br>}</pre><p>In this example, the Male interface extends the Person interface and introduces a new abstract method, world(). This makes Male a <strong>non-functional interface</strong>, while Person remains a <strong>Functional Interface</strong>.</p><h4>Making the Child Interface a Functional Interface</h4><p>To make the Male interface a Functional Interface, we can simply remove the additional abstract method (world()):</p><pre>interface Male extends Person {<br>    default void defaultMale() {<br>        System.out.println(&quot;Default Male Example&quot;);<br>    }<br>}</pre><p>Now, Male inherits the single abstract method hello() from Person and satisfies the criteria for being a <strong>Functional Interface</strong>.</p><h3>Using a Functional Interface with Lambda Expressions</h3><p>The real power of Functional Interfaces lies in their usage with <strong>Lambda Expressions</strong>. Lambda expressions allow you to define the abstract method’s implementation concisely without creating an explicit class.</p><h4>Example: Functional Interface with a Lambda Expression</h4><pre>@FunctionalInterface<br>interface Person {<br>    void hello();<br>}</pre><pre>public class Main {<br>    public static void main(String[] args) {<br>        Person lam = () -&gt; System.out.println(&quot;Lambda from Person&quot;);<br>        lam.hello();<br>    }<br>}</pre><h4>Explanation:</h4><ul><li>In the example above, no concrete implementation class for Person was created.</li><li>The hello() the method is implemented directly using a <strong>Lambda Expression</strong>.</li><li>The output will be:</li></ul><p>Lambda from Person</p><h4>Lambda Expressions Overview</h4><p>Lambda expressions provide a concise way to represent a method in a Functional Interface, enabling <strong>functional programming</strong> in Java. In the next chapter, we&#39;ll explore lambda expressions in detail.</p><h3>Predefined Functional Interfaces in Java</h3><p>Java provides several predefined <strong>functional interfaces</strong> under the java.util.function package. These interfaces can be directly used without the need to create custom functional interfaces.</p><p>Below are some commonly used predefined functional interfaces:</p><p><strong>1. Function&lt;T, R&gt;</strong></p><ul><li>Represents a function that takes one argument of type T and produces a result of type R.</li><li><strong>Example:</strong></li></ul><pre>import java.util.function.Function;  <br>public class Main {     <br>  public static void main(String[] args) {         <br>    Function&lt;String, Integer&gt; function = str -&gt; str.length();         <br>    System.out.println(function.apply(&quot;hello&quot;));     <br>  } <br>}</pre><ul><li><strong>Output:</strong></li></ul><pre>5</pre><p><strong>2. BiFunction&lt;T, U, R&gt;</strong></p><ul><li>Represents a function that takes two arguments of types T and U and produces a result of type R.</li><li><strong>Example:</strong></li></ul><pre>import java.util.function.BiFunction;  <br>public class Main {     <br>  public static void main(String[] args) {         <br>    BiFunction&lt;String, String, String&gt; function = (a, b) -&gt; a + b;         <br>    System.out.println(function.apply(&quot;hello&quot;, &quot;world&quot;));     <br>  } <br>}</pre><ul><li><strong>Output:</strong></li></ul><pre>helloworld</pre><p><strong>3. Consumer&lt;T&gt;</strong></p><ul><li>Represents an operation that takes a single argument of type T and performs some operation without returning a result.</li><li><strong>Example:</strong></li></ul><pre>import java.util.function.Consumer;  <br>public class Main {     <br>  public static void main(String[] args) {         <br>    Consumer&lt;String&gt; function = str -&gt; System.out.println(str);         <br>    function.accept(&quot;hello&quot;);     <br>  } <br>}</pre><ul><li><strong>Output:</strong></li></ul><pre>hello</pre><p><strong>4. BiConsumer&lt;T, U&gt;</strong></p><ul><li>Represents an operation that takes two arguments of types T and U and performs an operation without returning a result.</li><li><strong>Example:</strong></li></ul><pre>import java.util.function.BiConsumer;  <br>public class Main {     <br>  public static void main(String[] args) {         <br>    BiConsumer&lt;String, Integer&gt; function = (str, number) -&gt; System.out.println(String.format(&quot;The string is %s and the number is %d&quot;, str, number));         <br>    function.accept(&quot;hello&quot;, 3);     <br>  } <br>}<br></pre><ul><li><strong>Output:</strong></li></ul><pre>The string is hello and the number is 3</pre><p><strong>5. Supplier&lt;T&gt;</strong></p><ul><li>Represents a supplier of results. It does not take any arguments but produces a result of type T.</li><li><strong>Example:</strong></li></ul><pre>import java.util.function.Supplier;  <br>public class Main {     <br>  public static void main(String[] args) {         <br>    Supplier&lt;String&gt; function = () -&gt; &quot;I am a message from the Supplier&quot;;<br>    System.out.println(function.get());     <br>  } <br>}</pre><ul><li><strong>Output:</strong></li></ul><pre>I am a message from the Supplier</pre><p><strong>6. Predicate&lt;T&gt;</strong></p><ul><li>Represents a predicate (a boolean-valued function) that takes one argument of type T.</li><li><strong>Example:</strong></li></ul><pre>import java.util.function.Predicate;  <br>public class Main {     <br>   public static void main(String[] args) {         <br>      Predicate&lt;Integer&gt; function = in -&gt; in % 2 == 0;         <br>      System.out.println(function.test(6)); // true         <br>      System.out.println(function.test(7)); // false     <br>    } <br>}</pre><ul><li><strong>EOutput:</strong></li></ul><pre>true <br>false</pre><p><strong>7. BiPredicate&lt;T, U&gt;</strong></p><ul><li>Represents a predicate (a boolean-valued function) that takes two arguments of types T and U.</li><li><strong>Example:</strong></li></ul><pre>import java.util.function.BiPredicate;  <br>public class Main {     <br>  public static void main(String[] args) {         <br>    BiPredicate&lt;Integer, Integer&gt; function = (a, b) -&gt; a % b == 0;         <br>    System.out.println(function.test(6, 3)); // true         <br>    System.out.println(function.test(7, 2)); // false     <br>  } <br>}</pre><ul><li><strong>Output:</strong></li></ul><pre>true <br>false</pre><p><strong>8. UnaryOperator&lt;T&gt;</strong></p><ul><li>Represents an operation on a single operand that produces a result of the same type as the operand.</li><li><strong>Example:</strong></li></ul><pre>import java.util.function.UnaryOperator;  <br>public class Main {     <br>  public static void main(String[] args) {         <br>    UnaryOperator&lt;Integer&gt; function = a -&gt; a * a;         <br>    System.out.println(function.apply(2)); // 4     <br>  } <br>}</pre><ul><li><strong>Output:</strong></li></ul><pre>4</pre><p><strong>9. BinaryOperator&lt;T&gt;</strong></p><ul><li>Represents an operation on two operands of the same type, producing a result of the same type.</li><li><strong>Example:</strong></li></ul><pre>import java.util.function.BinaryOperator;  <br>public class Main {     <br>  public static void main(String[] args) <br>  {         <br>    BinaryOperator&lt;Integer&gt; function = (a, b) -&gt; a * b;         <br>    System.out.println(function.apply(3, 2)); // 6     <br>  } <br>}</pre><ul><li><strong>Output:</strong></li></ul><pre>6</pre><h3>Summary Chart of Functional Interfaces in java.util.function</h3><p>Here is a comprehensive summary of all the functional interfaces in the java.util.function package:</p><pre>+---------------------+-------------------------------------------------------------+------------------------+-------------+---------------------------------------------------+<br>|      Interface      |                           Purpose                           |    Abstract Method     | Utilization |                 Example Use Case                  |<br>+---------------------+-------------------------------------------------------------+------------------------+-------------+---------------------------------------------------+<br>| Function&lt;T, R&gt;      | Transforms an input of type T into an output of type R.     | R apply(T t)           | apply()     | Converting a string to its length.                |<br>| BiFunction&lt;T, U, R&gt; | Transforms two inputs (T, U) into an output of type R.      | R apply(T t, U u)      | apply()     | Concatenating two strings.                        |<br>| Consumer&lt;T&gt;         | Performs an operation on a single input of type T.          | void accept(T t)       | accept()    | Printing a message.                               |<br>| BiConsumer&lt;T, U&gt;    | Performs an operation on two inputs (T, U).                 | void accept(T t, U u)  | accept()    | Printing a string with a number.                  |<br>| Supplier&lt;T&gt;         | Supplies a value with no input.                             | T get()                | get()       | Providing a default value or lazy initialization. |<br>| Predicate&lt;T&gt;        | Evaluates a boolean condition for a single input of type T. | boolean test(T t)      | test()      | Checking if a number is even.                     |<br>| BiPredicate&lt;T, U&gt;   | Evaluates a boolean condition for two inputs (T, U).        | boolean test(T t, U u) | test()      | Checking divisibility of two numbers.             |<br>| UnaryOperator&lt;T&gt;    | Represents an operation on a single operand of type T.      | T apply(T t)           | apply()     | Squaring a number.                                |<br>| BinaryOperator&lt;T&gt;   | Represents an operation on two operands of the same type T. | T apply(T t1, T t2)    | apply()     | Multiplying two numbers.                          |<br>+---------------------+-------------------------------------------------------------+------------------------+-------------+---------------------------------------------------+</pre><h3>Conclusion</h3><p>The java.util.function package provides a rich set of <strong>predefined functional interfaces</strong> that simplify functional programming in Java. These interfaces enable concise and expressive code, especially when combined with <strong>Lambdas</strong> and the <strong>Stream API</strong>. By leveraging these functional interfaces, developers can perform common operations such as data transformation, filtering, and result generation with minimal effort.</p><h3>Next Topic: Lambda Expressions</h3><p>The next topic, <strong>Lambda Expressions</strong>, dives deeper into how these functional interfaces are implemented in Java. Lambda expressions allow us to write functional interface implementations compactly and elegantly, eliminating the need for verbose anonymous inner classes. We’ll explore the syntax, use cases, and how Lambdas enhance functional programming in Java.</p><p>Stay tuned for a hands-on exploration of Lambda expressions!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fbc77c3ef1e7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[List Interface in Java]]></title>
            <link>https://medium.com/@nikhil8.jsg/list-in-java-c244319b637b?source=rss-1f544615f9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/c244319b637b</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[collections-in-java]]></category>
            <category><![CDATA[list-in-java]]></category>
            <dc:creator><![CDATA[Nikhil Sharma]]></dc:creator>
            <pubDate>Mon, 13 Jan 2025 13:42:11 GMT</pubDate>
            <atom:updated>2025-01-13T13:50:58.852Z</atom:updated>
            <content:encoded><![CDATA[<p>In this tutorial, We’ll be exploring lists in Java.</p><p><strong>What is a List in Java?<br></strong>The List interface in Java, part of the java.util package, extends the Collection interface. It is designed to store ordered collections of elements, allowing you to manage data sequentially.</p><p><strong>What are the advantages of using a List?</strong><br>1. It maintains the order of elements as they are added.<br>2. Allows insertion of duplicate elements.<br>3. Can include null values, depending on the implementation.<br>4. ListIterator, helps to traverse through the list in both directions.</p><p><strong>Implement a List in Java?<br></strong>List&lt;Obj&gt;list = new ArrayList&lt;&gt;();<em><br></em>Here Obj is the type of Object and It can be any class or Object of your choice.</p><p><strong>Types of List in Java<br>1. ArrayList: </strong>It is a resizeable array implementation of the List interface, which allows fast random access but slower insertion or deletion compared to LinkedList. It is suitable for applications that require frequent access to elements by index.<br><strong>2. LinkedList: </strong>It is a doubly-linked list implementation of the List interface, allowing faster insertion and deletion than ArrayList slower random access. It is ideal for applications that require frequent insertion and deletion of elements.<br><strong>3. Vector: </strong>A synchronized, resizable array implementation of the List interface. It is thread-safe due to the synchronization overhead. It is ideal for applications that require thread-safe operations on lists.<br><strong>4. Stack: </strong>A subclass that implements a last-in, first-out (LIFO) stack, which provides operations like push, pop and peek making it useful for stack operations.</p><p><strong>Operations available in List:</strong><br>The List of operations available in List are:<br>1. size() <br>2. isEmpty()<br>3. contains(Object o)<br>4. iterator()<br>5. toArray()<br>6. add(E e)<br>7. remove(Object o)<br>8. containsAll(Collection&lt;?&gt; c)<br>9. add(Collection&lt;? extends E&gt; c)<br>10. add(int index, Collection&lt;? extends E&gt; c)<br>11. removeAll(Collection&lt;?&gt; c)<br>12. retainAll(Collection&lt;?&gt; c)<br>13. sort(Comparator&lt;? super E&gt; c)<br>14. equals(Object o)<br>15. clear()<br>16. hashCode()<br>17. E get(int index)<br>18. E set(int index, E element)<br>19. void add(int index, E element)<br>20. int indexOf(Object o)<br>21. E remove(int index)<br>22. int lastIndexOf(Object o)<br>23. ListIterator&lt;E&gt; listIterator()<br>24. ListIterator&lt;E&gt; listIterator(int index)<br>25. List&lt;E&gt; subList(int fromIndex, int toIndex)<br>26. &lt;E&gt; List&lt;E&gt; of(E e1, E e2)<br>27. static &lt;E&gt; List&lt;E&gt; copyOf(Collection&lt;? extends E&gt; coll)<br>and many more which can be seen in the <em>package java.util;</em></p><p><strong>Let us Dive into implementing the code for all of the above.</strong></p><p><strong><em>Problem statement 1: </em></strong><em>write a code showcasing to create a LinkedList<br></em><strong><em>Solution (Way 1): </em></strong><em>Versions before Java 10 had to add all the data sequences by increasing the code&#39;s length.</em></p><pre>import java.util.ArrayList;<br>import java.util.List;<br><br>public class Main {<br>    public static void main(String[] args) {<br>        List&lt;Integer&gt; listOfIntegers = new ArrayList&lt;&gt;();<br>        listOfIntegers.add(1);<br>        listOfIntegers.add(2);<br>        listOfIntegers.add(3);<br>        listOfIntegers.add(4);<br>        listOfIntegers.add(5);<br>        listOfIntegers.forEach(System.out::println);<br>    }<br>}</pre><p><strong><em>Solution (Way 2): </em></strong><em>Versions from Java 10 had received an </em><strong><em>of</em></strong><em> method in the List interface which reduced the code significantly.</em></p><pre>import java.util.ArrayList;<br>import java.util.List;<br><br>public class Main {<br>    public static void main(String[] args) {<br>        List&lt;Integer&gt; listOfIntegers = new ArrayList&lt;&gt;(List.of(1,2,3,4,5,6,7));<br>        listOfIntegers.forEach(System.out::println);<br>    }<br>}</pre><p><strong><em>Problem statement 2: </em></strong><em>write a code in Java to show the use of List and perform several operations on it.<br></em><strong><em>Solution:</em></strong><em> Please follow the comments to understand each line of code</em></p><pre>package com.journaldev.collections.linkedlist.example1;<br><br>import java.util.ArrayList;<br>import java.util.List;<br><br>public class Main {<br>    public static void main(String[] args) {<br>        List&lt;Integer&gt; listOfIntegers = new ArrayList&lt;&gt;(List.of(1,2,3,4,4,5,6,7));<br>        System.out.println(&quot;Initial list: &quot; + listOfIntegers);<br><br>        //To add data to the list<br>        listOfIntegers.add(8);<br>        System.out.println(&quot;After adding 8: &quot; + listOfIntegers);<br><br>        // To remove data from the list using index<br>        listOfIntegers.remove(0);<br>        System.out.println(&quot;After removing element at index 0: &quot; + listOfIntegers);<br><br>        // To remove data based on value<br>        listOfIntegers.remove(Integer.valueOf(8));<br>        System.out.println(&quot;After removing value 8: &quot; + listOfIntegers);<br><br>        // To get data from the list<br>        System.out.println(&quot;Element at index 0: &quot; + listOfIntegers.get(0));<br><br>        // To get the size of the list<br>        System.out.println(&quot;Size of list: &quot; + listOfIntegers.size());<br><br>        // To check if the list is empty<br>        System.out.println(&quot;Is list empty: &quot; + listOfIntegers.isEmpty());<br><br>        // To check if the list contains a particular value<br>        System.out.println(&quot;Does list contain 2: &quot; + listOfIntegers.contains(2));<br><br>        // To add a data in the index<br>        listOfIntegers.add(0, 0);<br>        System.out.println(&quot;After adding 0 at index 0: &quot; + listOfIntegers);<br><br>        // To get the first element of the list<br>        System.out.println(&quot;First element: &quot; + listOfIntegers.getFirst());<br><br>        // To get the last element of the list<br>        System.out.println(&quot;Last element: &quot; + listOfIntegers.getLast());<br><br>        // To get the index of a particular element<br>        System.out.println(&quot;Index of 2: &quot; + listOfIntegers.indexOf(2));<br><br>        // To get the index of a particular element<br>        System.out.println(&quot;Index of 4: &quot; + listOfIntegers.indexOf(4));<br><br>        // To get the last index of a particular element<br>        System.out.println(&quot;Last index of 2: &quot; + listOfIntegers.lastIndexOf(2));<br><br>        // To get the sublist from the list<br>        System.out.println(&quot;Sublist from index 0 to 2: &quot; + listOfIntegers.subList(0, 2));<br><br>        // To replace the element at a particular index<br>        listOfIntegers.set(0, 1);<br>        System.out.println(&quot;After replacing element at index 0 with 1: &quot; + listOfIntegers);<br><br>        System.out.print(&quot;Final list elements: &quot; + listOfIntegers);<br>    }<br>}<br><br>Output is:<br><br>Initial list: [1, 2, 3, 4, 4, 5, 6, 7]<br>After adding 8: [1, 2, 3, 4, 4, 5, 6, 7, 8]<br>After removing element at index 0: [2, 3, 4, 4, 5, 6, 7, 8]<br>After removing value 8: [2, 3, 4, 4, 5, 6, 7]<br>Element at index 0: 2<br>Size of list: 7<br>Is list empty: false<br>Does list contain 2: true<br>After adding 0 at index 0: [0, 2, 3, 4, 4, 5, 6, 7]<br>First element: 0<br>Last element: 7<br>Index of 2: 1<br>Index of 4: 3<br>Last index of 2: 1<br>Sublist from index 0 to 2: [0, 2]<br>After replacing element at index 0 with 1: [1, 2, 3, 4, 4, 5, 6, 7]<br>Final list elements: 12344567</pre><p><strong><em>Problem statement 3: </em></strong><em>write a code in Java to show the difference in operation speeds while reading or insertion and deletion in ArrayList, Vectors and LinkedList.<br></em><strong><em>Solution:</em></strong><em> Please follow the comments to understand each line of code</em></p><pre>package com.journaldev.collections.linkedlist.example1;<br><br>import java.util.ArrayList;<br>import java.util.LinkedList;<br>import java.util.List;<br>import java.util.Vector;<br><br>public class Main {<br>    public static void main(String[] args) {<br>        ArrayList&lt;Integer&gt; arrayList = new ArrayList&lt;&gt;();<br>        LinkedList&lt;Integer&gt; linkedList = new LinkedList&lt;&gt;();<br>        Vector&lt;Integer&gt; vector = new Vector&lt;&gt;();<br><br>        // Test insertion at end<br>        long startTime = System.nanoTime();<br>        for(int i = 0; i &lt; 100000; i++) {<br>            arrayList.add(i);<br>        }<br>        long endTime = System.nanoTime();<br>        System.out.println(&quot;Time taken for ArrayList insertion at end: &quot; + (endTime - startTime) + &quot; ns&quot;);<br><br>        startTime = System.nanoTime();<br>        for(int i = 0; i &lt; 100000; i++) {<br>            linkedList.add(i);<br>        }<br>        endTime = System.nanoTime();<br>        System.out.println(&quot;Time taken for LinkedList insertion at end: &quot; + (endTime - startTime) + &quot; ns&quot;);<br><br>        startTime = System.nanoTime();<br>        for(int i = 0; i &lt; 100000; i++) {<br>            vector.add(i);<br>        }<br>        endTime = System.nanoTime();<br>        System.out.println(&quot;Time taken for Vector insertion at end: &quot; + (endTime - startTime) + &quot; ns&quot;);<br><br>        // Test insertion at beginning<br>        startTime = System.nanoTime();<br>        for(int i = 0; i &lt; 1000; i++) {<br>            arrayList.add(0, i);<br>        }<br>        endTime = System.nanoTime();<br>        System.out.println(&quot;Time taken for ArrayList insertion at beginning: &quot; + (endTime - startTime) + &quot; ns&quot;);<br><br>        startTime = System.nanoTime();<br>        for(int i = 0; i &lt; 1000; i++) {<br>            linkedList.addFirst(i);<br>        }<br>        endTime = System.nanoTime();<br>        System.out.println(&quot;Time taken for LinkedList insertion at beginning: &quot; + (endTime - startTime) + &quot; ns&quot;);<br><br>        startTime = System.nanoTime();<br>        for(int i = 0; i &lt; 1000; i++) {<br>            vector.add(0, i);<br>        }<br>        endTime = System.nanoTime();<br>        System.out.println(&quot;Time taken for Vector insertion at beginning: &quot; + (endTime - startTime) + &quot; ns&quot;);<br><br>        // Test random access<br>        startTime = System.nanoTime();<br>        for(int i = 0; i &lt; 1000; i++) {<br>            arrayList.get(i);<br>        }<br>        endTime = System.nanoTime();<br>        System.out.println(&quot;Time taken for ArrayList random access: &quot; + (endTime - startTime) + &quot; ns&quot;);<br><br>        startTime = System.nanoTime();<br>        for(int i = 0; i &lt; 1000; i++) {<br>            linkedList.get(i);<br>        }<br>        endTime = System.nanoTime();<br>        System.out.println(&quot;Time taken for LinkedList random access: &quot; + (endTime - startTime) + &quot; ns&quot;);<br><br>        startTime = System.nanoTime();<br>        for(int i = 0; i &lt; 1000; i++) {<br>            vector.get(i);<br>        }<br>        endTime = System.nanoTime();<br>        System.out.println(&quot;Time taken for Vector random access: &quot; + (endTime - startTime) + &quot; ns&quot;);<br><br>        // Test deletion from beginning<br>        startTime = System.nanoTime();<br>        for(int i = 0; i &lt; 1000; i++) {<br>            arrayList.remove(0);<br>        }<br>        endTime = System.nanoTime();<br>        System.out.println(&quot;Time taken for ArrayList deletion from beginning: &quot; + (endTime - startTime) + &quot; ns&quot;);<br><br>        startTime = System.nanoTime();<br>        for(int i = 0; i &lt; 1000; i++) {<br>            linkedList.removeFirst();<br>        }<br>        endTime = System.nanoTime();<br>        System.out.println(&quot;Time taken for LinkedList deletion from beginning: &quot; + (endTime - startTime) + &quot; ns&quot;);<br><br>        startTime = System.nanoTime();<br>        for(int i = 0; i &lt; 1000; i++) {<br>            vector.remove(0);<br>        }<br>        endTime = System.nanoTime();<br>        System.out.println(&quot;Time taken for Vector deletion from beginning: &quot; + (endTime - startTime) + &quot; ns&quot;);<br>    }<br>}</pre><pre>Time taken for ArrayList insertion at end: 7714000 ns<br>Time taken for LinkedList insertion at end: 15606800 ns<br>Time taken for Vector insertion at end: 10998900 ns<br>Time taken for ArrayList insertion at beginning: 28226200 ns<br>Time taken for LinkedList insertion at beginning: 147900 ns<br>Time taken for Vector insertion at beginning: 26981300 ns<br>Time taken for ArrayList random access: 74400 ns<br>Time taken for LinkedList random access: 2685300 ns<br>Time taken for Vector random access: 45900 ns<br>Time taken for ArrayList deletion from beginning: 24694500 ns<br>Time taken for LinkedList deletion from beginning: 93200 ns<br>Time taken for Vector deletion from beginning: 18161300 ns</pre><p>As it can be seen the difference between the different List Implementation are compared below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*3i3lVyS4zSk5eILK1p8EAg.png" /><figcaption>All the values are in ns</figcaption></figure><ol><li>ArrayList performs the best with the lowest time (7,714,000).</li><li>LinkedList is the slowest (15,606,800), likely due to its need to traverse the list to find the end.</li><li>Vector is in between (10,998,900), but still slower than ArrayList.</li><li>LinkedList excels with the lowest time (147,900) due to its efficient handling of insertions at the beginning.</li><li>ArrayList and Vector are significantly slower (28,226,200 and 26,981,300, respectively) because they need to shift all elements to accommodate the new element.</li><li>ArrayList and Vector perform similarly well (74,400 and 45,900, respectively) due to their array-based structure, which allows fast random access.</li><li>LinkedList is much slower (2,685,300) because it needs to traverse the list to access elements randomly.</li><li>LinkedList is the fastest (93,200) due to its efficient handling of deletions at the beginning.</li><li>ArrayList and Vector are slower (24,694,500 and 18,161,300, respectively) because they need to shift all elements after the deletion.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/1*oz_5s3f6OLufKwybIwcLPw.png" /><figcaption>Image showing the time complexity</figcaption></figure><p><strong><em>Problem Statement 4: </em></strong><em>Write the program in Java to create a department class and show the use of an iterator<br></em><strong><em>Solution:</em></strong></p><pre>import java.util.ArrayList;<br>import java.util.Iterator;<br>import java.util.List;<br><br>class Department {<br>    private String name;<br>    private List&lt;String&gt; employees;<br><br>    public Department(String name) {<br>        this.name = name;<br>        this.employees = new ArrayList&lt;&gt;();<br>    }<br><br>    public void addEmployee(String employee) {<br>        employees.add(employee);<br>    }<br><br>    public Iterator&lt;String&gt; getEmployeeIterator() {<br>        return employees.iterator();<br>    }<br><br>    public String getName() {<br>        return name;<br>    }<br>}<br><br>public class Main {<br>    public static void main(String[] args) {<br>        // Create department<br>        Department dept = new Department(&quot;Engineering&quot;);<br><br>        // Add employees<br>        dept.addEmployee(&quot;John&quot;);<br>        dept.addEmployee(&quot;Mary&quot;);<br>        dept.addEmployee(&quot;Steve&quot;);<br><br>        // Get iterator<br>        Iterator&lt;String&gt; iterator = dept.getEmployeeIterator();<br><br>        // Iterate through employees<br>        System.out.println(&quot;Employees in &quot; + dept.getName() + &quot; department:&quot;);<br>        while(iterator.hasNext()) {<br>            String employee = iterator.next();<br>            System.out.println(employee);<br>        }<br><br>        // Use iterator to remove an employee<br>        iterator = dept.getEmployeeIterator();<br>        while(iterator.hasNext()) {<br>            String employee = iterator.next();<br>            if(employee.equals(&quot;Mary&quot;)) {<br>                iterator.remove();<br>            }<br>        }<br><br>        // Print remaining employees<br>        System.out.println(&quot;\nEmployees after removal:&quot;);<br>        iterator = dept.getEmployeeIterator();<br>        while(iterator.hasNext()) {<br>            System.out.println(iterator.next());<br>        }<br>    }<br>}</pre><pre>Employees in Engineering department:<br>John<br>Mary<br>Steve<br><br>Employees after removal:<br>John<br>Steve</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c244319b637b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploy docker-compose to Azure Container Instance]]></title>
            <link>https://medium.com/@nikhil8.jsg/deploy-docker-compose-to-azure-container-instance-6ece0e4d5492?source=rss-1f544615f9f------2</link>
            <guid isPermaLink="false">https://medium.com/p/6ece0e4d5492</guid>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Nikhil Sharma]]></dc:creator>
            <pubDate>Thu, 07 Jul 2022 07:03:20 GMT</pubDate>
            <atom:updated>2022-07-07T07:03:20.994Z</atom:updated>
            <content:encoded><![CDATA[<p>This blog is here to tell you how can you host a docker-compose app to Azure Container Instance</p><p>I went through a lot of blogs and posts on the internet as well as tutorials on youtube but was not able to find anything feasible for my work.</p><h3>Introduction</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*vWiQ__r_TwlsSWig.png" /></figure><p>Our app is built in Angular and Spring boot you can find the code here: <a href="https://github.com/Nikhil8bph/deployment.git"><strong>Github</strong></a></p><blockquote><em>Let’s get started.</em></blockquote><p>Let’s take an insight into the application:</p><ol><li>The application’s Frontend is built in <strong>Angular</strong></li><li>The application’s Backend is built in <strong>Spring boot</strong></li><li>We are going to use Docker for the deployment</li><li>You can find the <strong>docker-compose.yml</strong> that contains the run configuration</li><li>We’ll deploy the code on <a href="https://portal.azure.com/#home">Azure app services</a></li></ol><p>I guess now we are ready to dive into the code</p><h3>Dive in Practical</h3><p><strong>Step 1: </strong>create a folder, open in vs code and clone the directory</p><pre>git clone <a href="https://github.com/Nikhil8bph/deployment.git">https://github.com/Nikhil8bph/deployment.git</a> .</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/0*dfOUThBt6Q-aNwDW.png" /></figure><p><strong>Step 2: </strong>Install Azure cli from here</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/0*i_xNjUlX7WpFBCG5.png" /></figure><p><strong>Step 3: </strong>open a new terminal in Visual Studio Studio Code and run the following command to log in to azure</p><pre>az login</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cjMp3rCB-GFetkKB.png" /></figure><p><strong>Step 4: </strong>Now we need to create a resource group and for that we can use the following command</p><pre>az group create --name demoaci --location centralindia</pre><ul><li>Resource Group: demoaci</li><li>Location: centralindia</li></ul><p>depending on the name you like and the region, the above configurations can be changed</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/715/0*Jb9wr55r1VfuMRw0.png" /></figure><p><strong>Step 5: </strong>In this step we need to build a docker repository, using the following command</p><pre>az acr create --resource-group demoaci --name demoacirepo --sku Basic</pre><ul><li>Resource Group: demoaci</li><li>Repository name: demoacirepo</li><li>SKU: Basic</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7eSmPATS-1cWiFh0.png" /></figure><p><strong>Step 6: Log in</strong> to the repository created above with name <strong>demoacirepo</strong></p><pre>az acr login --name demoacirepo</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/0*9vTvkia9ph-JcxCS.png" /></figure><p><strong>Step 7: </strong>Enable admin sign-in to this repository</p><pre>az acr update -n demoacirepo --admin-enabled true</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cdCOseXyk6MeJiyc.png" /></figure><p><strong>Step 8: </strong>Adding tags to the image</p><p>Our compose file looks like this and now we need to add docker tags to the image</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/835/0*meMt7z8UEJppPsyT.png" /></figure><p>The image tags will look like this `reponame.azure.io/image-name’</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/831/0*OxZZ5PMQmRl9Gopw.png" /></figure><p><strong>Step 9: </strong>Time to build docker image</p><pre>docker-compose up --build -d</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/967/0*eI42lJQf5gxXGr7_.png" /></figure><p><strong>Step 10: </strong>check the image if it’s running</p><pre>docker ps</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/0*6CgnpMsUX-508GPr.png" /></figure><p><strong>Step 11: </strong>stop docker-compose</p><pre>docker-compose down</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*f-71d3qo-qx6JjG8.png" /></figure><p><strong>Step 12: </strong>push the images to Azure Container Repository</p><pre>docker-compose push</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/668/0*AJ1F_hMCLcydyA62.png" /></figure><p><strong>Step 13: </strong>login to docker azure</p><pre>docker login azure</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/0*DHuMEXQ6huWCn0s4.png" /></figure><p><strong>Step 14: </strong>create docker context for azure container instance followed by use, in my case context is already present so it may not look similar to yours</p><pre>docker context create aci myacicontext</pre><pre>docker context use myacicontext</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/605/0*9LmJCyoiZ9IMmZpp.png" /></figure><p><strong>Step 15: </strong>Now let’s start the docker in <em>Azure Container Instance</em></p><pre>docker compose up</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/465/0*IFmCd44CbskIJAg4.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/0*oU_VEr7aqNd4Ftbq.png" /></figure><p><strong>Step 15: </strong>Time to check the app link<em>Azure Container Instance</em></p><pre>docker ps</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/969/0*TuQxmyMoviip_5gq.png" /></figure><p><strong>Step 16: </strong>Hey, got our app running successfully :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kjNSZINscuhMeA9I.png" /></figure><p><strong>Step 17: </strong>If you find any error as shown below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/531/0*yJafr9V5hod3P-Dq.png" /></figure><p>Use the command given below to change context to default</p><pre>docker context use default</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/0*PLv-x5tLNF_EnLfm.png" /></figure><h3>Issues that persist</h3><p>While deploying the app to the azure container instance we face some issues like</p><ul><li>No Static IP address</li><li>Operational cost is high (it may not to feasible for small businesses)</li></ul><h3>Solutions</h3><ul><li><strong>Static IP address: </strong>Using v-net</li><li><strong>Operational cost is high (it may not to feasible for small businesses): </strong>Using Azure App Services</li></ul><h3>Reference:</h3><ul><li><a href="https://docs.microsoft.com/en-us/azure/container-instances/tutorial-docker-compose">https://docs.microsoft.com/en-us/azure/container-instances/tutorial-docker-compose</a></li><li><a href="https://docs.microsoft.com/en-us/azure/container-instances/container-instances-overview">https://docs.microsoft.com/en-us/azure/container-instances/container-instances-overview</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ece0e4d5492" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>