tag:blogger.com,1999:blog-2050302096842468872024-03-27T14:53:49.876+05:30Java tutorial and Java Interview Questions and AnswersJava tutorial, Java related real time issues and solution, Java/J2ee interview Questions and AnswersAnil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.comBlogger168125tag:blogger.com,1999:blog-205030209684246887.post-11127511988801019042024-03-16T10:58:00.002+05:302024-03-16T11:00:41.618+05:30Important Core Java Interview Questions for Freshers - 2024<p><span style="font-family: arial; font-size: medium;"> In this post I am sharing important core java interview questions with answers for freshers.</span></p><p><span style="font-family: arial; font-size: medium;"><b>1) Difference between c++ and java</b></span></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4hHQRcU18K4V5DMzgm06aCfPKs9nksjQwIpGk_txoFdW4Lni9rfWlvnEjkuLD65X3mWsg8ueWFZ4Wo8pItkuDTjK5dWjnZpm6Bldq40XrppH_le6xykTb2lgvr6xwiOQHaHP6tgrGYMPlUK_DMNSOZDfNg62zwnqidYdxJkbFBMoD1G2CqJlCjfvqUQQ/s596/DiffinJavaAndC++.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="424" data-original-width="596" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4hHQRcU18K4V5DMzgm06aCfPKs9nksjQwIpGk_txoFdW4Lni9rfWlvnEjkuLD65X3mWsg8ueWFZ4Wo8pItkuDTjK5dWjnZpm6Bldq40XrppH_le6xykTb2lgvr6xwiOQHaHP6tgrGYMPlUK_DMNSOZDfNg62zwnqidYdxJkbFBMoD1G2CqJlCjfvqUQQ/s16000/DiffinJavaAndC++.png" /></a></div><span style="font-family: arial; font-size: medium;"><br /></span><p></p><p><span style="font-family: arial; font-size: medium;"><b>2) What is mean by platform independent?</b></span></p><p><span style="font-family: arial; font-size: medium;"> We can say java is a platform independent it means that you can compile java code in one platform(e.g windows machine) and run it in another platform(e.g linux) without changing the code.</span></p><p><span style="font-family: arial; font-size: medium;"><b><br /></b></span></p><p><span style="font-family: arial; font-size: medium;"><b>3) Difference between jdk, jre and jvm.</b></span></p><p><span style="font-family: arial; font-size: medium;">Refer - <a href="https://www.scaler.com/topics/difference-between-jdk-jre-and-jvm/" target="_blank"><i>difference between jdk, jre and jvm</i></a></span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>4) What are access modifiers in java.</b></span></p><p><span style="font-family: arial; font-size: medium;"> The Access Modifiers in java are Default, Public, Protected and Private.</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;"><i>Default </i></span></li></ul><p></p><p><span style="font-family: arial; font-size: medium;"> This is a default access modifier in java. The scope of the default modifier is within the package.</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;"><i>Public</i></span></li></ul><span style="font-size: medium;"><span style="font-family: arial;"> Public modifier is used with an entity/model or any java class or variables </span><span style="font-family: arial;">that particular object or class is accessible throughout the application within or outside the package.</span></span><p></p><p></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;"><i>Protected </i></span></li></ul><span style="font-size: medium;"><span style="font-family: arial;"> The scope of this access modifier is within a package and this is also accessible outside the package </span><span style="font-family: arial;">through inherited class or child class.</span></span><p></p><p><span style="font-family: arial; font-size: medium;"></span></p><p></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;">Private </span></li></ul><span style="font-family: arial; font-size: medium;"> The scope of this access modifier is within a package, it can not access outside the package. </span><p></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>5) Explain main method in java - public static void main(String[] args).</b></span></p><p><span style="font-family: arial; font-size: medium;">In the main main public static void main(String[] args) -> </span></p><p><span style="font-family: arial; font-size: medium;"><i>public </i>-> public is a access modifier and it's accessible whole over the application within or outside the package.</span></p><p><span style="font-family: arial; font-size: medium;"><i>static </i>-> It's a keyword in java and it's used to call the variable or methods without creating the object.</span></p><p><span style="font-family: arial; font-size: medium;"><i>void</i> -> it's return type and void doesn't return anything.</span></p><p><span style="font-family: arial; font-size: medium;"><i>main</i> ->main is the method name here.</span></p><p><span style="font-family: arial; font-size: medium;"><i>String[] args</i> -> It stores the command line arguments and it's an array of type Strings. args is the argument name, it should be anything.</span></p><p><br /></p><p><span style="font-family: arial; font-size: medium;"><b>6) Why java is not pure object oriented language.</b></span></p><p><span style="font-family: arial; font-size: medium;"> Java has a primitive data type so those are not objects. So except primitive everything is objects but due to primitive data type we can not say java is pure object oriented language.</span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>7) Explain OOP's concept.</b></span></p><p><span style="font-family: arial; font-size: medium;">Refer - <a href="https://adnjavainterview.blogspot.com/2014/05/1tell-me-ooops-concept.html">OOP's Concept</a></span></p><p><span style="font-family: arial; font-size: medium;"><b><br /></b></span></p><p><span style="font-family: arial; font-size: medium;"><b>8) Explain data types in java.</b></span></p><p><span style="font-family: arial; font-size: medium;">There are 8 primitive data type as follows,</span></p><p><span style="font-family: arial; font-size: medium;"><b><i>a) boolean</i></b></span></p><p><span style="font-family: arial; font-size: medium;"><span style="white-space: pre;"> </span>Boolean data type represents only one bit of information either true or false. The default value is false.</span></p><p><span style="font-family: arial; font-size: medium;">Syntax:- </span><span style="font-family: arial; font-size: medium;">boolean booleanVarName;</span></p><p><span style="font-family: arial; font-size: medium;"><b><i>b) byte</i></b></span></p><p><span style="font-family: arial; font-size: medium;"> The byte data type is an 8-bit signed two’s complement integer - default value of byte is zero.</span></p><p><span style="font-family: arial; font-size: medium;">1 byte = 8 bits</span></p><p><span style="font-family: arial; font-size: medium;">Syntax :- </span><span style="font-family: arial; font-size: medium;">byte byteVarName</span><span style="font-family: arial; font-size: large;">;</span></p><p><span style="font-family: arial; font-size: medium;"><i><b>c) char</b> </i></span></p><p><span style="font-family: arial; font-size: medium;">The char data type is a single 16-bit Unicode character with the size of 2 bytes. The default value is<span style="white-space: pre;"> </span>\u0000.</span></p><p><span style="font-family: arial; font-size: medium;">Synatx :- </span><span style="font-size: medium;"><span style="font-family: arial;">char charVarName</span><b style="font-family: arial;">;</b></span></p><p><span style="font-family: arial; font-size: medium;"><b><i>d) short</i></b></span></p><p><span style="font-family: arial; font-size: medium;"> The short data type is a 16-bit signed two’s complement integer. The default value of short is zero.</span></p><p><span style="font-family: arial; font-size: medium;">Syntax :- </span><span style="font-family: arial; font-size: medium;">short shortVarName;</span></p><p><span style="font-family: arial; font-size: medium;"><b><i>e) int </i></b></span></p><p><span style="font-family: arial; font-size: medium;"> It is a 32-bit signed two’s complement integer. The default value of int is zero. 32 bits = 4 bytes</span></p><p><span style="font-family: arial; font-size: medium;">Syntax :- </span><span style="font-family: arial; font-size: medium;">int intVarName;</span></p><p><span style="font-family: arial; font-size: medium;"><b><i>f) long</i></b> </span></p><p><span style="font-family: arial; font-size: medium;"> The long data type is a 64-bit two’s complement integer. The deafult value of long is zero.</span></p><p><span style="font-family: arial; font-size: medium;">Syntax :- </span><span style="font-family: arial; font-size: medium;">long longVarName;</span></p><p><span style="font-family: arial; font-size: medium;"><b><i>g) float </i></b></span></p><p><span style="font-family: arial; font-size: medium;"> The float data type is a single-precision 32-bit floating-point. The default value of float is 0.0.</span></p><p><span style="font-family: arial; font-size: medium;">Syntax :- </span><span style="font-family: arial; font-size: medium;">float floatVarName;</span></p><p><span style="font-family: arial; font-size: medium;"><b><i>h) double</i></b> </span></p><p><span style="font-family: arial; font-size: medium;"> The double data type is a double-precision 64-bit floating-point. The default value of double is 0.0.</span></p><p><span style="font-family: arial; font-size: medium;">Syntax :- </span><span style="font-family: arial; font-size: medium;">double doubleVarName;</span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>9) What are wrapper classes in java.</b></span></p><p><span style="font-family: arial; font-size: medium;">Each primitive data type has corresponding wrapper class so there are 8 wrapper classes in java.</span></p><p><span style="font-family: arial; font-size: medium;"><i>Wrapper classes are Character, Byte, Short, Integer, Long, Float, Double and Boolean.</i></span></p><p><span style="font-family: arial; font-size: medium;">Using wrapper classes we can access object class methods like equals, compare, compareTo, hashcode and thread methods like wait, notify and notifyAll. Collection classes accept only object i.e wrapper not a primitive so this is also one of the advantage of wrapper class instead of primitive.</span></p><p><br /></p><p><span style="font-family: arial; font-size: medium;"><b>10) What is JIT compiler.</b></span></p><p><span style="font-family: arial; font-size: medium;"> JIT stands for <i><b>Just-in-Time </b></i>Compiler, it's part of JVM and it optimizes the process of converting byte code to machine specific language. It reduces the compilation time so that it improves the performance.</span></p><p><span style="font-family: arial; font-size: medium;"><b><br /></b></span></p><p><span style="font-family: arial; font-size: medium;"><b>11) What is mean by instance variable and local variable in java</b></span></p><p><b style="font-family: arial; font-size: large;"> </b><span style="font-size: medium;"><span style="font-family: arial;">The </span><span style="font-family: arial;">Variables defined inside methods, constructors or blocks are called <b><i>local variables</i></b>. </span><span style="font-family: arial;">The variable will be declared and initialized within the method and it will be destroyed when the method has completed.</span></span></p><p><span style="font-size: medium;"><span style="font-family: arial;"> <b><i>Instance variables</i></b> are variables within a class but outside the method or constructors or blocks. </span><span style="font-family: arial;">These variables are instantiated when the class is loaded and need to call the variables using class object.</span></span></p><p><span style="font-family: arial; font-size: medium;">Example :- </span></p>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">VariableEx</span><span style="color: #333333;">{</span>
<span style="color: #333399; font-weight: bold;">int</span> instanceVar<span style="color: #333333;">;</span> <span style="color: #888888;">//instance variable</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String args<span style="color: #333333;">[]){</span>
VariableEx obj <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> VariableEx<span style="color: #333333;">();</span>
<span style="color: #333399; font-weight: bold;">int</span> a <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">100</span><span style="color: #333333;">;</span> <span style="color: #888888;">//local variable</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"instance variable instanceVar value is : "</span></pre><pre style="line-height: 125%; margin: 0px;"><span style="color: #333333;"><span> </span><span> </span><span> </span><span> </span><span> </span>+</span>obj<span style="color: #333333;">.</span><span style="color: #0000cc;">instanceVar</span><span style="color: #333333;">);</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"local variable a value is : "</span><span style="color: #333333;">+</span>a<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<p><br /></p><p><span style="font-family: arial; font-size: medium;"><b>12) Difference between equals and == operator.</b></span></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5w-5VOBm29_uA5kXRlEODq4_3aaKq8nV5hor1TGPQbdCuncXmdokZwpTGKwmozuFfuTUqQULYiuV3eOLolgMQuH_6gm_MhL279AufjxV5JDZfjVy2u-yp7jyh287rKmtzYrTd0bPyCeYcxNc5qJhgOga2B8meOoV77QORsW3RUqXbrrIWtV1oi-hIIXo/s644/equalsAnd==Operator.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="338" data-original-width="644" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5w-5VOBm29_uA5kXRlEODq4_3aaKq8nV5hor1TGPQbdCuncXmdokZwpTGKwmozuFfuTUqQULYiuV3eOLolgMQuH_6gm_MhL279AufjxV5JDZfjVy2u-yp7jyh287rKmtzYrTd0bPyCeYcxNc5qJhgOga2B8meOoV77QORsW3RUqXbrrIWtV1oi-hIIXo/s16000/equalsAnd==Operator.png" /></a></div><br /><span style="font-family: arial; font-size: medium;"><br /></span><p></p><p><span style="font-family: arial; font-size: medium;"><b>13) What is constructor in java ? How many types constructors in java?</b></span></p><p><span style="font-family: arial; font-size: medium;"> Constructor in java is a block of code that used to create a object of the class. It's similar to method except name and return type, the constructor name should be same as class name and it doesn't have return type.</span></p><p><span style="font-family: arial; font-size: medium;">There are two types of constructors in java, </span></p><p><span style="font-family: arial; font-size: medium;">1) Default or No-args parameter constructor.</span></p><p><span style="font-family: arial; font-size: medium;">2) Parameterized constructor.</span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>14) What is Constructor chaining?</b></span></p><p><span style="font-family: arial; font-size: medium;"><span face="-apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"" style="background-color: white; border: 0px; font-weight: 700; margin: 0px; padding: 0px;">Constructor chaining in Java</span><span face="-apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"" style="background-color: white;"> is a technique of calling one constructor </span><span face="-apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"" style="background-color: white;">from within another constructor by using this and super keywords.</span></span></p><p></p><ul style="text-align: left;"><li><span face="-apple-system, system-ui, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol"><span style="font-family: arial; font-size: medium;">Using this() keyword to call the current class constructor within the same class.</span></span></li><li><span face="-apple-system, system-ui, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol"><span style="font-family: arial; font-size: medium;">Using super() keyword to call the superclass constructor from the base class.</span></span></li></ul><p></p><p><span face="-apple-system, system-ui, BlinkMacSystemFont, Segoe UI, Helvetica, Arial, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol"><span style="background-color: white; font-size: 18px;"></span></span></p><p><span style="font-family: arial; font-size: medium;"><b><br /></b></span></p><p><span style="font-family: arial; font-size: medium;"><b>15) What are interface and abstract class ? Difference between interface and abstract class.</b></span></p><p><span style="font-family: arial; font-size: medium;">Refer - <a href="https://adnjavainterview.blogspot.com/2014/05/2what-is-abstract-class-and.html" target="_blank">difference between abstract and interface in java</a></span></p><p><span style="font-family: arial; font-size: medium;"><b><br /></b></span></p><p><span style="font-family: arial; font-size: medium;"><b>16) What is marker interface in java </b></span></p><p><span style="font-family: arial; font-size: medium;">Refer - <a href="https://adnjavainterview.blogspot.com/2014/06/what-is-marker-interface-in-java-why.html" target="_blank">Marker interface in java</a></span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>17) Can we create object of abstract class?</b></span></p><p><span style="font-family: arial; font-size: medium;"> No, we can not create an object of abstract class but we can create object of child class that extends the abstract class.</span></p><p><span style="font-family: arial; font-size: medium;"><b><br /></b></span></p><p><span style="font-family: arial; font-size: medium;"><b>18) What is inheritance in java ? What are the types of inheritance in java.</b></span></p><p><span style="font-family: arial; font-size: medium;">Refer - <a href="https://adnjavainterview.blogspot.com/2014/05/1tell-me-ooops-concept.html" target="_blank">OOP's Concept</a></span></p><p><br /></p><p><span style="font-family: arial; font-size: medium;"><b>19) Is java supports multiple inheritance</b></span></p><p><span style="font-family: arial; font-size: medium;">No, Java doesn't support multiple inheritance.</span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>20) What is polymorphism</b></span></p><p><span style="font-family: arial; font-size: medium;">Refer - <a href="https://adnjavainterview.blogspot.com/2014/06/how-many-types-of-polymorhismexplain-in.html" target="_blank">Polymorphism in Java</a></span></p><p><br /></p><p><span style="font-family: arial; font-size: medium;"><b>21) Difference between method overloading and overriding.</b></span></p><p><span style="font-family: arial; font-size: medium;">Refer - <a href="https://adnjavainterview.blogspot.com/2014/06/how-many-types-of-polymorhismexplain-in.html" target="_blank">Polymorphism in Java</a></span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>22) What is static keyword in java?</b></span></p><p><span style="font-family: arial; font-size: medium;">Static is a keyword in java to apply to a variables, methods, blocks and nested classes.</span></p><p><span style="font-family: arial; font-size: medium;">If you declare variable as a final, that variable can not be reassigned. If we used final keyword for method level then the method can't be overriden. </span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>23) Can we overload static method?</b></span></p><p><span style="font-family: arial; font-size: medium;">Yes, we can overload static method in java.</span></p><p><br /></p><p><span style="font-family: arial; font-size: medium;"><b>24) Can we override static method?</b></span></p><p><span style="font-family: arial; font-size: medium;">No, we can not override static methods, it won't shows any error but it will execute only parent class static method.</span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"></span></p><p><span style="font-family: arial; font-size: medium;"><b>25) Can we overload the main() method?</b></span></p><div><span style="font-family: arial; font-size: medium;">Yes. Main method executes when class is being invoked but overloaded method need to call using class object.</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><p><span style="font-family: arial; font-size: medium;"><b>26) Difference between static and non static method.</b></span></p><p><span style="font-family: arial; font-size: medium;"><b> </b>Static methods belongs to a class and it's not belongs to an object of the class. We can call static method using class name directly no need to create an object of the class. Static method access only static variables and other static methods. Inside static methods we can't able to call non static methods.</span></p><p><span style="font-family: arial; font-size: medium;"> Non static methods belongs to an object of the class. Non static methods can access any static methods and static variables directly without creating an instance of the object.</span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>27) What is transient keyword in java.</b></span></p><p><span style="font-family: arial; font-size: medium;"><b> </b>The transient is a keyword in java and this is mainly used in serialization process. During serialization, transient variables won't persist or won't consider part of the serialization process.</span></p><p><span style="font-family: arial; font-size: medium;"><b><br /></b></span></p><p><span style="font-family: arial; font-size: medium;"><b>28) What is serialization in java.</b></span></p><p><span style="font-size: medium;"><span style="font-family: arial;">Serialization process in java is to convert an Object to byte stream that we can send over the network </span><span style="font-family: arial;">or save it as a file or store in a DB.</span></span></p><p><span style="font-size: medium;"><span style="font-family: arial;">If you want a class object to be serializable, then need to implement java.io.Serializable interface. </span><span style="font-family: arial;">Serializable is a marker interface means it doesn't have any fields or implemented methods.</span></span></p><p><span style="font-family: arial; font-size: medium;">Reference - <a href="https://adnjavainterview.blogspot.com/2015/04/what-is-serialization-in-java-where-we.html" target="_blank"><i>Serialization in java</i></a></span></p><p><span style="font-family: arial; font-size: medium;"><b><br /></b></span></p><p><span style="font-family: arial; font-size: medium;"><b>29) What is infinite loop in java.</b></span></p><p><span style="font-family: arial; font-size: medium;"> Infinite loop in java is a loop that executes indefinitely and the system will terminate or crash beacuse of the memory leak.</span></p><p><span style="font-family: arial; font-size: medium;">Code :- </span></p>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"> <span style="color: #008800; font-weight: bold;">for</span> <span style="color: #333333;">(;;)</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Inside a infinite loop"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
</pre></div>
<p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>30) Difference between finally block and final keyword.</b></span></p><p><span style="font-family: arial; font-size: medium;"><b><i>final </i></b>is a keyword to used for methods, variables and classes and those values can not change i mean the value should be constant.</span></p><p><span style="font-family: arial; font-size: medium;"><b><i>finally </i></b>is a block to use a clean up resouces like closing connections, closing file object and it executes always whether the exception throws or not.</span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>31) When finally block won't execute? </b></span></p><p><span style="font-family: arial; font-size: medium;">The finally block always executes except System.exit()</span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>32) What is the use of super keyword.</b></span></p><p><span style="font-family: arial; font-size: medium;"> The super keyword is used to call parent class constructor, to call parent/override parent class method and to call parent class variables.</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;">to call parent class constructor</span></li></ul>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #0066bb; font-weight: bold;">ClassChild</span><span style="color: #333333;">(</span>String parameter<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">super</span><span style="color: #333333;">(</span>parameter<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span></pre></div><div><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;">to call parent class method.</span></li></ul>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; text-align: left; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">method1</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">super</span><span style="color: #333333;">.</span><span style="color: #0000cc;">method2</span><span style="color: #333333;">();</span> <span style="color: #888888;">//this is a parent class method</span>
<span style="color: #333333;">}</span></pre></div></div><div><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;">to call parent class variable</span></li></ul>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">super</span><span style="color: #333333;">.</span><span style="color: #0000cc;">variable</span><span style="color: #333333;">;</span></pre></div><div><br /></div></div><p></p><p><span style="font-family: arial; font-size: medium;"><b>33) What is grabage collection in java.</b></span></p><p><span style="font-family: arial; font-size: medium;">Garbage Collection is the process of reclaiming unused memory by destroying the unused objects during run time.</span></p><p><span style="font-family: arial; font-size: medium;"></span></p><p><span style="font-family: arial; font-size: medium;"> This is automatic process to de-allocate the memory for unused objects so that it avoids memory leak.</span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>34) What is cloning and explain the types of cloning in java.</b></span></p><p><span style="font-family: arial; font-size: medium;">Cloning is java is nothing but creating a copy of the original object. To clone the object to use clone() method from the object class, will copy attributes and data from the original object.</span></p><p><span style="font-family: arial; font-size: medium;">There are two types of cloning in java,</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;"><i><b>Shallow Cloning</b></i> - Stores the copy of the original object and points the references to the objects. This cloning is faster compared to deep cloning.</span></li></ul><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;"><i><b>Deep Cloning</b></i> - Stores the copy of the original object and recursively copies the objects as well.</span></li></ul><p></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"></span></p><p style="-webkit-text-stroke-width: 0px; color: black; font-family: "Times New Roman"; font-size: medium; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"></p><ul style="text-align: left;"></ul><p></p><p style="-webkit-text-stroke-width: 0px; color: black; font-family: "Times New Roman"; font-size: medium; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; letter-spacing: normal; orphans: 2; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="font-family: arial; font-size: medium;"><b>35) How many ways to create an object in java.</b></span></p><p style="-webkit-text-stroke-width: 0px; color: black; font-family: "Times New Roman"; font-size: medium; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="font-family: arial; font-size: medium;">There are four ways to create an object in java, </span></p><p style="-webkit-text-stroke-width: 0px; color: black; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;">Using new Operator </span></li><li><span style="font-family: arial; font-size: medium;">Using Class.forName()</span></li><li><span style="font-family: arial; font-size: medium;">Using cloning</span></li><li><span style="font-family: arial; font-size: medium;">Object deserialization</span></li></ul><div><span style="font-family: arial; font-size: medium;">For more details, refer - <a href="https://adnjavainterview.blogspot.com/2014/06/how-many-ways-to-create-object-in-java.html" target="_blank">object creation in java</a></span></div><div><br /></div><p></p><p style="-webkit-text-stroke-width: 0px; color: black; font-family: "Times New Roman"; font-size: medium; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="font-family: arial; font-size: medium;"></span></p><p><span style="font-family: arial; font-size: medium;"><b>36) Difference between comparable and comparator.</b></span></p><p><span style="font-family: arial; font-size: medium;">Refer - <a href="https://adnjavainterview.blogspot.com/2014/06/difference-between-comparable.html" target="_blank">Difference between comparable and comparator in java</a></span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><b><span style="font-family: arial; font-size: medium;"></span></b></p><p style="-webkit-text-stroke-width: 0px; color: black; font-family: "Times New Roman"; font-size: medium; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; letter-spacing: normal; orphans: 2; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"></p><ul style="text-align: left;"></ul><p></p><p style="-webkit-text-stroke-width: 0px; color: black; font-family: "Times New Roman"; font-size: medium; font-style: normal; font-variant-caps: normal; font-variant-ligatures: normal; letter-spacing: normal; orphans: 2; text-align: left; text-decoration-color: initial; text-decoration-style: initial; text-decoration-thickness: initial; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span style="font-family: arial; font-size: medium;"><b>37) What is singleton class in Java and how can we make a class singleton?</b></span></p><p><span style="font-family: arial; font-size: medium;"> Singleton pattern/class restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine. The singleton class must provide a global access point to get the instance of the class. </span></p><p><span style="font-family: arial; font-size: medium;"><b style="background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.2px;"> </b><span style="background-color: white; color: #222222;">Singleton pattern is used for logging, drivers objects, caching and thread pool.</span><b style="background-color: white; color: #222222;"> </b><span style="background-color: white; color: #222222;">Singleton design pattern is also used in other design patterns like Abstract Factory, Builder, Prototype, Facade etc.</span><br style="background-color: white; color: #222222;" /><br style="background-color: white; color: #222222;" /><b style="background-color: white; color: #222222;"> </b><span style="background-color: white; color: #222222;">To implement the Singleton Design Pattern,you do the following the things,</span><br style="background-color: white; color: #222222;" /></span></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;"><span style="font-family: arial; font-size: medium;"><span style="background-color: white; color: #222222;">private constructor - no other class can instantiate a new object.</span></span></span></li><li><span style="font-family: arial; font-size: medium;">private reference - no external modification.</span></li><li><span style="font-family: arial; font-size: medium;">public static method is the only place that can get an object.</span></li></ul><p></p><p><span style="font-family: arial; font-size: medium;">Reference :- <a href="https://adnjavainterview.blogspot.com/2014/06/singleton-design-pattern-in-java-with.html" target="_blank">Singleton Class</a></span></p><p><br /></p><p><span style="font-family: arial; font-size: medium;"><b>38) How many ways to create String object.</b></span></p><p><span style="font-family: arial; font-size: medium;">There are two ways to create a String object </span></p><p></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;">Using String literal - e.g String a = "ABC";</span></li><li><span style="font-family: arial; font-size: medium;">Using new operator - e.g String a = new String("ABC");</span></li></ul><p><span style="font-family: arial; font-size: medium;"><b><br /></b></span></p><p><span style="font-family: arial; font-size: medium;"><b>39) Difference between String , StringBuffer and StringBuilder.</b></span></p><p><span style="font-family: arial; font-size: medium;"><b><i>String </i>: - </b>It's an immutable means once we define the value it can not be changed so it's thread safety. For concatenation of two strings we can use + operator or .concat method. String can use for the fixed length of content.</span></p><p><span style="font-family: arial; font-size: medium;"><b><i>StringBuffer </i>:- </b>StringBuffer is a mutable, we can modify the content without creating new Object. This is synchronization in nature so it's thread safety. We can use append method to concatenate the two objects. StringBuffer can use in multithreaded environment for frequent text changes.</span></p><p><span style="font-family: arial; font-size: medium;"><b><i>StringBuilder </i>:- </b></span><span style="font-family: arial; font-size: medium;">StringBuilder is a mutable, we can modify the content without creating new Object. This is not synchronized so it's not a thread safe. We can use append method to concatenate the two objects. StringBuilder can use in a single threaded environment for frequent text changes.</span></p><p><br /></p><p><span style="font-family: arial; font-size: medium;"><b>40) Explain exception hierarchy.</b></span></p><p><span style="font-family: arial; font-size: medium;">Throwable is the base class in exception and there are sub classes i.e Exception and Error. Again in Exception there are two types , Checked and Unchecked Exception. </span></p><p><span style="font-family: arial; font-size: medium;">Below diagram shows the exception hierarchy,</span></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxia5ihtAX3K1DYXDRPC3moXSwbHB57cBDQhC_WjXUV7-MZG6hxV36XMCtclzMx8QantGYmWnGDjTsEUfquLSgLBMUhNcOw7dfWXaZL6o0W_TJmVATXxIRiMO2e41SCtODSLJyY3XOHBhHcfn3S2bcx6C2B3KGBbk5SANITGv0qwXGwqTJd5GMPN7JFuw/s612/Exception.jpg" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="451" data-original-width="612" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxia5ihtAX3K1DYXDRPC3moXSwbHB57cBDQhC_WjXUV7-MZG6hxV36XMCtclzMx8QantGYmWnGDjTsEUfquLSgLBMUhNcOw7dfWXaZL6o0W_TJmVATXxIRiMO2e41SCtODSLJyY3XOHBhHcfn3S2bcx6C2B3KGBbk5SANITGv0qwXGwqTJd5GMPN7JFuw/s16000/Exception.jpg" /></a></div><br /><p></p><p><span style="font-family: arial; font-size: medium;"><b>41) Difference between Exception and Error.</b></span></p><p><span style="font-family: arial; font-size: medium;">The Throwable class is the base class for both the exception and error.</span></p><p><span style="font-family: arial; font-size: medium;"><b><i>Exception </i></b>:- An exception is an abnormal execution of the program, it stops the normal flow. Examples - NullPointerException, IOException and so on. There are two types of Exceptions,</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;">Checked Exception </span></li><li><span style="font-family: arial; font-size: medium;">Unchecked Exception. </span></li></ul><span style="font-family: arial; font-size: medium;">These exceptions can handle using try catch block in java.</span><p></p><p><span style="font-family: arial; font-size: medium;"><b><i>Error </i></b>:- </span><span style="background-color: white; color: #222222;"><span style="font-family: arial; font-size: medium;">Error indicates that a non-recoverable condition has occurred that should not be handled, examples - OutOfMemoryError and StackOverflowError</span></span><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; color: #222222; font-size: 13.2px;"> .</span></p><p><span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; color: #222222; font-size: 13.2px;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>42) Can we write try without catch ?</b></span></p><p><span style="font-family: arial; font-size: medium;">Yes, we can write try without catch, but try with finally block should exist, catch is not mandatory if finally block exists.</span></p>
<!--HTML generated using hilite.me--><div style="background: rgb(248, 248, 248); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: green; font-weight: bold;">try</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//code </span>
<span style="color: #666666;">....</span>
<span style="color: #666666;">}</span> <span style="color: green; font-weight: bold;">finally</span> <span style="color: #666666;">{</span>
<span style="color: #666666;">}</span>
</pre></div>
<p><span style="font-family: arial; font-size: medium;"><br /></span></p><p></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: large;"><b>Java 8</b></span></li></ul><p></p><p><span style="font-family: arial; font-size: medium;"><b>43) What are the new features in java 8.</b></span></p><p><span style="font-family: arial; font-size: medium;"><span style="background-color: white; color: #222222;">Some of the important Java 8 features are,</span><br style="background-color: white; color: #222222;" /></span></p><ul style="background-color: white; color: #222222; line-height: 1.4; margin: 0.5em 0px; padding: 0px 2.5em;"><li style="margin: 0px 0px 0.25em; padding: 0px;"><span style="font-family: arial; font-size: medium;">Lambda Expression</span></li><li style="margin: 0px 0px 0.25em; padding: 0px;"><span style="font-family: arial; font-size: medium;">static and default methods in interface</span></li><li style="margin: 0px 0px 0.25em; padding: 0px;"><span style="font-family: arial; font-size: medium;">Java Stream</span></li><li style="margin: 0px 0px 0.25em; padding: 0px;"><span style="font-family: arial; font-size: medium;">Functional Interfaces</span></li><li style="margin: 0px 0px 0.25em; padding: 0px;"><span style="font-family: arial; font-size: medium;">Java Time API</span></li><li style="margin: 0px 0px 0.25em; padding: 0px;"><span style="font-family: arial; font-size: medium;">Collection Improvement.</span></li><li style="margin: 0px 0px 0.25em; padding: 0px;"><span style="font-family: arial; font-size: medium;">Concurrency API improvement</span></li><li style="margin: 0px 0px 0.25em; padding: 0px;"><span style="font-family: arial; font-size: medium;">Java IO improvement</span></li></ul><div><span style="color: #222222; font-family: arial; font-size: medium;">Refer - <a href="https://adnjavainterview.blogspot.com/2018/09/java-8-features-with-examples.html" target="_blank">Java 8 features</a></span></div><div><br /></div><p><span style="font-family: arial; font-size: medium;"><b>44) What are the adantages of using Stream in java 8.</b></span></p><div style="background-color: white; color: #222222;"><span style="font-family: arial; font-size: medium;">Stream API is is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.</span></div><div style="background-color: white; color: #222222;"><span style="font-family: arial;"><span style="font-size: large;"> </span><i><span style="font-size: medium;">A stream does not store data means it is not a data structure. It also never modifies the underlying data source.</span></i></span></div><div style="background-color: white; color: #222222;"><span style="font-family: arial;"><span style="font-size: medium; font-weight: bold;"><br /></span></span></div><div style="background-color: white; color: #222222;"><span style="font-family: arial; font-size: medium;"><span style="font-weight: bold;">Advantages :- </span>In Stream, there many intermediate methods to perform operations like filter, map, sorted, filterMap and so on to reduce the code and also it's more readable and cleaner code. Using loop prior java 8 need to handle loop related exception explicitly in the code like ArrayindexOutOfBoundsException but using Stream, developer no need to bother about the loop issues, it handles internally.</span></div><p><span style="font-family: arial; font-size: medium;"><b><br /></b></span></p><p><span style="font-family: arial; font-size: medium;"><b>45) What is functional interface.</b></span></p><p><span style="font-family: arial; font-size: medium;">Functional interface is an interface that contains only one abstract method. @FunctionalInterface annotation is used to define the interface as Functional interface.</span></p>
<!--HTML generated using hilite.me--><div style="background: rgb(248, 248, 248); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #aa22ff;">@FunctionalInterface</span>
<span style="color: green; font-weight: bold;">public</span> <span style="color: green; font-weight: bold;">interface</span> <span style="color: blue; font-weight: bold;">Area</span> <span style="color: #666666;">{</span>
<span style="color: green; font-weight: bold;">public</span> <span style="color: #b00040;">int</span> <span style="color: blue;">calculateArea</span><span style="color: #666666;">(</span><span style="color: #b00040;">int</span> side<span style="color: #666666;">);</span>
<span style="color: #666666;">}</span>
</pre></div>
<p><br /></p><p></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: large;"><b>Thread</b></span></li></ul><p></p><p><span style="font-family: arial; font-size: medium;"><b>46) What is synchronization in java ?</b></span></p><p><span style="font-family: arial; font-size: medium;"><b> </b>Synchronization is a process of controlling the access of shared resources by the multiple threads in such a way </span><span style="font-size: medium;"><span style="font-family: arial;">that only one thread can access one resource at a time. In non synchronized multi threaded application, it is possible for </span><span style="font-family: arial;">one thread to modify a shared object while another thread is in the process of using or updating the object so </span><span style="font-family: arial;">in this case there is data inconsistency in the application.</span></span></p><p><span style="font-family: arial; font-size: medium;">Synchronization prevents such type of data corruption.</span></p><p><span style="font-family: arial; font-size: large;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>47) What is a thread ? How many ways to create thread in java and which one is preferred?</b></span></p><p><span style="font-family: arial; font-size: medium;">Thread in java is a light weight process and it's a path of execution within a process.</span></p><p><span style="font-family: arial; font-size: medium;"><span style="background-color: white; color: #222222;">There are two ways to create a thread in Java.</span><br style="background-color: white; color: #222222;" /></span></p><div style="background-color: white; color: #222222;"></div><p><span style="font-family: arial; font-size: medium;"></span></p><ul style="background-color: white; color: #222222; line-height: 1.4; margin: 0.5em 0px; padding: 0px 2.5em;"><li style="margin: 0px 0px 0.25em; padding: 0px;"><span style="font-family: arial; font-size: medium;">Extending the Thread class</span></li><li style="margin: 0px 0px 0.25em; padding: 0px;"><span style="font-family: arial; font-size: medium;">Using Runnable interface(this one prefer)</span></li></ul><div><span style="color: #222222; font-family: arial; font-size: medium;">The preferred way to create a thread is to implement Runnable interface </span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><p><span style="font-family: arial; font-size: medium;"><b>48) Explain thread life cycle.</b></span></p><p><span style="font-family: arial; font-size: medium;">A thread can be in one of the following states ,</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;">New born state(New)</span></li><li><span style="font-family: arial; font-size: medium;">Ready to run state (Runnable)</span></li><li><span style="font-family: arial; font-size: medium;">Running state(Running)</span></li><li><span style="font-family: arial; font-size: medium;">Blocked state</span></li><li><span style="font-family: arial; font-size: medium;">Dead state</span></li></ul><div><span style="font-family: arial; font-size: medium;">Refer - <a href="https://adnjavainterview.blogspot.com/2014/06/explain-thread-life-cycle-and.html" target="_blank">Thread life cycle</a></span></div><div><br /></div><p></p><p><span style="font-family: arial; font-size: medium;"></span></p><p><span style="font-family: arial; font-size: medium;"><b>49) What is daemon thread.</b></span></p><p><span style="font-family: arial; font-size: medium;">A daemon thread is a thread that executes the some tasks in the background like handling requests or various chronjobs that can exist in an application.</span></p><p><span style="font-family: arial; font-size: medium;"></span></p><p><span style="font-family: arial; font-size: medium;">The setDaemon() method of the Thread class is used to mark/set a particular thread as either a daemon thread or a user thread.</span></p>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;">thread<span style="color: #333333;">.</span><span style="color: #0000cc;">setDaemon</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">true</span><span style="color: #333333;">);</span><span style="color: #888888;">//if set false then it's normal thread.</span>
</pre></div>
<p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>50) Difference between wait and sleep method in a thread.</b></span></p><p><span style="font-family: arial; font-size: medium;">wait - It's belongs to a Object class. The wait method should be called within a synchronized method or block and this is not a static method. </span></p><p><span style="font-family: arial; font-size: medium;">sleep - This method belongs to a Thread class. No need to call this in synchronized block or method.</span></p><p><span style="font-family: arial; font-size: medium;"><b><br /></b></span></p><p><span style="font-family: arial; font-size: medium;"><b>51) Difference between start and run method. </b></span></p><p><span style="font-family: arial; font-size: medium;"><b>52) Explain join and yield method in thread</b></span></p><p><span style="font-family: arial; font-size: medium;"><b>53) What is multithreading in java?</b></span></p><p><span style="font-family: arial; font-size: medium;"><b>54) What is the volatile variable in java</b></span></p><p><span style="font-family: arial; font-size: medium;"><b>55) What is use of atomicinteger </b></span></p><p><br /></p><p></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;"><b>Collection</b></span></li></ul><p></p><p><span style="font-family: arial; font-size: medium;"><b>56) What is collection framework in java ? Explain each interface in collection framework</b></span></p><p><span style="font-family: arial; font-size: medium;"><b>57) Difference between arraylist and linked list</b></span></p><p><span style="font-family: arial; font-size: medium;"><b>58) How can you synchronize an ArrayList in Java?</b></span></p><p><span style="font-family: arial; font-size: medium;"><b>59) Difference between set and list </b></span></p><p><span style="font-family: arial; font-size: medium;"><b>60) Difference between HashMap and concurrentHashMap</b></span></p><p><span style="font-family: arial; font-size: medium;"><b>61) Difference between Treeset and HashSet</b></span></p><p><span style="font-family: arial; font-size: medium;"><b>62) Difference between HashMap and LinkedHashMap</b></span></p><p><span style="font-family: arial; font-size: medium;"><b>63) What is blocking queue?</b></span></p><p><span style="font-family: arial; font-size: medium;"><b>64) Difference between iterator listiterator</b></span></p><p><span style="font-family: arial; font-size: medium;"><b>65) Difference between failsafe and failfast</b></span></p><p><span style="font-family: arial; font-size: medium;"></span></p><p><br /></p>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.5945627-19.491571102478389 42.4383127 45.434768502478391 112.7508127tag:blogger.com,1999:blog-205030209684246887.post-39954391569283514952023-10-24T13:58:00.007+05:302023-10-24T22:18:29.101+05:30How to Return DTO's from Native Queries in Spring Data JPA<div style="text-align: left;"> <span style="font-family: arial; font-size: medium;"> In this post, we will discuss how to return DTO's of non entity basis native query result from JPA repository method. JPA provides many implemented methods for database operations like findAll, findById and so on. If we use in-built JPA repository methods then corresponding entity to map with the query result. <br /> </span></div><div style="text-align: left;"><span style="font-family: arial; font-size: medium;"> In some of the scenario's need to use native query to join with multiple tables to get the required details. In such case, we should not have the entity mapping so need to discuss how to map the native query result to the entity or DTO. We can achieve this using interface based DTO. The interface based DTO approach, JPA repository query maps with DTO instead of entity so need to convert again entity object to DTO object.</span></div><div style="text-align: left;"><span style="font-family: arial; font-size: medium;"><br /></span></div><div style="text-align: left;"><span style="font-family: arial; font-size: medium;">Let's discuss this with code as below,</span></div><div style="text-align: left;"><span style="font-family: arial; font-size: medium;"><br /></span></div><div style="text-align: left;"><span style="font-family: arial; font-size: medium;">JPA repository with native query,</span></div><div style="text-align: left;"><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(32, 32, 32); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: orange;">@Repository</span>
<span style="color: #6ab825; font-weight: bold;">public</span> <span style="color: #6ab825; font-weight: bold;">interface</span> <span style="color: #447fcf; text-decoration: underline;">PropertyTaxRepository</span> <span style="color: #6ab825; font-weight: bold;">extends</span> <span style="color: #d0d0d0;">JpaRepository<PropertyTax,</span> <span style="color: #d0d0d0;">Long></span> <span style="color: #d0d0d0;">{</span>
<span style="color: orange;">@Query</span><span style="color: #d0d0d0;">(value=</span><span style="color: #ed9d13;">" SELECT z.zone as zoneName, pt.status as propertyType, SUM(pt.tax) as amountCollected "</span>
<span style="color: #d0d0d0;">+</span> <span style="color: #ed9d13;">" FROM property_tax pt "</span>
<span style="color: #d0d0d0;">+</span> <span style="color: #ed9d13;">" INNER JOIN zone z ON z.id=pt.zonal_classification "</span>
<span style="color: #d0d0d0;">+</span> <span style="color: #ed9d13;">" group by z.zone, pt.status;"</span><span style="color: #d0d0d0;">,</span> <span style="color: #d0d0d0;">nativeQuery</span> <span style="color: #d0d0d0;">=</span> <span style="color: #6ab825; font-weight: bold;">true</span><span style="color: #d0d0d0;">)</span>
<span style="color: #6ab825; font-weight: bold;">public</span> <span style="color: #d0d0d0;">List<ReportDto></span> <span style="color: #447fcf;">getTaxCollectedReport</span><span style="color: #d0d0d0;">();</span>
<span style="color: #d0d0d0;">}</span>
</pre></div>
<br /><div><span style="font-family: arial; font-size: medium;">Created ReportDto interface with getter methods of the native query fields, there are three fields used in the select native query zoneName, propertyType and amountCollected. </span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;">Interface based DTO class as follows,</span></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(32, 32, 32); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #6ab825; font-weight: bold;">public</span> <span style="color: #6ab825; font-weight: bold;">interface</span> <span style="color: #447fcf; text-decoration: underline;">ReportDto</span> <span style="color: #d0d0d0;">{</span>
<span> </span><span style="color: #d0d0d0;">String</span> <span style="color: #447fcf;">getZoneName</span><span style="color: #d0d0d0;">();</span>
<span> </span><span style="color: #d0d0d0;">String</span> <span style="color: #447fcf;">getPropertyType</span><span style="color: #d0d0d0;">();</span>
<span> </span><span style="color: #d0d0d0;">BigDecimal</span> <span style="color: #447fcf;">getAmountCollected</span><span style="color: #d0d0d0;">();</span>
<span style="color: #d0d0d0;">}</span>
</pre></div>
<br /><div> <span style="font-family: arial; font-size: medium;">If you have any questions or clarifications, please add comment in the comment section or send an email to anilnivargi49@gmail.com.</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;">Thank you for visiting the blog.</span></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Sangli, Maharashtra, India16.8523973 74.5814773-11.457836536178846 39.4252273 45.162631136178845 109.7377273tag:blogger.com,1999:blog-205030209684246887.post-78133337741592507632023-10-01T23:31:00.006+05:302023-10-08T06:36:03.537+05:30Difference between @Component and @Bean annotation in Spring<p> <span style="font-family: arial; font-size: medium;">As we discussed about <a href="https://adnjavainterview.blogspot.com/2021/10/spring-stereotype-annotations.html" target="_blank">Stereotype annotations</a> in one of the blog post, the @Component annotation is a stereotype annotation in Spring. If you want to convert any java class as a Spring managed bean then to use component annotation. It will scan during the project start up and will create the bean if the component class exist in the component scan package.</span></p><p> <span style="font-family: arial; font-size: medium;">@Bean annotation is used to create a bean, this can use along with Configuration annotation. Bean annotation is a method level annotation and name of the method serves as a bean name. Component annotation is a class level annotation.</span></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhFA-t9HKcTGsut1boyb5SHWc1KN76U0OFLtrH4pnSeb7NcXRTIJfnxHpauDUmMCyCoA7or1kWiS_j1-SAbkSPKs6nuspdZpBmCEwHo2y59NN99q3LDQ4Stt0LPmjr8rJsvd6lIcZ2Ca0OBuXcNOarWsG0AdGB0WnA901bbcVgZfImPgKkG537iT4lUyY/s628/BeanComponentAnnotation.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Difference between @Bean and @Component" border="0" data-original-height="320" data-original-width="628" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhFA-t9HKcTGsut1boyb5SHWc1KN76U0OFLtrH4pnSeb7NcXRTIJfnxHpauDUmMCyCoA7or1kWiS_j1-SAbkSPKs6nuspdZpBmCEwHo2y59NN99q3LDQ4Stt0LPmjr8rJsvd6lIcZ2Ca0OBuXcNOarWsG0AdGB0WnA901bbcVgZfImPgKkG537iT4lUyY/s16000/BeanComponentAnnotation.png" title="Difference between @Bean and @Component" /></a></div><br /><span style="font-family: arial; font-size: medium;">Example:-</span><div><span style="font-family: arial; font-size: medium;"><br /></span><!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #555555; font-weight: bold;">@Component</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">ProductUtil</span><span style="color: #333333;">{</span>
<span style="color: #888888;">//methods</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /></div><div><span style="font-family: arial; font-size: medium;">@Bean annotation</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #555555; font-weight: bold;">@Configuration</span>
<span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">HMSAppConfiguration</span><span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Bean</span>
<span style="color: #008800; font-weight: bold;">public</span> Customer <span style="color: #0066bb; font-weight: bold;">getCustomer</span><span style="color: #333333;">(){</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #0066bb; font-weight: bold;">Customer</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: arial; font-size: medium;">Youtube Video:- </span></div><div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen='allowfullscreen' webkitallowfullscreen='webkitallowfullscreen' mozallowfullscreen='mozallowfullscreen' width='528' height='322' src='https://www.blogger.com/video.g?token=AD6v5dx9gc-oNNHMyl-7INRpzun6NQsJFkqqjY4iCrkOq4oqp8ZVDrdKRys05aFs_wGkNrG8E8GDbbOc-z-Tbs1tQA' class='b-hbp-video b-uploaded' frameborder='0'></iframe></div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;">Thank you for reading the blog.</span></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Sangli, Maharashtra, India16.8523973 74.5814773-11.457836536178846 39.4252273 45.162631136178845 109.7377273tag:blogger.com,1999:blog-205030209684246887.post-64937364426804655092023-09-30T21:10:00.002+05:302023-09-30T21:37:59.018+05:30Difference between PermGen(Permanent Generation) and Metaspace in Java<p> <span style="font-family: arial; font-size: medium;"> In this post, we will discuss the difference between PermGen i.e Permanent Generation and Metaspace in java 8(feature).</span></p><p><span style="font-family: arial; font-size: medium;"> Before java 8, static methods and variables are stored in PermGen space but in java 8 they have introduced new memory space called Metaspace. The default allocation size of PermGen is 64MB, if it exceeds it should throw an OutOfMemoryError. As if we need to fix this issue need to increase the PermGen space using JVM parameter –XX:MaxPermSize.</span></p><p><span style="font-family: arial; font-size: medium;"> Metaspace memory space introduced in java 8 to resolve the above issue. Internally it will automatically increases the size when allocation memory size increases. No need to increase the memory size manually.</span></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9vllgIzBr22TRPuyIuhfQHCSZjgd1XWBXWqwkemUevjYoTMuSgWzy1LHZZ3HC9qh5SRbLTq4iWkXKhGE7EOYRSROz3eS_yhRk6hblKO1JBzezeM-WHQolMS3tm6pnIC97GNCxbt1ZjYJEciYcw6PbQh4xLp9AE-xgQsuyLlWTyxNcsdeE_k7h-bfUNG4/s623/PermGenMetaspace.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="PermGen and Metaspace difference" border="0" data-original-height="316" data-original-width="623" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi9vllgIzBr22TRPuyIuhfQHCSZjgd1XWBXWqwkemUevjYoTMuSgWzy1LHZZ3HC9qh5SRbLTq4iWkXKhGE7EOYRSROz3eS_yhRk6hblKO1JBzezeM-WHQolMS3tm6pnIC97GNCxbt1ZjYJEciYcw6PbQh4xLp9AE-xgQsuyLlWTyxNcsdeE_k7h-bfUNG4/s16000/PermGenMetaspace.png" title="PermGen and Metaspace difference" /></a></div><br /><p><span style="font-family: arial; font-size: medium;">Thank you for visiting the blog.</span></p>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Sangli, Maharashtra, India16.8523973 74.5814773-11.457836536178846 39.4252273 45.162631136178845 109.7377273tag:blogger.com,1999:blog-205030209684246887.post-41379889521112328152023-09-27T11:35:00.003+05:302023-09-27T11:44:49.946+05:30Difference between equals method and == operator in java<p><span style="font-family: arial; font-size: medium;"> In this post, we can discuss one of the important java interview questions i.e difference between equals() and == operator and sample examples.</span></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWeRd7EiM3bFfliOvrVoI_fK6QM-v1YyX0e_qNfXHDPmAMkrItXtXrWZneEJZbqedxYOaADbd1-Q_UuM2iLJKCSTdp6Gwbd2MCyfaRHw3OpAQjb_Bg1pcsZ2XTy6MKPIWhMXxJRCFAc-OZ2yLViPj4ILtErOiJzUuy3-g4St2sTZCpSUqjj-REfNh3xLI/s644/blogPicture.png" style="margin-left: 1em; margin-right: 1em;"><img alt="equals and == operator in java" border="0" data-original-height="338" data-original-width="644" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWeRd7EiM3bFfliOvrVoI_fK6QM-v1YyX0e_qNfXHDPmAMkrItXtXrWZneEJZbqedxYOaADbd1-Q_UuM2iLJKCSTdp6Gwbd2MCyfaRHw3OpAQjb_Bg1pcsZ2XTy6MKPIWhMXxJRCFAc-OZ2yLViPj4ILtErOiJzUuy3-g4St2sTZCpSUqjj-REfNh3xLI/s16000/blogPicture.png" title=".equals() and == operator in java" /></a></div><p><span style="font-family: arial; font-size: medium;">The equals() method compares the content of two objects but == opeator checks the objects references of two objects for equality.</span></p><p><span style="font-family: arial; font-size: medium;">Let me see few examples to check both equals and == operator,</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;"><b>Created two string objects using new operator and applied equals and == operator to check the equality.</b></span></li></ul>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">DemoClass</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
String s1 <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> String<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">);</span>
String s2 <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> String<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>s1<span style="color: #333333;">.</span><span style="color: #0000cc;">equals</span><span style="color: #333333;">(</span>s2<span style="color: #333333;">))</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"s1 equals s2 are equal"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">else</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"s1 equals s2 are not equal"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>s1 <span style="color: #333333;">==</span> s2<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"s1 == s2 are equal"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">else</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"s1 == s2 are not equal"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: arial; font-size: medium;"><i>Output:- </i></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;">s1 equals s2 are equal
s1 <span style="color: #333333;">==</span> s2 are not equal
</pre></div>
<div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;">Used new operator to create String objects and those objects will store in heap memory of JVM and will create two different objects in the heap even the contents are same. So == operator result will return false.</span></div><div><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;"><b>Created two string objects using literal and applied equals and == operator to check the equality.</b></span></li></ul></div>
<b><!--HTML generated using hilite.me--></b><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">DemoClass</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
String s1 <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">;</span>
String s2 <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>s1<span style="color: #333333;">.</span><span style="color: #0000cc;">equals</span><span style="color: #333333;">(</span>s2<span style="color: #333333;">))</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"s1 equals s2 are equal"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">else</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"s1 equals s2 are not equal"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>s1 <span style="color: #333333;">==</span> s2<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"s1 == s2 are equal"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">else</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"s1 == s2 are not equal"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: arial; font-size: medium;"><i>Output:-</i></span></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 16px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;">s1 equals s2 are equal
s1 <span style="color: #333333;">==</span> s2 are equal
</pre></div>
<div><br /></div><div><b>Youtube Video:-</b></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="315" src="https://www.youtube.com/embed/A-hvIkU6twc" width="636" youtube-src-id="A-hvIkU6twc"></iframe></div><div><br /></div><div><span style="font-family: arial; font-size: medium;">Thank you for visiting blog.</span></div><br />Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Sangli, Maharashtra, India16.8523973 74.5814773-11.457836536178846 39.4252273 45.162631136178845 109.7377273tag:blogger.com,1999:blog-205030209684246887.post-83372252889755461522023-09-07T21:41:00.005+05:302023-09-24T15:32:04.619+05:30Sort List In Ascending & Descending Order Using Java 8 stream API<p><span style="font-family: arial; font-size: medium;"> In one of the previous post, listed all the java 8 stream coding questions and answers(<a href="https://adnjavainterview.blogspot.com/2023/07/java-8-stream-coding-interview-questions-and-answers.html" target="_blank">blog post</a>). Today's post we will discuss one of the important coding questions i.e to sort the list in ascending and descending order using java 8 stream api.</span></p><p><span style="font-family: arial; font-size: medium;"><b>1) Sort the string of list object in ascending order using java 8 stream</b></span></p>
<b><!--HTML generated using hilite.me--></b><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">SortExample</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> listOfStrings <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"E"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"B"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"C"</span><span style="color: #333333;">);</span>
listOfStrings<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">sorted</span><span style="color: #333333;">().</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s <span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: arial; font-size: medium;"><i>Output:- </i>A</span></div><div><span style="font-family: arial; font-size: medium;"> B</span></div><div><span style="font-family: arial; font-size: medium;"> C</span></div><div><span style="font-family: arial; font-size: medium;"> E</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><b><span style="font-size: medium;"><span style="font-family: arial;">2) </span><span style="font-family: arial;">Sort the string of list object in descending order using java 8 stream</span></span></b></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Comparator</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">SortExample</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> listOfStrings <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"E"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"B"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"C"</span><span style="color: #333333;">);</span>
listOfStrings<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">sorted</span><span style="color: #333333;">(</span>Comparator<span style="color: #333333;">.</span><span style="color: #0000cc;">reverseOrder</span><span style="color: #333333;">())</span></pre><pre style="line-height: 125%; margin: 0px;"><span style="color: #333333;"> .</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s <span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: arial; font-size: medium;"><i>Output:- </i> E</span></div><div><span style="font-family: arial; font-size: medium;"> C</span></div><div><span style="font-family: arial; font-size: medium;"> B</span></div><div><span style="font-family: arial; font-size: medium;"> A</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>3) Sort the custom(Employee class) java object of list in ascending or descending order using java 8 stream</b></span></div><div><span style="font-family: arial; font-size: medium;"><b><br /></b></span></div><div><span style="font-family: arial; font-size: medium;"><i>Employee.java,</i></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Employee</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">private</span> Long id<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> String firstName<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> String lastName<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> String address<span style="color: #333333;">;</span>
<span style="color: #888888;">//constructor, setter and getter</span>
<span style="color: #888888;">// and toString metghod</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: arial; font-size: medium;"><i>SortExample.java,</i></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">main</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.ArrayList</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">SortExample</span> <span style="color: #333333;">{</span>
<span> </span><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
Employee e1 <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Employee<span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">1L</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"Anil"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"Nivargi"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"XYZ"</span><span style="color: #333333;">);</span>
Employee e2 <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Employee<span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">2L</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"Mahesh"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"Nivargi"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"XYZ"</span><span style="color: #333333;">);</span>
List<span style="color: #333333;"><</span>Employee<span style="color: #333333;">></span> employeeList <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><>();</span>
employeeList<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>e1<span style="color: #333333;">);</span>
employeeList<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>e2<span style="color: #333333;">);</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Sort a list in ascending order using firstName -"</span><span style="color: #333333;">);</span>
<span style="color: #888888;">//sort a list in ascending order using firstName</span>
employeeList<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">sorted</span><span style="color: #333333;">(</span><span style="color: #333333;">(</span>s1<span style="color: #333333;">,</span>s2<span style="color: #333333;">)</span> <span style="color: #333333;">-></span> </pre><pre style="line-height: 125%; margin: 0px;"> s1<span style="color: #333333;">.</span><span style="color: #0000cc;">getFirstName</span><span style="color: #333333;">().</span><span style="color: #0000cc;">compareTo</span><span style="color: #333333;">(</span>s2<span style="color: #333333;">.</span><span style="color: #0000cc;">getFirstName</span><span style="color: #333333;">()))</span><span style="color: #333333;">.</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span></pre><pre style="line-height: 125%; margin: 0px;"> s <span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span></pre><pre style="line-height: 125%; margin: 0px;">
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Sort a list in descending order using firstName -"</span><span style="color: #333333;">);</span>
<span style="color: #888888;">//sort a list in descending order using firstName</span>
employeeList<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">sorted</span><span style="color: #333333;">((</span>s1<span style="color: #333333;">,</span>s2<span style="color: #333333;">)</span> <span style="color: #333333;">-></span> </pre><pre style="line-height: 125%; margin: 0px;"> s2<span style="color: #333333;">.</span><span style="color: #0000cc;">getFirstName</span><span style="color: #333333;">().</span><span style="color: #0000cc;">compareTo</span><span style="color: #333333;">(</span>s1<span style="color: #333333;">.</span><span style="color: #0000cc;">getFirstName</span><span style="color: #333333;">()))</span><span style="color: #333333;">.</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span></pre><pre style="line-height: 125%; margin: 0px;"> s <span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span> </span><span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><b><span style="font-family: arial; font-size: medium;"><i>Output:- </i></span></b></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;">Sort a list in ascending order using firstName <span style="color: #333333;">-</span>
Employee <span style="color: #333333;">[</span>id<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,</span> firstName<span style="color: #333333;">=</span>Anil<span style="color: #333333;">,</span> lastName<span style="color: #333333;">=</span>Nivargi<span style="color: #333333;">,</span> address<span style="color: #333333;">=</span>XYZ<span style="color: #333333;">]</span>
Employee <span style="color: #333333;">[</span>id<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span> firstName<span style="color: #333333;">=</span>Mahesh<span style="color: #333333;">,</span> lastName<span style="color: #333333;">=</span>Nivargi<span style="color: #333333;">,</span> address<span style="color: #333333;">=</span>XYZ<span style="color: #333333;">]</span>
Sort a list in descending order using firstName <span style="color: #333333;">-</span>
Employee <span style="color: #333333;">[</span>id<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span> firstName<span style="color: #333333;">=</span>Mahesh<span style="color: #333333;">,</span> lastName<span style="color: #333333;">=</span>Nivargi<span style="color: #333333;">,</span> address<span style="color: #333333;">=</span>XYZ<span style="color: #333333;">]</span>
Employee <span style="color: #333333;">[</span>id<span style="color: #333333;">=</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,</span> firstName<span style="color: #333333;">=</span>Anil<span style="color: #333333;">,</span> lastName<span style="color: #333333;">=</span>Nivargi<span style="color: #333333;">,</span> address<span style="color: #333333;">=</span>XYZ<span style="color: #333333;">]</span>
</pre></div>
<div><br /></div><div class="separator" style="clear: both; text-align: center;"><iframe allowfullscreen="" class="BLOG_video_class" height="308" src="https://www.youtube.com/embed/DP7vyZwCFD8" width="526" youtube-src-id="DP7vyZwCFD8"></iframe></div><br /><div><br /></div><div><span style="font-family: arial; font-size: medium;">Thank you reading the blog post.</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>Reference Posts:-</b></span></div><div><span style="font-family: arial; font-size: medium;"><b>1) <a href="https://adnjavainterview.blogspot.com/2023/07/java-8-stream-coding-interview-questions-and-answers.html" target="_blank">Java 8 stream coding questions</a></b></span></div><div><span style="font-family: arial; font-size: medium;"><b>2) <a href="https://adnjavainterview.blogspot.com/2023/07/difference-between-map-and-flatmap-method-in-java8.html" target="_blank">Difference between map and flatMap method in java 8</a></b></span></div><div><span style="font-family: arial; font-size: medium;"><b>3) <a href="https://adnjavainterview.blogspot.com/2023/08/find-duplicate-elements-from-given-list-using-java8.html" target="_blank">Find duplicate element from the list using stream</a></b></span></div><div><span style="font-family: arial; font-size: medium;"><b>4) <a href="https://adnjavainterview.blogspot.com/2023/08/java-8-optional-class-with-examples.html" target="_blank">Java 8 Optional class usages</a></b></span></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Sangli, Maharashtra, India16.8523973 74.5814773-11.457836536178846 39.4252273 45.162631136178845 109.7377273tag:blogger.com,1999:blog-205030209684246887.post-87762267320785429162023-09-01T23:24:00.004+05:302023-09-21T18:42:44.981+05:30Difference Between @Resource, @Autowired and @Inject in Spring Injection<p> <span style="font-family: arial; font-size: medium;">In this post, we will learn the difference between Resource, Autowired and Inject annotation. All these annotations are used to inject the bean dependencies. Resource and Inject annotations are defined in Java and Autowired annotation defined in Spring framework.</span></p><p><span style="font-family: arial; font-size: medium;"><b>@Resource</b> – It's defined in the javax.annotation package and it's part of java.</span></p><p><span style="font-family: arial; font-size: medium;"><b>@Inject</b> – It's defined in the javax.inject package and it's part of Java</span></p><p><span style="font-family: arial; font-size: medium;"><b>@Autowired</b> – It's defined in the package org.springframework.bean.factory and part of Spring framework.</span></p><p><span style="font-family: arial; font-size: medium;"> Internally @Inject and @Autowired annotations use the <a href="https://docs.spring.io/spring-framework/docs/3.0.x/javadoc-api/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.html" target="_blank">AutowiredAnnotationBeanPostProcessor</a> to inject the dependencies. But @Resource annotation internally use <a href="https://docs.spring.io/spring-framework/docs/3.1.0.M2/javadoc-api/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.html" target="_blank">CommonAnnotationBeanPostProcessor</a> to inject the dependencies. Execution path/step is same for both Inject and Autowired annotations, Resource annotation execution paths also same but order of execution paths are not same.</span></p><p></p><ul style="text-align: left;"><li><span style="font-family: arial; font-size: large;"><b>@Inject and @Autowired</b></span></li></ul><div><span style="font-family: arial; font-size: medium;"> 1) Matches by Type</span></div><div><span style="font-family: arial; font-size: medium;"> 2) Restricts by Qualifiers</span></div><div><span style="font-family: arial; font-size: medium;"> 3) Matches by Name</span></div><div><ul style="text-align: left;"><li><span style="font-family: arial; font-size: medium;"><b>@Resource</b></span></li></ul><div><span style="font-family: arial; font-size: medium;"> 1) Matches by Name</span></div></div><div><span style="font-family: arial; font-size: medium;"> 2) Matched by Type</span></div><div><span style="font-family: arial; font-size: medium;"> 3) Restricts by Qualifiers</span></div><div><br /></div><div><span style="font-family: arial; font-size: medium;">Let us discuss with example of each Annotation execution path,</span></div><div><br /></div><div><span style="font-family: arial; font-size: medium;"><i>Notification.java,</i></span></div>
<span style="font-family: arial; font-size: medium;"><i><!--HTML generated using hilite.me--></i></span><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">interface</span> <span style="color: #bb0066; font-weight: bold;">Notification</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// some methods</span>
<span style="color: #333333;">}</span></span>
</span></pre></div>
<br /><div><span style="font-family: arial; font-size: medium;"><i>Email.java,</i></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.stereotype.Component</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Component</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Email</span> <span style="color: #008800; font-weight: bold;">implements</span> Notification <span style="color: #333333;">{</span>
<span style="color: #888888;">// some methods</span>
<span style="color: #333333;">}</span></span><span style="font-size: 14px;">
</span></pre></div>
<br /><div><span style="font-size: medium;"><i><b>SMS.java,</b></i></span></div>
<span style="font-size: medium;"><i><b><!--HTML generated using hilite.me--></b></i></span><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.stereotype.Component</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Component</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">SMS</span> <span style="color: #008800; font-weight: bold;">implements</span> Notification <span style="color: #333333;">{</span>
<span style="color: #888888;">// some methods</span>
<span style="color: #333333;">}</span></span>
</pre></div>
<br /><div><span style="font-family: arial; font-size: medium;">Inject the above beans into the caller java class,</span></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #555555; font-weight: bold;">@Resource</span>
<span style="color: #555555; font-weight: bold;">@Qualifier</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"invalid"</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">private</span> Notification email<span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Autowired</span>
<span style="color: #555555; font-weight: bold;">@Qualifier</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"invalid"</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">private</span> Notification email<span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Inject</span>
<span style="color: #555555; font-weight: bold;">@Qualifier</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"invalid"</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">private</span> Notification email<span style="color: #333333;">;</span>
</pre></div>
<br /><div><span style="font-family: arial; font-size: medium;">when we execute above code, the @Resource annotation will work fine without any error but @Autowired and @Inject will fail and through an exception like below,</span></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;">org<span style="color: #333333;">.</span><span style="color: #0000cc;">springframework</span><span style="color: #333333;">.</span><span style="color: #0000cc;">beans</span><span style="color: #333333;">.</span><span style="color: #0000cc;">factory</span><span style="color: #333333;">.</span><span style="color: #0000cc;">NoSuchBeanDefinitionException</span><span style="color: #333333;">:</span>
No matching bean of type <span style="color: #333333;">[</span>com<span style="color: #333333;">.</span><span style="color: #0000cc;">prj</span><span style="color: #333333;">.</span><span style="color: #0000cc;">basics</span><span style="color: #333333;">.</span><span style="color: #0000cc;">notification</span><span style="color: #333333;">.</span><span style="color: #0000cc;">Notification</span><span style="color: #333333;">]</span>
</pre></div>
<br /><div><span style="font-family: arial; font-size: medium;"> The Resource annotation checks by field name and it found the bean definition using name i.e email and will create the bean object internally but other two annotations checks by qualifier i.e by bean name mentioned in the component and there is no bean name with invalid and throw an exception.</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;">Below code should works for all three annotations without fail.</span></div><div><span style="font-family: arial; font-size: medium;"> </span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #555555; font-weight: bold;">@Resource</span>
<span style="color: #555555; font-weight: bold;">@Qualifier</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"email"</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">private</span> Notification email<span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Autowired</span>
<span style="color: #555555; font-weight: bold;">@Qualifier</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"email"</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">private</span> Notification email<span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Inject</span>
<span style="color: #555555; font-weight: bold;">@Qualifier</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"email"</span><span style="color: #333333;">)</span>
<span style="color: #008800; font-weight: bold;">private</span> Notification email<span style="color: #333333;">;</span>
</pre></div>
<br /><div>OR </div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #555555; font-weight: bold;">@Resource</span>
<span style="color: #008800; font-weight: bold;">private</span> Email email<span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Autowired</span>
<span style="color: #008800; font-weight: bold;">private</span> Email email<span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Inject</span>
<span style="color: #008800; font-weight: bold;">private</span> Email email<span style="color: #333333;">;</span>
</pre></div>
<br /><div><span style="font-family: arial; font-size: medium;">Thank you for visiting the blog.</span></div><div><br /></div><div><span style="font-family: arial; font-size: medium;"><b>Reference posts:-</b></span></div><div><span style="font-family: arial; font-size: medium;"><b>1) <a href="https://adnjavainterview.blogspot.com/2023/08/how-to-resolve-circular-dependency-in-spring.html" target="_blank">How to resolve circular dependency in spring Framework</a></b></span></div><div><span style="font-family: arial; font-size: medium;">2) <a href="https://adnjavainterview.blogspot.com/2018/12/the-springbootapplication-annotation.html" target="_blank">T<b><span style="color: #2b00fe;">he @SpringBootApplication Annotation usage and example</span></b></a></span></div><div><span style="font-family: arial; font-size: medium;"><b>3) <a href="https://adnjavainterview.blogspot.com/2017/11/spring-annotations.html" target="_blank">Spring annotations</a></b></span></div><div><span style="font-family: arial; font-size: medium;"><b>4) <a href="http://adnjavainterview.blogspot.com/2018/01/spring-qualifier-annotation-with-example.html" target="_blank">Spring Qualifier annotation</a></b></span></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.5945627-15.338635136178846 42.4383127 41.281832536178847 112.7508127tag:blogger.com,1999:blog-205030209684246887.post-60072274238523283332023-08-29T08:59:00.003+05:302023-09-11T08:13:16.377+05:30Akkodis company Java technical Lead(Exp - 9 to 12 yrs) interview questions<p> <span face="Arial, Tahoma, Helvetica, FreeSans, sans-serif" style="background-color: white; color: #222222; font-size: 13.2px;"> </span><span style="color: #222222;"><span style="font-family: arial;">Below are the list of java technical lead interview questions asked in <a href="https://www.akkodis.com/" target="_blank"><b>Akkodis</b> </a>during the firstround</span></span><span style="color: #222222; font-family: arial; font-size: 13.2px;">.</span></p><div><div><span style="color: #222222; font-family: arial; font-size: medium;">1) What are the features in java8 ?</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">2) Why introduced default method in java 8 ?</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">3) What is the use of static method in java 8 ?</span></div><div><span style="color: #222222; font-family: arial;"><span style="font-size: medium;"><br /></span></span></div><div><span style="color: #222222; font-family: arial;"><span style="font-size: medium;">4) What is functional interface in java8 ?</span></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">5) What is the use of flatMap in streams and explain with example</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">6) List<Integer> list = Arrays.asList(10, 23, 8, null);</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">Write a program to sort the list in ascending and descending order and remove the nulls --using streams</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial;"><span style="font-size: medium;">7) what is generics in java ? Is this supports polymorphism ? (Answer -- yes)</span></span></div><div><span style="color: #222222; font-family: arial;"><span style="font-size: medium;">if yes then which polymorphism supports compile or runtime(Answer - runtime) </span></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">8) How many ways to create thread ? which one prefer and why ?</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">9) What implemenation or changes done in HashMap in java 8 ?</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">10) Which scenario's to use ArrayList and LinkedList</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">11) Asked some Linux commands.</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">12) Explain contract between equals and hashcode method.</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">13) What are the intermediate functions in streams.</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">14) How to make ArrayList as final - as final is immutable.</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">15) How to create singleton object and how to restrict for clone and deserialization</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">16) How to handle global the exception in spring boot</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">17) What is checked and unchecked exception</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">18) How to set the priority thread in threads.</span></div><div><span style="color: #222222; font-family: arial; font-size: medium;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">19) What is completable future in java 8.</span></div></div><div><span style="color: #222222; font-family: arial;"><br /></span></div><div><span style="color: #222222; font-family: arial;"><br /></span></div><div><span style="color: #222222; font-family: arial; font-size: medium;">Thank you for visiting the blog.</span></div><div><div style="background-color: white; text-align: left;"><span style="font-size: medium;"><b><span style="font-family: arial;"><span style="color: #222222;">1) </span><a href="https://adnjavainterview.blogspot.com/2023/07/publicis-sapient-java-technical-lead-interview-questions.html" style="text-decoration-line: none;" target="_blank"><span style="color: #2b00fe;">Publicis Sapient Java Technical Lead Interview questions<br /></span></a></span></b><b><span style="font-family: arial;"><span style="color: #222222;">2) </span><a href="https://adnjavainterview.blogspot.com/2023/08/mobtexting-company-java-technical-lead-interview-questions.html" style="text-decoration-line: none;" target="_blank"><span style="color: #2b00fe;">mobtexting company Java Technical Lead interview questions<br /></span></a></span></b><b><span style="font-family: arial;">3) <a href="https://adnjavainterview.blogspot.com/2023/08/ltimindtree-java-technica-lead-interview-questions.html" target="_blank">LTIMindtree java technical interview questions</a></span></b></span></div></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.5945627-15.338635136178846 42.4383127 41.281832536178847 112.7508127tag:blogger.com,1999:blog-205030209684246887.post-80191951480665819502023-08-24T23:51:00.003+05:302024-02-17T17:16:48.314+05:30Find duplicate elements from the given a list using Java 8 Stream<p> <span style="font-family: arial;">In this post, we need to have a look one of the important coding interview questions asked for java 8 streams. Given list contains either String or Integers. Refer all coding questions <a href="https://adnjavainterview.blogspot.com/p/coding.html" target="_blank"><b>here</b></a></span></p><p><span style="font-family: arial; font-size: medium;"><b>1) Print duplicate elements using frequency and toSet methods . </b></span></p><p> <span style="font-family: arial;"> In the below code used Collections.frequency to filter the data with occurance value greater than 1 and toSet method used to remove the duplicated in output.</span></p>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Collections</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Set</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.stream.Collectors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8StreamEx</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;"><span> </span>public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span> </span>List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> integerList <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">5</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">6</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">6</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">6</span><span style="color: #333333;">);</span>
<span> </span>Set<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> integerList<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">filter</span><span style="color: #333333;">(</span>
<span> </span>s <span style="color: #333333;">-></span> Collections<span style="color: #333333;">.</span><span style="color: #0000cc;">frequency</span><span style="color: #333333;">(</span>integerList<span style="color: #333333;">,</span> s<span style="color: #333333;">)</span> <span style="color: #333333;">></span> <span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">)</span></pre><pre style="line-height: 125%; margin: 0px;"><span style="color: #333333;"> .</span><span style="color: #0000cc;">collect</span><span style="color: #333333;">(</span>Collectors<span style="color: #333333;">.</span><span style="color: #0000cc;">toSet</span><span style="color: #333333;">());</span>
<span> </span>list<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s <span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Duplicate element - "</span><span style="color: #333333;">+</span>s<span style="color: #333333;">));</span>
<span> </span><span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: arial;"><i>Output:-</i> </span><span style="font-family: arial;">Duplicate element - 3</span></div><div><span style="font-family: arial;"> Duplicate element - 6</span></div><div><span style="font-family: arial;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>2) Using frequency and toMap method(map used to store element with occurance)</b></span></div><div><span style="font-family: arial; font-size: medium;"><b><br /></b></span></div><div><span style="font-family: arial;">Used frequency and toMap method to store duplicates into the Map with occurance as a value.</span></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Collections</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Map</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.function.Function</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.stream.Collectors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8StreamEx</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> integerList <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">5</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">6</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">6</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">6</span><span style="color: #333333;">);</span>
<span style="color: #888888;">// store duplicate into the map</span>
Map<span style="color: #333333;"><</span>Integer<span style="color: #333333;">,</span> Long<span style="color: #333333;">></span> map <span style="color: #333333;">=</span> </pre><pre style="line-height: 125%; margin: 0px;"> integerList<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">filter</span><span style="color: #333333;">(</span>
s <span style="color: #333333;">-></span> Collections<span style="color: #333333;">.</span><span style="color: #0000cc;">frequency</span><span style="color: #333333;">(</span>integerList<span style="color: #333333;">,</span> s<span style="color: #333333;">)</span> <span style="color: #333333;">></span> <span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">).</span><span style="color: #0000cc;">collect</span><span style="color: #333333;">(</span>
Collectors<span style="color: #333333;">.</span><span style="color: #0000cc;">toMap</span><span style="color: #333333;">(</span>Function<span style="color: #333333;">.</span><span style="color: #0000cc;">identity</span><span style="color: #333333;">(),</span> v <span style="color: #333333;">-></span> <span style="color: #0000dd; font-weight: bold;">1L</span><span style="color: #333333;">,</span> <span style="color: #997700; font-weight: bold;">Long:</span><span style="color: #333333;">:</span>sum<span style="color: #333333;">));</span>
map<span style="color: #333333;">.</span><span style="color: #0000cc;">entrySet</span><span style="color: #333333;">().</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s <span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: arial;"><i>Output: -</i> 3=2</span></div><div><span style="font-family: arial;"> 6=3</span></div><div><span style="font-family: arial;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>3) Using groupingBy and counting methods - group the similar elements and to find the occarance used counting.</b></span></div><div><span style="font-family: arial; font-size: medium;"><b><br /></b></span></div><div><span style="font-family: arial;">To write the code to find the duplicate using groupingBy,</span></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Collections</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Map</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.function.Function</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.stream.Collectors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8StreamEx</span> <span style="color: #333333;">{</span>
<span> <span> </span></span><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span> </span><span> </span><span> </span>List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> listOfString <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"B"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"C"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"D"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"B"</span><span style="color: #333333;">);</span>
<span> </span>Map<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span> Long<span style="color: #333333;">></span> mapOfStrings <span style="color: #333333;">=</span>
listOfString<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">filter</span><span style="color: #333333;">(</span>
<span> </span>s <span style="color: #333333;">-></span> Collections<span style="color: #333333;">.</span><span style="color: #0000cc;">frequency</span><span style="color: #333333;">(</span>listOfString<span style="color: #333333;">,</span> s<span style="color: #333333;">)</span> <span style="color: #333333;">></span> <span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">)</span>
<span> </span><span style="color: #333333;">.</span><span style="color: #0000cc;">collect</span><span style="color: #333333;">(</span>Collectors<span style="color: #333333;">.</span><span style="color: #0000cc;">groupingBy</span><span style="color: #333333;">(</span>Function<span style="color: #333333;">.</span><span style="color: #0000cc;">identity</span><span style="color: #333333;">(),</span>
<span> </span><span> </span>Collectors<span style="color: #333333;">.</span><span style="color: #0000cc;">counting</span><span style="color: #333333;">()));</span>
<span> </span>mapOfStrings<span style="color: #333333;">.</span><span style="color: #0000cc;">entrySet</span><span style="color: #333333;">().</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s <span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;"><span> </span>}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: arial;"><i>Output:-</i>A=3</span></div><div><span style="font-family: arial;"> B=2</span></div><div><br /></div><div><span style="font-family: arial;"><br /></span></div><div><span style="font-family: arial;">Thank you for visiting blog.</span></div><div><span style="font-family: arial;"><br /></span></div><div><span style="font-family: arial;">Refer the post for coding questions - <a href="https://adnjavainterview.blogspot.com/p/coding.html" target="_blank">coding questions</a></span></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.5945627-15.338635136178846 42.4383127 41.281832536178847 112.7508127tag:blogger.com,1999:blog-205030209684246887.post-18668444253219674222023-08-21T08:59:00.007+05:302023-08-23T07:23:19.943+05:30LTIMindtree Java technical Lead interview questions<p><span style="font-family: arial; font-size: medium;"> <span> Below are the list of java technical lead interview questions asked in <a href="https://www.ltimindtree.com/" target="_blank"><b>LTIMindtree</b></a> during the first round</span></span></p><p><span style="font-family: arial; font-size: medium;">1) Explain Saga design pattern used in Microservice(<a href="https://medium.com/design-microservices-architecture-with-patterns/saga-pattern-for-microservices-distributed-transactions-7e95d0613345" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">2) What is orchestrator in Saga pattern?(<a href="https://medium.com/design-microservices-architecture-with-patterns/saga-pattern-for-microservices-distributed-transactions-7e95d0613345" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">3) Which authentication mechanism used in project and how to implemented(AOuth2 with bearer token)?</span></p><p><span style="font-family: arial; font-size: medium;">3) What is the use of Eureka server?(<a href="https://www.section.io/engineering-education/introduction-to-microservices-with-spring-and-eureka/" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">4) What is Functional interface in java 8? do we have default method in Functional interface(<a href="https://adnjavainterview.blogspot.com/2018/09/java-8-features-with-examples.html" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">5) Why introduced deafult method in interface in java 8?(<a href="https://adnjavainterview.blogspot.com/2018/09/java-8-features-with-examples.html" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">6) What is lambda expression ?(<a href="https://www.geeksforgeeks.org/lambda-expressions-java-8/" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">7) What is difference between collection and stream?(<a href="https://www.javaguides.net/2021/12/collections-vs-streams-in-java.html#:~:text=Collections%20are%20used%20to%20store,%2C%20or%20I%2FO%20resources." target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">8) What is the use of status code in Rest API ?(<a href="https://restfulapi.net/http-status-codes/" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">9) What is database/memory lock expeption?</span></p><p><span style="font-family: arial; font-size: medium;">10) What are HTTP idempotent methods?(<a href="https://restcookbook.com/HTTP%20Methods/idempotency/" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">11) What are the key components in spring boot?</span></p><p><span style="font-family: arial; font-size: medium;">12) What is the use of @WebMvcTest . Difference between @WebMvcTest and @SpringBootTest(<a href="https://www.javaguides.net/2022/03/springboottest-vs-webmvctest.html" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">13) Write a program to find second largest word in the sentence.</span></p><p><span style="font-family: arial; font-size: medium;">14) Write a query to find the max salary from the employee table whose name is Reta</span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><i>Thank you for visiting blog.</i></span></p><p><span style="font-family: arial; font-size: medium;"><b><a href="https://adnjavainterview.blogspot.com/2023/08/java-8-optional-class-with-examples.html" target="_blank">Previous</a> <a href="https://adnjavainterview.blogspot.com/" target="_blank">Home</a></b></span></p><p><span style="font-family: arial; font-size: medium;"><b>Related Posts:-</b></span></p><p><span style="font-family: arial; font-size: medium;"><b>1) <a href="https://adnjavainterview.blogspot.com/2023/07/publicis-sapient-java-technical-lead-interview-questions.html" target="_blank">Publicis Sapient Java Technical Lead Interview questions</a></b></span></p><p><span style="font-family: arial; font-size: medium;"><b>2) <a href="https://adnjavainterview.blogspot.com/2023/08/mobtexting-company-java-technical-lead-interview-questions.html" target="_blank">mobtexting company Java Technical Lead interview questions</a></b></span></p>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.5945627-15.338635136178846 42.4383127 41.281832536178847 112.7508127tag:blogger.com,1999:blog-205030209684246887.post-45994331833007496312023-08-16T11:36:00.006+05:302023-08-23T07:24:52.024+05:30Java 8 Optional Class with examples<p><span style="font-size: medium;"><span style="font-family: arial;"> In java8 introduced new class called Optional, using optional we can handle NullPointerException without using any null check condition.( Refer - <a href="https://adnjavainterview.blogspot.com/2018/09/java-8-features-with-examples.html" target="_blank">Java 8 features</a>) </span><span style="font-family: arial;">It provided many methods and using those it will be helpful to write cleanable code.</span></span></p><p><span style="font-family: arial; font-size: medium;"> Using optional if variable value is null then using orElse method we can have a null functionality code inside it.</span></p><p><span style="font-family: arial; font-size: medium;"> Below are the methods available in Optional class,</span></p><p></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkL19RZ34VuX04PmOi1Dde3pc63J5005K9WzrbDA-v5-UqD-kCSC-ajkF4TWEnd5i5LjisJRp5K0Oox8VWBq_yHZc_kAyfOJULeCLZ4rbXMKfipu3TVY89zBpaszCeYvTGRcZN4Kf7_qWU7nsTgAFr73Lv4SOTi5t_9ef5GKwmniYtahzuYBSMeonGme0/s423/OptionalClassMethods.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Java 8 optional class methods" border="0" data-original-height="423" data-original-width="382" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkL19RZ34VuX04PmOi1Dde3pc63J5005K9WzrbDA-v5-UqD-kCSC-ajkF4TWEnd5i5LjisJRp5K0Oox8VWBq_yHZc_kAyfOJULeCLZ4rbXMKfipu3TVY89zBpaszCeYvTGRcZN4Kf7_qWU7nsTgAFr73Lv4SOTi5t_9ef5GKwmniYtahzuYBSMeonGme0/w578-h640/OptionalClassMethods.png" title="Java 8 optional class methods" width="578" /></a></div><br /><p></p><p><span style="font-size: medium;"><span style="font-family: arial;"><b>The Optional.ofNullable()</b> --- > method returns a Non-empty Optional if a value present in the given object. </span><span style="font-family: arial;">Otherwise returns an empty Optional. </span></span></p><p><span style="font-family: arial; font-size: medium;"><b>Optional.empty()</b> -- > method is useful to create an empty Optional object.</span></p><p><span style="font-family: arial; font-size: medium;"><b>Optional.of(value)</b> --> creates an Optional object using value. </span></p><p><span style="font-family: arial; font-size: medium;"><b>Optional.ifPresent</b> -- > If optional variable value is present, invoke the specified consumer with the value, otherwise, do nothing.</span></p><p><span style="font-family: arial; font-size: medium;"><b>orElse </b>--> If optional variable value is null then invokes orElse method.</span></p><p><span style="font-size: medium;"><span style="font-family: arial;"> </span><span style="font-family: arial;"><b>orElseGet</b> --> If optional variable value is null then invokes orElse method and return the result of that invocation.</span></span></p><p><span style="font-family: arial; font-size: medium;"> <b>orElseThrow</b> --> If optional variable value is null then invokes orElseThrow method and throws the exception provided in the method.</span></p><p><span style="font-family: arial; font-size: medium;"><b> get </b>-- > method returns the value from the optional.</span></p><p><span style="font-family: arial; font-size: medium;"></span></p><p><span style="font-family: arial; font-size: medium;"> <b>isPresent</b> - if optional variable value is present/exist then return true else it's false.</span></p><p><span style="font-family: arial; font-size: medium;"><b><br /></b></span></p><p><span style="font-family: arial; font-size: medium;"><b>Optional Class Code Example - </b></span></p>
<!--HTML generated using hilite.me--><div style="background: rgb(248, 248, 248); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: green; font-weight: bold;">import</span> <span style="color: blue; font-weight: bold;">java.util.Optional</span><span style="color: #666666;">;</span>
<span style="color: green; font-weight: bold;">public</span> <span style="color: green; font-weight: bold;">class</span> <span style="color: blue; font-weight: bold;">OptionalClassUsages</span> <span style="color: #666666;">{</span>
<span style="color: green; font-weight: bold;">public</span> <span style="color: green; font-weight: bold;">static</span> <span style="color: #b00040;">void</span> <span style="color: blue;">main</span><span style="color: #666666;">(</span>String<span style="color: #666666;">[]</span> args<span style="color: #666666;">)</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//ofNullable, orElse, orElseGet and orElseThrow method example</span>
String str <span style="color: #666666;">=</span> <span style="color: #ba2121;">"Optional with ofNullable method usage"</span><span style="color: #666666;">;</span>
System<span style="color: #666666;">.</span><span style="color: #7d9029;">out</span><span style="color: #666666;">.</span><span style="color: #7d9029;">println</span><span style="color: #666666;">(</span>Optional<span style="color: #666666;">.</span><span style="color: #7d9029;">ofNullable</span><span style="color: #666666;">(</span>str<span style="color: #666666;">).</span><span style="color: #7d9029;">orElse</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"null logic"</span><span style="color: #666666;">));</span>
str <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">null</span><span style="color: #666666;">;</span>
System<span style="color: #666666;">.</span><span style="color: #7d9029;">out</span><span style="color: #666666;">.</span><span style="color: #7d9029;">println</span><span style="color: #666666;">(</span>Optional<span style="color: #666666;">.</span><span style="color: #7d9029;">ofNullable</span><span style="color: #666666;">(</span>str<span style="color: #666666;">).</span><span style="color: #7d9029;">orElse</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"null logic"</span><span style="color: #666666;">));</span>
System<span style="color: #666666;">.</span><span style="color: #7d9029;">out</span><span style="color: #666666;">.</span><span style="color: #7d9029;">println</span><span style="color: #666666;">(</span>Optional<span style="color: #666666;">.</span><span style="color: #7d9029;">ofNullable</span><span style="color: #666666;">(</span>str<span style="color: #666666;">).</span><span style="color: #7d9029;">orElseGet</span><span style="color: #666666;">(()</span> <span style="color: #666666;">-></span> <span style="color: #ba2121;">"orElseGet method"</span><span style="color: #666666;">));</span>
<span style="color: green; font-weight: bold;">try</span> <span style="color: #666666;">{</span>
Optional<span style="color: #666666;">.</span><span style="color: #7d9029;">ofNullable</span><span style="color: #666666;">(</span>str<span style="color: #666666;">).</span><span style="color: #7d9029;">orElseThrow</span><span style="color: #666666;">(()</span> <span style="color: #666666;">-></span> <span style="color: #666666;">{</span>
<span style="color: green; font-weight: bold;">return</span> <span style="color: green; font-weight: bold;">new</span> <span style="color: blue;">Exception</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"orElseThrow Exception"</span><span style="color: #666666;">);</span>
<span style="color: #666666;">});</span>
<span style="color: #666666;">}</span> <span style="color: green; font-weight: bold;">catch</span> <span style="color: #666666;">(</span>Exception e<span style="color: #666666;">)</span> <span style="color: #666666;">{</span>
e<span style="color: #666666;">.</span><span style="color: #7d9029;">printStackTrace</span><span style="color: #666666;">();</span>
<span style="color: #666666;">}</span>
<span style="color: #408080; font-style: italic;">//optional of, ifPresent, filter usages</span>
Optional<span style="color: #666666;"><</span>String<span style="color: #666666;">></span> optional <span style="color: #666666;">=</span> Optional<span style="color: #666666;">.</span><span style="color: #7d9029;">of</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"ab"</span><span style="color: #666666;">);</span>
optional<span style="color: #666666;">.</span><span style="color: #7d9029;">ifPresent</span><span style="color: #666666;">(</span>s<span style="color: #666666;">-></span> System<span style="color: #666666;">.</span><span style="color: #7d9029;">out</span><span style="color: #666666;">.</span><span style="color: #7d9029;">println</span><span style="color: #666666;">(</span>s<span style="color: #666666;">));</span>
optional<span style="color: #666666;">.</span><span style="color: #7d9029;">filter</span><span style="color: #666666;">(</span>s<span style="color: #666666;">-></span>s<span style="color: #666666;">.</span><span style="color: #7d9029;">equals</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"ab"</span><span style="color: #666666;">))</span>
<span style="color: #666666;">.</span><span style="color: #7d9029;">ifPresent</span><span style="color: #666666;">(</span>s<span style="color: #666666;">-></span> System<span style="color: #666666;">.</span><span style="color: #7d9029;">out</span><span style="color: #666666;">.</span><span style="color: #7d9029;">println</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"ifPresent method usage"</span><span style="color: #666666;">));</span>
<span style="color: #408080; font-style: italic;">//isPresent and get method usage example</span>
System<span style="color: #666666;">.</span><span style="color: #7d9029;">out</span><span style="color: #666666;">.</span><span style="color: #7d9029;">println</span><span style="color: #666666;">(</span>optional<span style="color: #666666;">.</span><span style="color: #7d9029;">isPresent</span><span style="color: #666666;">());</span>
System<span style="color: #666666;">.</span><span style="color: #7d9029;">out</span><span style="color: #666666;">.</span><span style="color: #7d9029;">println</span><span style="color: #666666;">(</span>optional<span style="color: #666666;">.</span><span style="color: #7d9029;">get</span><span style="color: #666666;">());</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span>
</pre></div>
<br /><div><span style="font-family: arial;"><i><b>Output:- </b></i></span></div><div><span style="font-family: arial;"><div><br /></div></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(248, 248, 248); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;">Optional with ofNullable method usage
<span style="color: green; font-weight: bold;">null</span> logic
orElseGet method
java<span style="color: #666666;">.</span><span style="color: #7d9029;">lang</span><span style="color: #666666;">.</span><span style="color: #7d9029;">Exception</span><span style="color: #666666;">:</span> orElseThrow Exception
at com<span style="color: #666666;">.</span><span style="color: #7d9029;">main</span><span style="color: #666666;">.</span><span style="color: #7d9029;">OptionalClassUsages</span><span style="color: #666666;">.</span><span style="color: #7d9029;">lambda</span>$1<span style="color: #666666;">(</span>OptionalClassUsages<span style="color: #666666;">.</span><span style="color: #7d9029;">java</span><span style="color: #666666;">:19)</span>
at java<span style="color: #666666;">.</span><span style="color: #7d9029;">util</span><span style="color: #666666;">.</span><span style="color: #7d9029;">Optional</span><span style="color: #666666;">.</span><span style="color: #7d9029;">orElseThrow</span><span style="color: #666666;">(</span>Optional<span style="color: #666666;">.</span><span style="color: #7d9029;">java</span><span style="color: #666666;">:290)</span>
at com<span style="color: #666666;">.</span><span style="color: #7d9029;">main</span><span style="color: #666666;">.</span><span style="color: #7d9029;">OptionalClassUsages</span><span style="color: #666666;">.</span><span style="color: #7d9029;">main</span><span style="color: #666666;">(</span>OptionalClassUsages<span style="color: #666666;">.</span><span style="color: #7d9029;">java</span><span style="color: #666666;">:18)</span>
ab
ifPresent method usage
<span style="color: green; font-weight: bold;">true</span>
ab
</pre></div>
<br /><div><br /></div><div>Thank you for visiting the blog.</div><div><span style="font-family: arial;"><br /></span></div><div><a href="https://adnjavainterview.blogspot.com/2023/08/java-program-to-find-second-largest-number-in-array-or-list.html" target="_blank"><b><span style="font-family: arial;">Previous</span></b></a> <a href="https://adnjavainterview.blogspot.com/" target="_blank"><span style="font-family: arial;"><b>Home</b></span></a> <a href="https://adnjavainterview.blogspot.com/2023/08/ltimindtree-java-technica-lead-interview-questions.html" target="_blank"><b><span style="font-family: arial;">Next</span></b></a><br /></div><div><span style="font-family: arial;"><b><br /></b></span></div><div><span style="font-family: arial;"><b>Reference posts:</b></span></div><div><span style="font-family: arial;"><b>1) <a href="https://adnjavainterview.blogspot.com/2018/09/java-8-features-with-examples.html" target="_blank">Java 8 features</a></b></span></div><div><span style="font-family: arial;"><b>2)</b> <a href="http://adnjavainterview.blogspot.com/2014/06/string-related-interview-questions.html" target="_blank"><b><span style="color: #2b00fe;">String related interview questions and answers</span></b></a></span></div><div><b>3) <a href="http://adnjavainterview.blogspot.com/2019/04/java-8-foreach-method-with-examples.html" target="_blank"><span style="color: #2b00fe; font-family: arial;">Java 8 forEach method examples</span></a></b></div><div><b>4)</b> <a href="http://adnjavainterview.blogspot.com/2019/05/java-8-consumer-interface-with-examples.html" target="_blank"><span style="color: #2b00fe; font-family: arial;"><b>Java 8 - Consumer Interface with examples</b></span></a></div><div><br /></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.5945627-15.338635136178846 42.4383127 41.281832536178847 112.7508127tag:blogger.com,1999:blog-205030209684246887.post-4224473325504700262023-08-15T08:21:00.001+05:302023-09-11T08:01:40.483+05:30Java program to find the Second largest number in an Array or List<p> <span style="font-family: arial; font-size: medium;">Given an unsorted array of an integers or a List, to write a Java program to find the second largest number or element from the Array or a List</span></p><p><span style="font-family: arial; font-size: medium;"><b>1) Given input is an array of integers</b></span></p>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">SecondLargest</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
SecondLargest secondLargest <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> SecondLargest<span style="color: #333333;">();</span>
<span style="color: #333399; font-weight: bold;">int</span><span style="color: #333333;">[]</span> array <span style="color: #333333;">=</span> <span style="color: #333333;">{</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">8</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">4</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">5</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">7</span> <span style="color: #333333;">};</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Second largest number - "</span> <span style="color: #333333;">+</span> secondLargest<span style="color: #333333;">.</span><span style="color: #0000cc;">findSecondLargestNumber</span><span style="color: #333333;">(</span>array<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">findSecondLargestNumber</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span><span style="color: #333333;">[]</span> array<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">sort</span><span style="color: #333333;">(</span>array<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> array<span style="color: #333333;">[</span>array<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span> <span style="color: #333333;">-</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">];</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: arial; font-size: medium;"><i><b>Output :- </b></i>Second largest number - 7</span></div><div><span style="font-family: arial;"><br /></span></div><div><span style="font-family: arial;"><b><br /></b></span></div><div><span style="font-family: arial; font-size: medium;"><b>2) Given input is a list</b></span></div><div><span style="font-family: arial;"><b><br /></b></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Collections</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">SecondLargest</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
SecondLargest secondLargest <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> SecondLargest<span style="color: #333333;">();</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Integer<span style="color: #333333;">[]</span> <span style="color: #333333;">{</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">8</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">4</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">5</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">7</span> <span style="color: #333333;">});</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Second largest number - "</span>
<span style="color: #333333;">+</span> secondLargest<span style="color: #333333;">.</span><span style="color: #0000cc;">findSecondLargestNumber</span><span style="color: #333333;">(</span>list<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">findSecondLargestNumber</span><span style="color: #333333;">(</span>List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> listOfIntegers<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
Collections<span style="color: #333333;">.</span><span style="color: #0000cc;">sort</span><span style="color: #333333;">(</span>listOfIntegers<span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> listOfIntegers<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">(</span>listOfIntegers<span style="color: #333333;">.</span><span style="color: #0000cc;">size</span><span style="color: #333333;">()</span> <span style="color: #333333;">-</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-size: medium;"><span style="font-family: arial;"><i><b>Output:-</b></i> </span><span style="font-family: arial;">Second largest number - 7</span></span></div><div><span style="font-family: arial;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>3) Given input as a List, find second largest number using Stream(Java8)</b></span></div><div><span style="font-family: arial;"><b><br /></b></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">SecondLargest</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
SecondLargest secondLargest <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> SecondLargest<span style="color: #333333;">();</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Integer<span style="color: #333333;">[]</span> <span style="color: #333333;">{</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">8</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">4</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">5</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">7</span> <span style="color: #333333;">});</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Second largest number - "</span>
<span style="color: #333333;">+</span> secondLargest<span style="color: #333333;">.</span><span style="color: #0000cc;">findSecondLargestNumber</span><span style="color: #333333;">(</span>list<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">findSecondLargestNumber</span><span style="color: #333333;">(</span>List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> listOfIntegers<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> listOfIntegers<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">sorted</span><span style="color: #333333;">()</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">skip</span><span style="color: #333333;">(</span>listOfIntegers<span style="color: #333333;">.</span><span style="color: #0000cc;">size</span><span style="color: #333333;">()</span> <span style="color: #333333;">-</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">)</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">findFirst</span><span style="color: #333333;">()</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<span style="font-family: arial;"><b><i><br /></i></b></span><div><span style="font-size: medium;"><span style="font-family: arial;"><b><i>Output:- </i></b></span><span style="font-family: arial;">Second largest number - 7.</span></span></div><div><span style="font-family: arial;"><br /></span></div><div><br /></div><div><span style="font-family: arial; font-size: medium;">Thank you for visiting the blog.</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>Related page:-</b></span></div><div><a href="https://adnjavainterview.blogspot.com/p/coding.html" target="_blank"><span style="font-family: arial; font-size: medium;"><b>Java coding questions</b></span></a><br /></div><div><span style="font-family: arial;"><br /></span></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.5945627-15.338635136178846 42.4383127 41.281832536178847 112.7508127tag:blogger.com,1999:blog-205030209684246887.post-20076940643894161972023-08-14T02:13:00.003+05:302023-08-14T07:34:45.504+05:30How to resolve circular dependency in spring Framework<p><span style="font-family: arial;"> In this tutorial, we can dsicuss about circular dependency in Spring, first need to understand what's the circular dependency and how to fix it.</span></p><p><span style="font-family: arial; font-size: medium;"><b>What is Circular Dependency in Spring ?</b></span></p><p><span style="font-family: arial;">In Spring, circular dependency is a dependency chain where two or more beans depends on each other.</span></p><p><span style="font-family: arial;">For example - Class A requires an instance of class B and class B also requires an instance of class A through any type of injection. </span><span style="font-family: arial;">In this case, the Spring IoC container detects circular reference at runtime, and throws a BeanCurrentlyInCreationException.</span></p><p><span style="font-family: arial;">In Spring Boot, circular dependency can occur when using the constructor injection as see below code,</span></p>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.stereotype.Component</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Component</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">BeanA</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">final</span> BeanB beanB<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #0066bb; font-weight: bold;">BeanA</span><span style="color: #333333;">(</span>BeanB beanB<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">beanB</span><span style="color: #333333;">=</span> beanB<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<div><br /></div>
<span style="font-family: arial;">Second bean,<br />
<!--HTML generated using hilite.me--></span><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.stereotype.Component</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Component</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">BeanB </span><span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">final</span> BeanA beanA<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #0066bb; font-weight: bold;">BeanB</span><span style="color: #333333;">(</span>BeanA beanA<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">beanA</span> <span style="color: #333333;">=</span> beanA<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br />
<div><span style="font-family: arial;">Main Application Class,</span></div><span style="font-family: arial;"><!--HTML generated using hilite.me--></span><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.boot.SpringApplication</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.boot.autoconfigure.SpringBootApplication</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@SpringBootApplication</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">DemoSpringBootApplication</span><span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
SpringApplication<span style="color: #333333;">.</span><span style="color: #0000cc;">run</span><span style="color: #333333;">(</span>HMSApplication<span style="color: #333333;">.</span><span style="color: #0000cc;">class</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div><div><br /></div><span style="font-family: arial;">When we run the application using IDE or command(mvn spring-boot:run) during runtime throws below error,</span><div><span style="font-family: arial;"><br /></span>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;">Caused <span style="color: #997700; font-weight: bold;">by:</span> org<span style="color: #333333;">.</span><span style="color: #0000cc;">springframework</span><span style="color: #333333;">.</span><span style="color: #0000cc;">beans</span><span style="color: #333333;">.</span><span style="color: #0000cc;">factory</span><span style="color: #333333;">.</span><span style="color: #0000cc;">BeanCurrentlyInCreationException</span><span style="color: #333333;">:</span>
Error creating bean with name <span style="background-color: #ffaaaa; color: red;">'</span>beanA<span style="background-color: #ffaaaa; color: red;">'</span><span style="color: #333333;">:</span>
Requested bean is currently in <span style="color: #997700; font-weight: bold;">creation:</span> Is there an unresolvable circular reference<span style="color: #333333;">?</span>
</pre></div>
<br /></div><div><span style="font-family: arial; font-size: medium;"><b>Solution: </b></span></div><div><span style="font-family: arial;"><b><br /></b></span></div><div><span style="font-family: arial;"><b>1) Use Setter injection</b></span></div><div><span style="font-family: arial;"><b><br /></b></span></div><div><span style="font-family: arial;"><div> As see in the above example about circular dependency as there used construction injection so here the solution is to edit the source code of the class to be configured by setter injection instead of constructor injection.</div><div> In such example need to avoid constructor injection and to use setter injection only. </div><div><br />
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.stereotype.Component</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Component</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">BeanA</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">final</span> BeanB beanB<span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Autowired</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setBeanB</span><span style="color: #333333;">(</span>BeanB beanB<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">beanB</span><span style="color: #333333;">=</span> beanB<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br />
</div><div>Second Bean,</div><div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.stereotype.Component</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Component</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">BeanB</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">final</span> BeanA beanA<span style="color: #333333;">;</span></pre><pre style="line-height: 125%; margin: 0px;"> </pre><pre style="line-height: 125%; margin: 0px;"><span style="color: #555555; font-weight: bold;"> @Autowired</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setBeanA</span><span style="color: #333333;">(</span>BeanA beanA<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">beanA</span><span style="color: #333333;">=</span> beanA<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
</div><div><br /></div><div>The above approach is not recommended, you can configure circular dependencies with setter injection as well.</div><div><br /></div><div><b>2) Use Lazy Initialization</b></div><div><b><br /></b></div><div>One of the dependency bean to define as @Lazy so it will load the bean whenever it's called. It's not being intialized during the application start up. So this is one of the way to handle the circular dependency issue.</div><div><br /></div><div>In the previous example, need to use @Lazy annotation at BeanA class with BeanB setter injenction.</div><div><br /></div></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.springframework.stereotype.Component</span><span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Component</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">BeanA</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">final</span> BeanB beanB<span style="color: #333333;">;</span>
<span style="color: #555555; font-weight: bold;">@Autowired</span>
<span style="color: #555555; font-weight: bold;">@Lazy</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">setBeanB</span><span style="color: #333333;">(</span>BeanB beanB<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">beanB</span><span style="color: #333333;">=</span> beanB<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><br /></div><div><i><span style="font-family: arial;">Thank you for visiting the blog.</span></i></div><div><br /></div><div><br /></div><div><b><span style="font-family: arial;">Reference Post:-</span></b></div><div><b><span style="font-family: arial;">1) <a href="http://adnjavainterview.blogspot.com/2018/01/spring-qualifier-annotation-with-example.html" target="_blank">Spring @Qualifier Annotation with example</a></span></b></div><div><b>2) <a href="http://adnjavainterview.blogspot.com/2018/01/what-is-autowiring-in-spring-expain.html" target="_blank"><span style="font-family: arial;">What is Autowiring in Spring ? Explain Autowiring modes and limitations with examples</span></a></b></div><div><b><span style="font-family: arial;">3) <a href="https://adnjavainterview.blogspot.com/2017/08/what-are-different-spring-bean-scopes.html" target="_blank">What are different Spring Bean Scopes?</a></span></b></div><div><b>4) <a href="https://adnjavainterview.blogspot.com/2017/11/spring-annotations.html" target="_blank"><span style="font-family: arial;">Spring Annotations</span></a></b></div><div><b><span style="font-family: arial;">5) <a href="https://adnjavainterview.blogspot.com/2018/01/what-is-ioc-container-in-spring.html" target="_blank">What is IOC Container in Spring? Difference between BeanFactory and ApplicationContext</a></span></b></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0tag:blogger.com,1999:blog-205030209684246887.post-77897342292241244142023-08-02T23:56:00.003+05:302023-09-11T08:11:57.230+05:30mobtexting company Java Technical Lead interview questions<p><span style="font-family: arial;"> <span style="font-size: medium;"><span style="background-color: white; color: #222222;">Below are the list of java technical lead interview questions asked </span></span></span><span style="background-color: white; color: #222222; font-family: arial; font-size: large;">in <a href="https://www.mobtexting.com/" target="_blank">mobtexting</a> company.</span></p><p><b><span style="font-family: arial; font-size: medium;">1) </span></b></p>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-family: arial;">List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> <span style="color: #008800;">new</span> ArrayList<span style="color: #333333;"><</span>String<span style="color: #333333;">>();</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"abc"</span><span style="color: #333333;">);</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> linkedList <span style="color: #333333;">=</span> <span style="color: #008800;">new</span> LinkedList<span style="color: #333333;"><</span>String<span style="color: #333333;">>();</span>
linkedList<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"abc"</span><span style="color: #333333;">);</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">() </span>
linkedList<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">(); </span></span>
</pre></div>
<div><br /></div><div><span style="font-family: arial; font-size: medium;">In the above code, which one is faster, whether list.get or linkedList.get ?</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><i><b>Answer </b></i>:- list.get is faster than linkedList. more details refer <a href="https://bikashdubey42.medium.com/arraylist-vs-linkedlist-in-java-633e68f49d22" target="_blank">Time Complexity of Collection API</a></span></div><div><br /></div><div><b>2) </b></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-family: arial;">HashMap<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span> String<span style="color: #333333;">></span> map <span style="color: #333333;">=</span> <span style="color: #008800;">new</span> HashMap<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span> String<span style="color: #333333;">>();</span>
map<span style="color: #333333;">.</span><span style="color: #0000cc;">put</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"a"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"abc"</span><span style="color: #333333;">);</span>
TreeMap<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span> String<span style="color: #333333;">></span> treeMap <span style="color: #333333;">=</span> <span style="color: #008800;">new</span> TreeMap<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span> String<span style="color: #333333;">>();</span>
treeMap<span style="color: #333333;">.</span><span style="color: #0000cc;">put</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"a"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"abc"</span><span style="color: #333333;">);</span>
map<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"a"</span><span style="color: #333333;">);</span>
treeMap<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"a"</span><span style="color: #333333;">);</span></span>
</pre></div>
<br /><div><span style="font-family: arial; font-size: medium;">Which one is faster HashMap or TreeMap retrieval ? (<a href="https://stackabuse.com/hashmap-and-treemap-in-java-differences-and-similarities/" target="_blank">Answer</a>)</span></div><div><span style="font-family: arial; font-size: medium;">and also explain the internal working of HashMap and TreeMap ?</span></div><div><span style="font-family: arial;"><br /></span></div><div><span style="font-family: arial;"><b>3) </b></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Key</span><span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">private</span> String name<span style="color: #333333;">,</span>
<span style="color: #008800; font-weight: bold;">private</span> String id<span style="color: #333333;">;</span>
<span style="color: #888888;">//constructir and Setters and getters</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Main</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
Map<span style="color: #333333;"><</span>Key<span style="color: #333333;">,</span> String<span style="color: #333333;">></span> map <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> HahsMap<span style="color: #333333;"><</span>Key<span style="color: #333333;">,</span> String<span style="color: #333333;">>();</span>
Key a <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Key<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"XYZ"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"123"</span><span style="color: #333333;">);</span>
Key b <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Key<span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"XYZ"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"234"</span><span style="color: #333333;">);</span>
map<span style="color: #333333;">.</span><span style="color: #0000cc;">put</span><span style="color: #333333;">(</span>a<span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"Value"</span><span style="color: #333333;">);</span>
map<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">(</span>b<span style="color: #333333;">);</span>
map<span style="color: #333333;">.</span><span style="color: #0000cc;">get</span><span style="color: #333333;">(</span>a<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<div><span style="font-family: arial;"><b><br /></b></span></div><div><span style="font-family: arial; font-size: medium;">What is the output for map.get code ?</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>4) </b>What is dependency injection ? (<a href="https://www.geeksforgeeks.org/spring-dependency-injection-with-example/" target="_blank">Answer</a>) What is diffrence between Spring MVC and Spring boot?(<a href="https://www.simplilearn.com/tutorials/spring-boot-tutorial/spring-vs-spring-boot" target="_blank">Spring</a>)</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-size: medium;"><span style="font-family: arial;"><b>5)</b> </span><span style="font-family: arial;">What is difference between setter injection and constructor injection.(<a href="https://www.java4coding.com/contents/spring/setter-vs-constructor-injection" target="_blank">Answer</a>)</span></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>6)</b> What are the different ways to create bean ? (<a href="https://www.geeksforgeeks.org/how-to-create-a-spring-bean-in-3-different-ways/" target="_blank">Answer</a>)</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>7)</b> Explain Spring bean scopes and Spring bean singleton is thread safe or not ?(<a href="http://adnjavainterview.blogspot.com/2017/08/what-are-different-spring-bean-scopes.html" target="_blank">Answer</a>)</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>8)</b> What are IOC containers in spring ?(<a href="https://adnjavainterview.blogspot.com/2018/01/what-is-ioc-container-in-spring.html" target="_blank">Answer</a>)</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>9)</b> What is difference between Hibernate and JPA? can we use hibernate without JPA?</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>10)</b> Explain JPA entity lifecycle/status.(<a href="https://adnjavainterview.blogspot.com/2018/05/what-are-different-states-of-entity.html" target="_blank">Answer</a>)</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>11)</b> What is eager and lazy loading ? whats N+1 problem and solution(<a href="https://springhow.com/n-plus-1-selects-problem-in-hibernate/" target="_blank">Answer</a>)</span></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>12)</b> Explain first level and second level caching in hibernate(<a href="https://adnjavainterview.blogspot.com/2018/06/what-is-hibernate-caching-explain-first.html" target="_blank">Answer</a>)</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-size: medium;"><span style="font-family: arial;"><b>13)</b> Explain ACID properties of transaction</span><span style="font-family: arial;"> (<a href="https://adnjavainterview.blogspot.com/2018/01/transaction-management-in-spring.html" target="_blank">Answer</a>)</span></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-size: medium;"><span style="font-family: arial;"><b>14) </b></span><span style="font-family: arial;">SQL</span></span></div>
<span style="font-size: medium;"><!--HTML generated using hilite.me--></span><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">create</span> <span style="color: #008800; font-weight: bold;">table</span> person(
id <span style="color: #007020;">integer</span>,
name <span style="color: #007020;">varchar</span>,
<span style="color: #008800; font-weight: bold;">primary</span> <span style="color: #008800; font-weight: bold;">key</span>(id)
);
<span style="color: #008800; font-weight: bold;">create</span> car_owner(
person_id <span style="color: #007020;">integer</span>(it<span style="background-color: #fff0f0;">'s primary key of person table),</span>
<span style="background-color: #fff0f0;"> car_id integer (it'</span>s <span style="color: #008800; font-weight: bold;">primary</span> <span style="color: #008800; font-weight: bold;">key</span> <span style="color: #008800; font-weight: bold;">of</span> car <span style="color: #008800; font-weight: bold;">table</span>)
);
<span style="color: #008800; font-weight: bold;">create</span> <span style="color: #008800; font-weight: bold;">table</span> car(
id <span style="color: #007020;">integer</span>,
car_brand <span style="color: #007020;">varchar</span>,
car_color <span style="color: #007020;">varchar</span>,
);
</pre></div>
<div><span style="font-family: arial;"><b> </b></span></div><div><span style="font-family: arial; font-size: medium;"> Write a query to return car brand and car owner name for all the owners</span></div><div><span style="font-family: arial;"><b> </b></span></div><div><span style="font-family: arial;"><b> </b><i><span style="font-size: medium;">Answer </span></i><b>:- </b></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">SELECT</span> p.name, <span style="color: #008800; font-weight: bold;">c</span>.brand <span style="color: #008800; font-weight: bold;">FROM</span> person p, car_owner co, car <span style="color: #008800; font-weight: bold;">c</span>
<span style="color: #008800; font-weight: bold;">WHERE</span> p.id <span style="color: #333333;">=</span> co.person_id <span style="color: #008800; font-weight: bold;">AND</span>
co.car_id <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">c</span>.id
</pre></div>
<div><span style="font-family: arial;"><b> </b></span></div><div><span style="font-size: medium;"><span style="font-family: arial;"> W</span><span style="font-family: arial;">rite a query to count the total cars for each color greater than 10.</span></span></div><div><span style="font-family: arial;"><b> </b></span></div><div><span style="font-family: arial;"><b> </b><i><span style="font-size: medium;">Answer </span></i><b>- </b></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">SELECT</span> <span style="color: #008800; font-weight: bold;">count</span>(<span style="color: #333333;">*</span>), color <span style="color: #008800; font-weight: bold;">FROM</span> car
<span style="color: #008800; font-weight: bold;">group</span> <span style="color: #008800; font-weight: bold;">by</span> color <span style="color: #008800; font-weight: bold;">HAVING</span> <span style="color: #008800; font-weight: bold;">count</span>(<span style="color: #333333;">*</span>) <span style="color: #333333;">></span> <span style="color: #0000dd; font-weight: bold;">10</span>;
</pre></div>
<br /><div><br /></div><div><span style="font-family: arial; font-size: medium;">Thank you for visiting the blog.</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><p><span style="font-family: arial; font-size: medium;"><b>Reference Posts:-</b></span></p><div><span style="font-family: arial; font-size: medium;"><b>1) <a href="https://adnjavainterview.blogspot.com/2023/07/publicis-sapient-java-technical-lead-interview-questions.html" target="_blank">Publicis Sapient Java Technical Lead Interview questions</a></b></span></div><div><span style="font-family: arial; font-size: medium;"><b>2) <a href="https://adnjavainterview.blogspot.com/2023/08/ltimindtree-java-technica-lead-interview-questions.html" target="_blank">LTIMindtree interview questions</a></b></span></div><div><span style="font-family: arial; font-size: medium;"><b>3) <a href="https://adnjavainterview.blogspot.com/2023/08/akkodis-company-java-technical-leadexp-interview-questions.html" target="_blank">AKKODIS company interview questions</a></b></span></div></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0tag:blogger.com,1999:blog-205030209684246887.post-38389920347614724472023-07-27T19:34:00.001+05:302023-07-27T22:18:31.719+05:30Strategy Design Pattern <span style="font-family: arial; font-size: medium;">In this post, we need to learn Strategy Design pattern with real time examples. For all design pattern posts refer - <a href="https://adnjavainterview.blogspot.com/p/design-patterns.html" target="_blank"><i>Design Patterns</i></a></span><div><span style="font-family: arial;"><span style="font-size: medium;"> </span></span></div><div><span style="font-family: arial;"><span style="font-size: medium;"> Strategy design pattern is one of the <a href="https://stackabuse.com/behavioral-design-patterns-in-java/" target="_blank">behavioral design pattern</a>. Strategy pattern is used when we have multiple algorithms for a specific task and client decides the actual implementation to be used at runtime. Strategy pattern provides more flexibility, extensibility, and choice.</span></span><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"> There are many real time examples like payment implementation for different payment types, different sorting algorithms and many more.</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-size: medium;"><span style="font-family: arial;"> I will take a GST tax example and will discuss in more detail with code, so GST tax varies between each products. </span><span style="font-family: arial;">There are four slabs - 5% GST, 12%GST, 18% GST and 28% GST(currently it's four slabs, it might change in future) and some of products doesn't have GST I mean it's a zero slab, but for code implementation we are not considering that. </span></span></div><div><span style="font-family: arial;"><span style="font-size: medium;"><br /></span></span></div><div><span style="font-family: arial; font-size: medium;"> Instead of creating and writing logic in four different classes for each slab, we need to follow the Strategy design pattern it's more flexible, readable and reusable code and also it follows the OOP's design principle.</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;">First step is to create a interface and then for each slab need to create seperate class and implements the interface.</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial;"><i>TaxCalculation.java</i></span></div></div><div><span style="font-family: arial;"><i><br /></i></span></div>
<i><!--HTML generated using hilite.me--></i><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">interface</span> <span style="color: #bb0066; font-weight: bold;">TaxCalculation</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> Double <span style="color: #0066bb; font-weight: bold;">calculateTax</span><span style="color: #333333;">(</span>Double productPrice<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: arial; font-size: medium;">Implemented classes, the overriden method calculateTax method logic will differ for each slab.</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial;"><i>FivePercentageTax.java</i></span></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">FivePercentageTax</span> <span style="color: #008800; font-weight: bold;">implements</span> TaxCalculation<span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> Double <span style="color: #0066bb; font-weight: bold;">calculateTax</span><span style="color: #333333;">(</span>Double productPrice<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #333333;">(</span>productPrice <span style="color: #333333;">*</span> <span style="color: #0000dd; font-weight: bold;">5</span><span style="color: #333333;">)</span> <span style="color: #333333;">/</span> <span style="color: #0000dd; font-weight: bold;">100</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: arial;"><i>TwelvePercentageTax.java</i></span></div><div><span style="font-family: arial;"><i><br /></i></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">TwelvePercentageTax</span> <span style="color: #008800; font-weight: bold;">implements</span> TaxCalculation<span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> Double <span style="color: #0066bb; font-weight: bold;">calculateTax</span><span style="color: #333333;">(</span>Double productPrice<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #333333;">(</span>productPrice <span style="color: #333333;">*</span> <span style="color: #0000dd; font-weight: bold;">12</span><span style="color: #333333;">)</span> <span style="color: #333333;">/</span> <span style="color: #0000dd; font-weight: bold;">100</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: arial;"><i>EighteenPercentageTax.java</i></span></div><div><span style="font-family: arial;"><i><br /></i></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">EighteenPercentageTax</span> <span style="color: #008800; font-weight: bold;">implements</span> TaxCalculation <span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> Double <span style="color: #0066bb; font-weight: bold;">calculateTax</span><span style="color: #333333;">(</span>Double productPrice<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #333333;">(</span>productPrice <span style="color: #333333;">*</span> <span style="color: #0000dd; font-weight: bold;">18</span><span style="color: #333333;">)</span> <span style="color: #333333;">/</span> <span style="color: #0000dd; font-weight: bold;">100</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><i><span style="font-family: arial;">TwentyEightPercentageTax.java</span></i></div><div><i><span style="font-family: arial;"><br /></span></i></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">TwentyEightPercentageTax</span> <span style="color: #008800; font-weight: bold;">implements</span> TaxCalculation <span style="color: #333333;">{</span>
<span style="color: #555555; font-weight: bold;">@Override</span>
<span style="color: #008800; font-weight: bold;">public</span> Double <span style="color: #0066bb; font-weight: bold;">calculateTax</span><span style="color: #333333;">(</span>Double productPrice<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #333333;">(</span>productPrice <span style="color: #333333;">*</span> <span style="color: #0000dd; font-weight: bold;">28</span><span style="color: #333333;">)</span> <span style="color: #333333;">/</span> <span style="color: #0000dd; font-weight: bold;">100</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<div><br /></div><div><span style="font-family: arial; font-size: medium;">The below class - GSTTaxCalculator is the main class where I created a method calculateGSTTax, in this need to pass the TaxCalculation implemented class object and using object we can call the claculateTax method it should invoke corresponding tax implementation method.</span></div><br /><div><span style="font-family: arial;"><i>GSTTaxCalculator.java</i></span></div><div><span style="font-family: arial;"><i><br /></i></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">GSTTaxCalculator</span> <span style="color: #333333;">{</span>
<span> </span><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">double</span> taxAmount<span style="color: #333333;">;</span>
<span> </span><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">calculateGSTTax</span><span style="color: #333333;">(</span>TaxCalculation taxCalculation<span style="color: #333333;">,</span> <span style="color: #333399; font-weight: bold;">double</span> productPrice<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">taxAmount</span> <span style="color: #333333;">=</span> taxCalculation<span style="color: #333333;">.</span><span style="color: #0000cc;">calculateTax</span><span style="color: #333333;">(</span>productPrice<span style="color: #333333;">);</span>
<span> </span><span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: arial; font-size: medium;">StrategyDesignPatternDemo.java is a demo class to show the Strategy design outcome. So in your code implemention you just need to invoke above class - GSTTaxCalculator method using object and pass TaxCalculation implemented class object and also to pass the item price, it's just an example. We need to implement the Strategy Design pattern as per your requirement and scenario's.</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial;"><i>StrategyDesignPatternDemo.java</i></span></div><div><span style="font-family: arial;"><i><br /></i></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; font-size: 14px; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">StrategyDesignPatternDemo</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
GSTTaxCalculator gstTaxCalculator <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> GSTTaxCalculator<span style="color: #333333;">();</span>
gstTaxCalculator<span style="color: #333333;">.</span><span style="color: #0000cc;">calculateGSTTax</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> FivePercentageTax<span style="color: #333333;">(),</span> <span style="color: #0000dd; font-weight: bold;">250</span><span style="color: #333333;">);</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"GST tax for 5% slab - "</span><span style="color: #333333;">+</span>gstTaxCalculator<span style="color: #333333;">.</span><span style="color: #0000cc;">taxAmount</span><span style="color: #333333;">);</span>
gstTaxCalculator<span style="color: #333333;">.</span><span style="color: #0000cc;">calculateGSTTax</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> TwelvePercentageTax<span style="color: #333333;">(),</span> <span style="color: #0000dd; font-weight: bold;">250</span><span style="color: #333333;">);</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"GST tax for 12% slab - "</span><span style="color: #333333;">+</span>gstTaxCalculator<span style="color: #333333;">.</span><span style="color: #0000cc;">taxAmount</span><span style="color: #333333;">);</span>
gstTaxCalculator<span style="color: #333333;">.</span><span style="color: #0000cc;">calculateGSTTax</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> EighteenPercentageTax<span style="color: #333333;">(),</span> <span style="color: #0000dd; font-weight: bold;">250</span><span style="color: #333333;">);</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"GST tax for 18% slab - "</span><span style="color: #333333;">+</span>gstTaxCalculator<span style="color: #333333;">.</span><span style="color: #0000cc;">taxAmount</span><span style="color: #333333;">);</span>
gstTaxCalculator<span style="color: #333333;">.</span><span style="color: #0000cc;">calculateGSTTax</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> TwentyEightPercentageTax<span style="color: #333333;">(),</span> <span style="color: #0000dd; font-weight: bold;">250</span><span style="color: #333333;">);</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"GST tax for 28% slab - "</span><span style="color: #333333;">+</span>gstTaxCalculator<span style="color: #333333;">.</span><span style="color: #0000cc;">taxAmount</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: arial;">Output:- <i>GST tax for 5% slab - 12.5</i></span></div><div><span style="font-family: arial;"><i> GST tax for 12% slab - 30.0</i></span></div><div><span style="font-family: arial;"><i> GST tax for 18% slab - 45.0</i></span></div><div><span style="font-family: arial;"><i> GST tax for 28% slab - 70.0</i></span></div><div><br /></div><div><span style="font-family: arial;"><i><br /></i></span></div><div><span style="font-family: arial;"><i>Thank you for visiting the blog.</i></span></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.5945627-15.338635136178846 42.4383127 41.281832536178847 112.7508127tag:blogger.com,1999:blog-205030209684246887.post-30843198169317401422023-07-25T12:04:00.003+05:302023-09-11T08:06:40.030+05:30Publicis Sapient Java Technical Lead Interview questions<p> <span style="font-family: arial; font-size: medium;">Below are the list of java technical lead interview questions asked in <b><a href="https://www.publicissapient.com/" target="_blank">Publicis Sapient</a></b> during the second round.</span></p><p><span style="font-family: arial; font-size: medium;">1) Time complexity for HashMap, LinkedHashMap, ArrayList, LinkedList, ConcurrentHashMap.(<a href="https://www.baeldung.com/java-collections-complexity" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">2) Explain ReentrantLock in java (<a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantLock.html" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">3) What is thread pool and explain the executor framework.</span></p><p><span style="font-family: arial; font-size: medium;">4) Explain java 8 features(<a href="https://adnjavainterview.blogspot.com/2018/09/java-8-features-with-examples.html" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">5) What is difference between map and flatMap method in stream(<a href="https://adnjavainterview.blogspot.com/2023/07/difference-between-map-and-flatmap-method-in-java8.html" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">6) What is the difference between stream and parallel stream. Explain parallel stream performance.</span></p><p><span style="font-family: arial; font-size: medium;">7) What is association and aggregation in java(<a href="https://www.geeksforgeeks.org/association-composition-aggregation-java/" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">8) Explain Solid Design Principles(<a href="http://adnjavainterview.blogspot.com/2021/07/solid-design-principles-with-examples.html" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">9) Difference between SOA and Microservices (<a href="https://www.guru99.com/microservices-vs-soa.html" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">10) Explain Design pattern used in Microservices mainly Circuit design pattern and Saga pattern(<a href="https://dzone.com/articles/design-patterns-for-microservices" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">11) How to communicate two microservices ? (<a href="https://levelup.gitconnected.com/4-ways-to-establish-communication-between-microservices-984207f29497" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">12) Difference between Kafka and RabitMQ(<a href="https://www.designgurus.io/blog/RabbitMQ-Kafka-ActiveMQ-System-Design?gclid=Cj0KCQjwwvilBhCFARIsADvYi7Ka-vg0XbQuMF7k4ulsDOPc5eM-HWiMWOhHxi0Fak0nZ_9kTUBd7GoaAm5-EALw_wcB" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">13) Explain the scope of spring beans (<a href="https://adnjavainterview.blogspot.com/2017/08/what-are-different-spring-bean-scopes.html" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">14) What is the use of spring actuator? (<a href="https://howtodoinjava.com/spring-boot/actuator-endpoints-example/" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">15) Explain Spring security used in application(<a href="https://www.marcobehler.com/guides/spring-security" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial; font-size: medium;">16) Explain if you used any cahcing algorithm in application</span></p><p><span style="font-family: arial; font-size: medium;">17) Which annotations included in @SpringBootApplication (<a href="https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/using-boot-using-springbootapplication-annotation.html" target="_blank">Answer</a>)</span></p><p><span style="font-family: arial;"><br /></span></p><p><span style="font-family: arial; font-size: medium;">Thank you for reading the blog.</span></p><p><span style="font-family: arial; font-size: medium;"><br /></span></p><p><span style="font-family: arial; font-size: medium;"><b>Reference Posts:-</b></span></p><div style="text-align: left;"><span style="font-family: arial; font-size: medium;"><b>1) <a href="https://adnjavainterview.blogspot.com/2023/08/mobtexting-company-java-technical-lead-interview-questions.html" target="_blank">mobtexting company interview questions</a></b></span></div><div style="text-align: left;"><span style="font-family: arial; font-size: medium;"><b>2) <a href="https://adnjavainterview.blogspot.com/2023/08/ltimindtree-java-technica-lead-interview-questions.html" target="_blank">LTIMindtree interview questions</a></b></span></div><div style="text-align: left;"><span style="font-family: arial; font-size: medium;"><b>3) <a href="https://adnjavainterview.blogspot.com/2023/08/akkodis-company-java-technical-leadexp-interview-questions.html" target="_blank">AKKODIS company interview questions</a></b></span></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.5945627-15.338635136178846 42.4383127 41.281832536178847 112.7508127tag:blogger.com,1999:blog-205030209684246887.post-41518569008907290892023-07-24T13:59:00.002+05:302023-09-11T07:58:58.018+05:30Java 8 Stream Coding Interview questions and answers<p> <span style="font-family: arial; font-size: medium;">Before attending any interview need to look into the Stream coding questions beacuse Stream API is an important topic in java 8.</span></p><p><span style="font-family: arial; font-size: medium;">Below are the java 8 stream coding questions and answers.</span></p><p><span style="font-family: arial; font-size: medium;"><b>1) Write a program to print the list of elements from a list using java 8 stream</b></span></p><p>
<b><!--HTML generated using hilite.me--></b></p><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-family: arial; font-size: medium;"><span style="color: #008800;">import</span> <span style="color: #0e84b5;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800;">import</span> <span style="color: #0e84b5;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800;">public</span> <span style="color: #008800;">class</span> <span style="color: #bb0066;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800;">public</span> <span style="color: #008800;">static</span> <span style="color: #333399;">void</span> <span style="color: #0066bb;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800;">new</span> String<span style="color: #333333;">[]</span> <span style="color: #333333;">{</span><span style="background-color: #fff0f0;">"ABC"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"BCD"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"CDE"</span><span style="color: #333333;">});</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s <span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span></span></pre></div>
<br /><div><span style="font-family: arial; font-size: medium;"><i>Output:- ABC</i></span></div><div><span style="font-family: arial; font-size: medium;"><i><span> </span><span> </span><span> </span><span> BCD</span><br /></i></span></div><div><span><span style="font-family: arial; font-size: medium;"><i> CDE</i></span></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>2) Write a program to convert lowercase string element of a list into uppercase.</b></span></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> String<span style="color: #333333;">[]</span> <span style="color: #333333;">{</span><span style="background-color: #fff0f0;">"Abc"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"bcd"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"cde"</span><span style="color: #333333;">});</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">map</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span> s<span style="color: #333333;">.</span><span style="color: #0000cc;">toUpperCase</span><span style="color: #333333;">()).</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>
s <span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span></span>
</pre></div>
<br />
<div><div><span style="font-family: arial; font-size: medium;"><i>Output:- ABC</i></span></div><div><span style="font-family: arial; font-size: medium;"><i><span> </span><span> </span><span> </span><span> BCD</span><br /></i></span></div><div><span><span style="font-family: arial; font-size: medium;"><i> CDE</i></span></span></div></div><div><span><span style="font-family: arial;"><i><br /></i></span></span></div><div><span style="font-family: arial; font-size: medium;"><b>3) Print a list of strings those start with letter "A" and return updated list.</b></span></div><div><span style="font-family: arial;"><br /></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.stream.Collectors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> String<span style="color: #333333;">[]</span> <span style="color: #333333;">{</span><span style="background-color: #fff0f0;">"Abc"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"bcd"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"cde"</span><span style="color: #333333;">});</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> updatedList <span style="color: #333333;">=</span> list<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">filter</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span> s<span style="color: #333333;">.</span><span style="color: #0000cc;">startsWith</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">))</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">collect</span><span style="color: #333333;">(</span>Collectors<span style="color: #333333;">.</span><span style="color: #0000cc;">toList</span><span style="color: #333333;">());</span>
updatedList<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s <span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span></span>
</pre></div>
<br /><div><div><span style="font-family: arial; font-size: medium;"><i>Output:- Abc</i></span></div></div><div><span style="font-family: arial; font-size: medium;"><i><br /></i></span></div><div><b><span style="font-size: medium;"><span style="font-family: arial;">4) W</span><span style="font-family: arial;">rite a code to calculate the summation of the integers from the list.</span></span></b></div><div><span style="font-family: arial;"><br /></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Integer<span style="color: #333333;">[]</span> <span style="color: #333333;">{</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">4</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">5</span><span style="color: #333333;">});</span>
<span style="color: #888888;">//Using lambda expression</span>
Integer sum <span style="color: #333333;">=</span> list<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">mapToInt</span><span style="color: #333333;">(</span>i <span style="color: #333333;">-></span> i<span style="color: #333333;">).</span><span style="color: #0000cc;">sum</span><span style="color: #333333;">();</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>sum<span style="color: #333333;">);</span>
<span style="color: #888888;">//Using Integer valueOf method</span>
Integer sumOfIntegers <span style="color: #333333;">=</span> list<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">mapToInt</span><span style="color: #333333;">(</span><span style="color: #997700; font-weight: bold;">Integer:</span><span style="color: #333333;">:</span>valueOf<span style="color: #333333;">).</span><span style="color: #0000cc;">sum</span><span style="color: #333333;">();</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>sumOfIntegers<span style="color: #333333;">);</span>
<span style="color: #888888;">//Using Integer intValue method</span>
Integer sumOfIntegerValue <span style="color: #333333;">=</span> list<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">mapToInt</span><span style="color: #333333;">(</span><span style="color: #997700; font-weight: bold;">Integer:</span><span style="color: #333333;">:</span>intValue<span style="color: #333333;">).</span><span style="color: #0000cc;">sum</span><span style="color: #333333;">();</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>sumOfIntegerValue<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span></span>
</pre></div>
<br /><div><i style="font-family: arial;"><span style="font-size: medium;">Output:- 15</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> 15</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> 15</span></i></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>5) Create a Map object from List using Stream API</b></span></div><div><span style="font-size: medium;"><br /></span></div>
<span style="font-size: medium;"><!--HTML generated using hilite.me--></span><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Map</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.stream.Collectors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> String<span style="color: #333333;">[]</span> <span style="color: #333333;">{</span><span style="background-color: #fff0f0;">"Ab"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"Bcd"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"Ce"</span><span style="color: #333333;">});</span>
Map<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span> Integer<span style="color: #333333;">></span> map <span style="color: #333333;">=</span> list<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">collect</span><span style="color: #333333;">(</span>Collectors<span style="color: #333333;">.</span><span style="color: #0000cc;">toMap</span><span style="color: #333333;">(</span>
s<span style="color: #333333;">-></span>s<span style="color: #333333;">,</span> s<span style="color: #333333;">-></span>s<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;">()));</span>
map<span style="color: #333333;">.</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">((</span>x<span style="color: #333333;">,</span>v<span style="color: #333333;">)</span> <span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"key - "</span><span style="color: #333333;">+</span>x<span style="color: #333333;">+</span><span style="background-color: #fff0f0;">", value - "</span><span style="color: #333333;">+</span>v<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span></span>
</pre></div>
<br /><div><i style="font-family: arial;"><span style="font-size: medium;">Output:-</span></i></div><div><span><div style="font-style: italic;"><span style="font-family: arial; font-size: medium;">key - Ab, value - 2</span></div><div style="font-style: italic;"><span style="font-family: arial; font-size: medium;">key - Ce, value - 2</span></div><div style="font-style: italic;"><span style="font-family: arial; font-size: medium;">key - Bcd, value - 3</span></div><div style="font-family: arial; font-style: italic;"><span style="font-size: medium;"><br /></span></div><div style="font-family: arial;"><b><span style="font-size: medium;">6) Write a program to list the distinct integers from list or remove duplicates from the list.</span></b></div><div style="font-family: arial;"><br /></div></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.stream.Collectors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Integer<span style="color: #333333;">[]</span> <span style="color: #333333;">{</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">4</span><span style="color: #333333;">});</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> listWithDistinct <span style="color: #333333;">=</span> list<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">distinct</span><span style="color: #333333;">().</span><span style="color: #0000cc;">collect</span><span style="color: #333333;">(</span>Collectors<span style="color: #333333;">.</span><span style="color: #0000cc;">toList</span><span style="color: #333333;">());</span>
listWithDistinct<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span></span>
</pre></div>
<br /><div><i style="font-family: arial;"><span style="font-size: medium;">Output:-1</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> 2</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> 4</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"><br /></span></i></div><div><span style="font-family: arial; font-size: medium;"><b>7) Write a program to find the Minimum number from a Stream/list.</b></span></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Integer<span style="color: #333333;">[]</span> <span style="color: #333333;">{</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">4</span><span style="color: #333333;">});</span>
Integer minNumber <span style="color: #333333;">=</span> list<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">mapToInt</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span>s<span style="color: #333333;">).</span><span style="color: #0000cc;">min</span><span style="color: #333333;">().</span><span style="color: #0000cc;">getAsInt</span><span style="color: #333333;">();</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>minNumber<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span></span>
</pre></div>
<i style="font-family: arial;"><div><i style="font-family: arial;"><br /></i></div><span style="font-size: medium;">Output:-1</span></i><div><i style="font-family: arial;"><span style="font-size: medium;"><br /></span></i></div><div><b><span style="font-size: medium;"><span style="font-family: arial;">8) </span><span style="font-family: arial;">Write a program to find the Maximum number from a Stream/list.</span></span></b></div><div><span style="font-family: arial;"><br /></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Integer<span style="color: #333333;">[]</span> <span style="color: #333333;">{</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">4</span><span style="color: #333333;">});</span>
Integer maxNumber <span style="color: #333333;">=</span> list<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">mapToInt</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span>s<span style="color: #333333;">).</span><span style="color: #0000cc;">max</span><span style="color: #333333;">().</span><span style="color: #0000cc;">getAsInt</span><span style="color: #333333;">();</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>maxNumber<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span></span>
</pre></div>
<br /><div><i style="font-family: arial;"><span style="font-size: medium;">Output:-4</span></i></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>9) Write a program to sort the given list using java 8 stream.</b></span></div><div><span style="font-family: arial;"><br /></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.stream.Collectors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Integer<span style="color: #333333;">[]</span> <span style="color: #333333;">{</span><span style="color: #0000dd; font-weight: bold;">8</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">4</span><span style="color: #333333;">});</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> sortedList <span style="color: #333333;">=</span> list<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">sorted</span><span style="color: #333333;">().</span><span style="color: #0000cc;">collect</span><span style="color: #333333;">(</span>Collectors<span style="color: #333333;">.</span><span style="color: #0000cc;">toList</span><span style="color: #333333;">());</span>
sortedList<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span></span>
</pre></div>
<br /><div><i style="font-family: arial;"><span style="font-size: medium;">Output:-2</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> 3</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> 4</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> 8</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"><br /></span></i></div><div><span style="font-family: arial; font-size: medium;"><b>10) Write a program to sort the given list in descending order using stream.</b></span></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Collections</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.stream.Collectors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Integer<span style="color: #333333;">[]</span> <span style="color: #333333;">{</span><span style="color: #0000dd; font-weight: bold;">8</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">4</span><span style="color: #333333;">});</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> sortedList <span style="color: #333333;">=</span> list<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">sorted</span><span style="color: #333333;">(</span>
Collections<span style="color: #333333;">.</span><span style="color: #0000cc;">reverseOrder</span><span style="color: #333333;">()).</span><span style="color: #0000cc;">collect</span><span style="color: #333333;">(</span>Collectors<span style="color: #333333;">.</span><span style="color: #0000cc;">toList</span><span style="color: #333333;">());</span>
sortedList<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</span></pre></div>
<br /><div><div><i style="font-family: arial;"><span style="font-size: medium;">Output:-8</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> 4</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> 3</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> 2</span></i></div></div><div><i style="font-family: arial;"><span style="font-size: medium;"><br /></span></i></div><div><span style="font-family: arial; font-size: medium;"><b>11) Write a program to count the total number of integers in the list.</b></span></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Integer<span style="color: #333333;">[]</span> <span style="color: #333333;">{</span><span style="color: #0000dd; font-weight: bold;">8</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">4</span><span style="color: #333333;">});</span>
<span style="color: #333399; font-weight: bold;">long</span> count <span style="color: #333333;">=</span> list<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">count</span><span style="color: #333333;">();</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>count<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</span></pre></div>
<br /><div><i style="font-family: arial;"><span style="font-size: medium;">Output:-4</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"><br /></span></i></div><div><span style="font-family: arial; font-size: medium;"><b>12) Convert List<List<String>> into List<String> using flatMap method.</b></span></div><div><span style="font-family: arial;"><br /></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.ArrayList</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.stream.Collectors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">>></span> listListOfStrings <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><</span>List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">>>();</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Integer<span style="color: #333333;">[]</span> <span style="color: #333333;">{</span><span style="color: #0000dd; font-weight: bold;">8</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">3</span><span style="color: #333333;">,</span><span style="color: #0000dd; font-weight: bold;">4</span><span style="color: #333333;">});</span>
listListOfStrings<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>list<span style="color: #333333;">);</span>
<span style="color: #888888;">//use flatMap method</span>
List<span style="color: #333333;"><</span>Integer<span style="color: #333333;">></span> newList <span style="color: #333333;">=</span> listListOfStrings<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">flatMap</span><span style="color: #333333;">(</span>
s<span style="color: #333333;">-></span> s<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">()).</span><span style="color: #0000cc;">collect</span><span style="color: #333333;">(</span>Collectors<span style="color: #333333;">.</span><span style="color: #0000cc;">toList</span><span style="color: #333333;">());</span>
newList<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span></span>
</pre></div>
<br /><div><div><i style="font-family: arial;"><span style="font-size: medium;">Output:-8</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> 2</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> 3</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> 4</span></i></div></div><div><i style="font-family: arial;"><span style="font-size: medium;"><br /></span></i></div><div><span style="font-family: arial; font-size: medium;"><b>13) Group by a list element and display the total count of the element using Collectors groupingBy method.</b></span></div><div><span style="font-family: arial;"><br /></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Map</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.function.Function</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.stream.Collectors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> letters <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"B"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"C"</span><span style="color: #333333;">,</span><span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"D"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"C"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"D"</span><span style="color: #333333;">);</span>
Map<span style="color: #333333;"><</span>String<span style="color: #333333;">,</span> Long<span style="color: #333333;">></span> mapWithLetterCount <span style="color: #333333;">=</span> letters<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">collect</span><span style="color: #333333;">(</span>
Collectors<span style="color: #333333;">.</span><span style="color: #0000cc;">groupingBy</span><span style="color: #333333;">(</span>
Function<span style="color: #333333;">.</span><span style="color: #0000cc;">identity</span><span style="color: #333333;">(),</span> Collectors<span style="color: #333333;">.</span><span style="color: #0000cc;">counting</span><span style="color: #333333;">()));</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>mapWithLetterCount<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span></span>
</pre></div>
<br /><div><div><span style="font-family: arial; font-size: medium;">Output:- <i>{A=2, B=1, C=2, D=2}</i></span></div></div><div><span style="font-family: arial; font-size: medium;"><i><br /></i></span></div><div><span style="font-family: arial; font-size: medium;"><b>14) Find the first element from the given list using java 8 stream.</b></span></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> letters <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"B"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"C"</span><span style="color: #333333;">,</span><span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"D"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"C"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"D"</span><span style="color: #333333;">);</span>
String firstElement <span style="color: #333333;">=</span> letters<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">findFirst</span><span style="color: #333333;">().</span><span style="color: #0000cc;">orElse</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"Default"</span><span style="color: #333333;">);</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>firstElement<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</span></pre></div>
<br /><div><i style="font-family: arial;"><span style="font-size: medium;">Output:-A</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"><br /></span></i></div><div><span style="font-family: arial; font-size: medium;"><b>15) anyMatch code example - if any one of the list element matches with given character "A" then return true.</b></span></div><div><br /></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> letters <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"B"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"C"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"D"</span><span style="color: #333333;">);</span>
<span style="color: #333399; font-weight: bold;">boolean</span> foundElement <span style="color: #333333;">=</span> letters<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">anyMatch</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span> s<span style="color: #333333;">.</span><span style="color: #0000cc;">equalsIgnoreCase</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">));</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>foundElement<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span></span>
</pre></div>
<br /><div><i style="font-family: arial;"><span style="font-size: medium;">Output:-true</span></i></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>16) allMatch code example - if all of the elements matches with the given element say "A" then return true else false.</b></span></div><div><span style="font-family: arial;"><br /></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> letters <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"B"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"C"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"D"</span><span style="color: #333333;">);</span>
<span style="color: #333399; font-weight: bold;">boolean</span> foundMatches <span style="color: #333333;">=</span> letters<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">allMatch</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span> s<span style="color: #333333;">.</span><span style="color: #0000cc;">equalsIgnoreCase</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">));</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>foundMatches<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</span></pre></div>
<br /><div><i style="font-family: arial;"><span style="font-size: medium;">Output:-false</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"><br /></span></i></div><div><span style="font-family: arial; font-size: medium;"><b>17) Write a program to remove the duplicates from the list using Collectors toSet method.</b></span></div><div><span style="font-family: arial;"><br /></span></div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Set</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.stream.Collectors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Java8Stream</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> letters <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"B"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"C"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"D"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"A"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"B"</span><span style="color: #333333;">);</span>
Set<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> nonDuplicateSet <span style="color: #333333;">=</span> letters<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">collect</span><span style="color: #333333;">(</span>Collectors<span style="color: #333333;">.</span><span style="color: #0000cc;">toSet</span><span style="color: #333333;">());</span>
nonDuplicateSet<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span></span>
</pre></div>
<br /><div><i style="font-family: arial;"><span style="font-size: medium;">Output:-A</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> B</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> C</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> D</span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"><br /></span></i></div><div><i style="font-family: arial;"><span style="font-size: medium;"> </span></i></div><div><span style="font-family: arial; font-size: medium;">Thank you for reading the post.</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><b>Reference Posts:-</b></span></div><div><span style="font-family: arial; font-size: medium;"><b>1) <a href="https://adnjavainterview.blogspot.com/2023/09/sort-list-in-ascending-descending-order-in-java-8-using-stream-api.html" target="_blank">Sort list in ascending and descending order using java 8 streams</a></b></span></div><div><span style="font-family: arial; font-size: medium;"><b>2) </b><a href="https://adnjavainterview.blogspot.com/2023/08/java-8-optional-class-with-examples.html" target="_blank"><b>Java 8 optional class usages</b></a></span></div><div><span style="font-family: arial; font-size: medium;"><b>3) </b><a href="https://adnjavainterview.blogspot.com/2023/07/difference-between-map-and-flatmap-method-in-java8.html" target="_blank"><b>difference between map and flatmap method in java8</b></a></span></div><div><b><span style="font-family: arial; font-size: medium;">4) <a href="https://adnjavainterview.blogspot.com/2023/08/find-duplicate-elements-from-given-list-using-java8.html" target="_blank">F<span style="color: #2b00fe;">ind duplicate elements from the list using java stream</span></a></span></b></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.5945627-15.338635136178846 42.4383127 41.281832536178847 112.7508127tag:blogger.com,1999:blog-205030209684246887.post-91214745347833517012023-07-19T17:52:00.000+05:302023-07-19T17:52:04.389+05:30Difference between map and flatMap method in Java Stream<p> <span style="font-family: arial;"> A </span><span style="font-family: times; font-size: medium;">Stream API is an important feature introduced in java 8, Stream is not a data structure means not stores any data and also not modifying the data but it will operate with the data source and to process data in convenient and faster. Stream API is a cleaner and more readable code.</span></p><p><span style="font-family: times; font-size: medium;">Let us discuss the map and flatMap method usages and differences.</span></p><p><i><span style="font-family: times; font-size: medium;"><b>Use a map() of stream method if you just want to transform one Stream into another where each element gets converted to one single value. </b></span></i></p><p><i><span style="font-family: times; font-size: medium;"><b>Use flatMap() of stream method if the function used by map operation returns multiple values and you want just one list containing all the values of the lists.</b></span></i></p><p><span style="font-family: times; font-size: medium;">map method code example,</span></p><p>
<!--HTML generated using hilite.me--></p><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.stream.Collectors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">StreamMapExamples</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> listOfStrings <span style="color: #333333;">=</span> Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> String<span style="color: #333333;">[]{</span><span style="background-color: #fff0f0;">"abc"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"bcd"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"cde"</span><span style="color: #333333;">});</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> list <span style="color: #333333;">=</span> listOfStrings<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">()</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">map</span><span style="color: #333333;">(</span> s<span style="color: #333333;">-></span> s<span style="color: #333333;">.</span><span style="color: #0000cc;">toUpperCase</span><span style="color: #333333;">()).</span><span style="color: #0000cc;">collect</span><span style="color: #333333;">(</span>Collectors<span style="color: #333333;">.</span><span style="color: #0000cc;">toList</span><span style="color: #333333;">());</span>
list<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<p></p><p><span style="font-family: times; font-size: medium;">the above code will print ABC, BCD and CDE.</span></p><p><span style="font-family: times; font-size: medium;">flatMap code example,</span></p>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.ArrayList</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.stream.Collectors</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">StreamflatMapExample</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>List<span style="color: #333333;"><</span>String<span style="color: #333333;">>></span> listListOfStrings <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><</span>List<span style="color: #333333;"><</span>String<span style="color: #333333;">>>();</span>
listListOfStrings<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> String<span style="color: #333333;">[]{</span><span style="background-color: #fff0f0;">"ABC"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"BCD"</span><span style="color: #333333;">}));</span>
listListOfStrings<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>Arrays<span style="color: #333333;">.</span><span style="color: #0000cc;">asList</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> String<span style="color: #333333;">[]{</span><span style="background-color: #fff0f0;">"CDE"</span><span style="color: #333333;">,</span> <span style="background-color: #fff0f0;">"DEF"</span><span style="color: #333333;">}));</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> listOfStrings <span style="color: #333333;">=</span> listListOfStrings<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">flatMap</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span>s<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">()).</span><span style="color: #0000cc;">collect</span><span style="color: #333333;">(</span>Collectors<span style="color: #333333;">.</span><span style="color: #0000cc;">toList</span><span style="color: #333333;">());</span>
listOfStrings<span style="color: #333333;">.</span><span style="color: #0000cc;">stream</span><span style="color: #333333;">().</span><span style="color: #0000cc;">forEach</span><span style="color: #333333;">(</span>s<span style="color: #333333;">-></span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>s<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre></div>
<br /><div><span style="font-family: times; font-size: medium;">The flatMap method converts list of strings of list into list of strings, the above code will print ABC, BCD, CDE and DEF.</span></div><div><br /></div><div><br /></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.5945627-15.338635136178846 42.4383127 41.281832536178847 112.7508127tag:blogger.com,1999:blog-205030209684246887.post-4068955874788678092023-07-10T09:20:00.001+05:302023-07-10T09:20:51.389+05:30MySQL error - Packet for query is too large (4,739,923 > 65,535). You can change this value on the server by setting the 'max_allowed_packet' variable'<p> <span style="font-family: arial;"><b>Souce - MySQL doc</b> -> A communication packet is a single SQL statement sent to the MySQL server, a single row that is sent to the client, or a binary log event sent from a replication source server to a replica.</span></p><p><span style="font-family: arial;">The largest possible packet that can be transmitted to or from a MySQL 8.0 server or client is 1GB.</span></p><p><span style="font-family: arial;">When MySQL server or client receives packet bigger than default max_allowed_packet size then throw the exception</span> - <i><b>Packet for query is too large (4,739,923 > 65,535). You can change this value on the server by setting the 'max_allowed_packet' variable</b></i></p><p><span style="font-family: arial;">For the above issue, the exception itslef suggests to change the max_allowed_packet size. If you are using or writing/executing query from command prompt then use below query to set or increase the max_allowed_packet.</span></p><p><span style="font-family: arial;">mysqld> <span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-feature-settings: inherit; font-kerning: inherit; font-optical-sizing: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-variation-settings: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;">SET</span><span style="color: var(--highlight-color); font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;"> </span><span class="hljs-keyword" style="border: 0px; box-sizing: inherit; color: var(--highlight-keyword); font-feature-settings: inherit; font-kerning: inherit; font-optical-sizing: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-variation-settings: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;">GLOBAL</span><span style="color: var(--highlight-color); font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;"> max_allowed_packet=</span><span style="color: var(--highlight-color); font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;">128M</span></span></p><p><span style="color: var(--highlight-color); font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;"><span style="font-family: arial;">Or even set 512M </span></span></p><p><span style="color: var(--highlight-color); font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;"><span style="font-family: arial;">If you are using MYSQL workbench then directly execute above query.</span></span></p><p><span style="color: var(--highlight-color); font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;"><span style="font-family: arial;">Default server max_allowed_packet size is 65M.</span></span></p><p><span style="color: var(--highlight-color); font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;"><span style="font-family: arial;">To check the existing max_allowed_packet size then use below query,</span></span></p><p><span style="color: var(--highlight-color); font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;"><span style="font-family: arial;">--> show variables like 'max_allowed_packet'.</span></span></p><p><span style="color: var(--highlight-color); font-style: inherit; font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;"><span style="font-family: arial;"><br /></span></span></p><p><span style="color: var(--highlight-color); font-variant-caps: inherit; font-variant-ligatures: inherit; font-weight: inherit; white-space: inherit;"><span style="font-family: arial;"><i>Thank you for reading the post.</i></span></span></p>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0tag:blogger.com,1999:blog-205030209684246887.post-47534611383395511912023-06-24T17:59:00.112+05:302023-07-09T20:39:09.212+05:30Maven build error: Fatal error compiling: invalid flag: --release -> [Help 1]<p><span style="font-family: arial;"> When try to build the maven java project using mvn clean install or mvn package, you may get this error if maven java version and project java version mismatches.</span></p>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #333333;">[</span>ERROR<span style="color: #333333;">]</span> Failed to execute goal org<span style="color: #333333;">.</span><span style="color: #0000cc;">apache</span><span style="color: #333333;">.</span><span style="color: #0000cc;">maven</span><span style="color: #333333;">.</span><span style="color: #0000cc;">plugins</span><span style="color: #333333;">:</span>maven<span style="color: #333333;">-</span>compiler<span style="color: #333333;">-</span><span style="color: #997700; font-weight: bold;">plugin:</span><span style="color: #6600ee; font-weight: bold;">3.8</span><span style="color: #333333;">.</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">:</span>testCompile
<span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">default</span><span style="color: #333333;">-</span>testCompile<span style="color: #333333;">)</span> on project spring<span style="color: #333333;">-</span>boot<span style="color: #333333;">-</span>postgresql<span style="color: #333333;">-</span><span style="color: #997700; font-weight: bold;">crud:</span>
Fatal error <span style="color: #997700; font-weight: bold;">compiling:</span> invalid target <span style="color: #997700; font-weight: bold;">release:</span> <span style="color: #0000dd; font-weight: bold;">11</span> <span style="color: #333333;">-></span> <span style="color: #333333;">[</span>Help <span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">]</span>
<span style="color: #333333;">[</span>ERROR<span style="color: #333333;">]</span>
<span style="color: #333333;">[</span>ERROR<span style="color: #333333;">]</span> To see the full stack trace of the errors<span style="color: #333333;">,</span> re<span style="color: #333333;">-</span>run Maven with the <span style="color: #333333;">-</span>e <span style="color: #008800; font-weight: bold;">switch</span><span style="color: #333333;">.</span>
<span style="color: #333333;">[</span>ERROR<span style="color: #333333;">]</span> Re<span style="color: #333333;">-</span>run Maven using the <span style="color: #333333;">-</span>X <span style="color: #008800; font-weight: bold;">switch</span> to enable full debug logging<span style="color: #333333;">.</span>
<span style="color: #333333;">[</span>ERROR<span style="color: #333333;">]</span>
<span style="color: #333333;">[</span>ERROR<span style="color: #333333;">]</span> For more information about the errors and possible solutions<span style="color: #333333;">,</span> please read the following <span style="color: #997700; font-weight: bold;">articles:</span>
<span style="color: #333333;">[</span>ERROR<span style="color: #333333;">]</span> <span style="color: #333333;">[</span>Help <span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">]</span> <span style="color: #997700; font-weight: bold;">http:</span><span style="color: #888888;">//cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException</span>
</pre></div>
<p><span style="font-family: arial;">The error is invalid target release: 11 - that means the project’s Java version is 11 but Maven is running under lower JDK version. </span></p><p><span style="font-family: arial;">Need to check maven version which compatible with java version.</span></p><p><span style="font-family: arial;">you can use the maven command, version -> mvn -version,</span></p><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgetEoyPp1Qi9uB2v0N9j3v7-SvyeEFNOXqC7wtkXM8kG5TEVtswHzmfLdMk1pP6FR1SFxotVO2u7XbNxTIuMm8tBf14VujdaPubXbdv2M9dn7KHUytntPgVP89kR6O_NaiENJA9Amx2SVEGl7pTQ8LQX4rxU48TdbccFDOel_jYRn72qs-56r2-oALruM/s836/mavenVersionCapture.JPG" style="margin-left: auto; margin-right: auto;"><img border="0" data-original-height="116" data-original-width="836" height="90" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgetEoyPp1Qi9uB2v0N9j3v7-SvyeEFNOXqC7wtkXM8kG5TEVtswHzmfLdMk1pP6FR1SFxotVO2u7XbNxTIuMm8tBf14VujdaPubXbdv2M9dn7KHUytntPgVP89kR6O_NaiENJA9Amx2SVEGl7pTQ8LQX4rxU48TdbccFDOel_jYRn72qs-56r2-oALruM/w640-h90/mavenVersionCapture.JPG" width="640" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">command prompt - maven version command</td></tr></tbody></table><p><span style="font-family: arial;">So maven compatible java version is 1.8 or below versions.</span></p><p><span style="font-family: arial;"><b>Solutions:-</b></span></p><div style="text-align: left;"><span style="font-family: arial;"><b>1) Changing the project java version i.e compatible java maven version - here it's java 1.8</b></span></div><p><span style="font-family: arial;">If project is a spring boot web application then change pom.xml java version as follows,</span></p><!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #333333;"><</span>properties<span style="color: #333333;">></span>
<span style="color: #333333;"><</span>java<span style="color: #333333;">.</span><span style="color: #0000cc;">version</span><span style="color: #333333;">>1.</span><span style="color: #0000dd;"><b>8</b></span><span style="color: #333333;"></</span>java<span style="color: #333333;">.</span><span style="color: #0000cc;">version</span><span style="color: #333333;">></span>
<span style="color: #333333;"></</span>properties<span style="color: #333333;">></span></pre></div><div><br /></div><span style="font-family: arial;">If project is general java project, then need to update pom.xml file as follows,</span><div><span style="font-family: arial;"><br /></span>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #333333;"><</span>properties<span style="color: #333333;">></span>
<span style="color: #333333;"><</span>maven<span style="color: #333333;">.</span><span style="color: #0000cc;">compiler</span><span style="color: #333333;">.</span><span style="color: #0000cc;">source</span><span style="color: #333333;">></span><span style="color: #0000dd;"><b>8</b></span><span style="color: #333333;"></</span>maven<span style="color: #333333;">.</span><span style="color: #0000cc;">compiler</span><span style="color: #333333;">.</span><span style="color: #0000cc;">source</span><span style="color: #333333;">></span>
<span style="color: #333333;"><</span>maven<span style="color: #333333;">.</span><span style="color: #0000cc;">compiler</span><span style="color: #333333;">.</span><span style="color: #0000cc;">target</span><span style="color: #333333;">></span><span style="color: #0000dd;"><b>8</b></span><span style="color: #333333;"></</span>maven<span style="color: #333333;">.</span><span style="color: #0000cc;">compiler</span><span style="color: #333333;">.</span><span style="color: #0000cc;">target</span><span style="color: #333333;">></span>
<span style="color: #333333;"></</span>properties<span style="color: #333333;">></span>
</pre></div>
<div><br /></div></div><div><b>2) <span style="font-family: arial;">Changing maven's java version</span></b></div><div><span style="font-family: arial;"><br /></span></div><div><span style="font-family: arial;"><span> </span><span> </span>If you don't want to change the project java version then change maven java version, install and new jdk(i.e 11 here) and set the environment variables i.e JAVA_HOME should point to the new jdk path.</span></div><div><span style="font-family: arial;"><b><br /></b></span></div><div><span style="font-family: arial;"><b>3) Changing runtime JRE in IDE(Eclipse, Intellij)</b></span></div><div><span style="font-family: arial;"><br /></span></div><div><span style="font-family: arial;"><span> </span>We can fix this issue in developer tool/ide like eclipse or intellij if you are using these tools to build,</span></div><div><br /></div><div><span style="font-family: arial;">Select Project -> Go to run configurations -> maven build then select or click on JRE</span></div><div><span style="font-family: arial;"><br /></span></div><div><table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto;"><tbody><tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhN29-QTxZ6CY3LFyEmlF_lpfERKr71rnpGOSkhH-K1GkxGh0SQAc1nCjJCPwkF_C0iBpqyEvzyMSqcRSU5DvrKX-6YeUqzxCoqrxohTU7WFLa03xjfqkjM2UqRgbVOnlrb8bYf_e5hxjX3SkA29L2XsPmztqtdZ3-WsPLC3Xr4-oNlsA0L1CBXrtyAJaA/s833/targetReleaseFixinIDE.JPG" style="margin-left: auto; margin-right: auto;"><img alt="Invalid target maven build issue fix in IDE" border="0" data-original-height="631" data-original-width="833" height="485" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhN29-QTxZ6CY3LFyEmlF_lpfERKr71rnpGOSkhH-K1GkxGh0SQAc1nCjJCPwkF_C0iBpqyEvzyMSqcRSU5DvrKX-6YeUqzxCoqrxohTU7WFLa03xjfqkjM2UqRgbVOnlrb8bYf_e5hxjX3SkA29L2XsPmztqtdZ3-WsPLC3Xr4-oNlsA0L1CBXrtyAJaA/w640-h485/targetReleaseFixinIDE.JPG" title="Invalid target maven build issue fix in IDE" width="640" /></a></td></tr><tr><td class="tr-caption" style="text-align: center;">Invalid target relase issue fix in IDE</td></tr></tbody></table></div><div><span style="font-family: arial;"><br /></span></div><div><span style="font-family: arial;">the default or selected JRE should be greater than or equal to the version of the project java version, in the mentioned issue here need to select jre version as 11.</span></div><div><span style="font-family: arial;"><br /></span></div><div><span style="font-family: arial;"><br /></span></div><div><span style="font-family: arial;">Thank you for reading the blog.</span></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.5945627-15.338635136178846 42.4383127 41.281832536178847 112.7508127tag:blogger.com,1999:blog-205030209684246887.post-91624959999697734322022-10-29T21:58:00.001+05:302022-10-29T21:59:55.370+05:30How to solve springboot "Cannot determine embedded database driver class for database type NONE" Exception<p><span style="font-family: inherit;"> <span style="font-size: medium;"> I posted a last post a year ago, sorry to the users to have disconnectivity of the blog posts due to some reasons. </span><span style="font-size: medium;">Today we will discuss about one of the issue will face during start of spring boot application without configuring any database things.</span></span></p><p> <span style="font-size: medium;"> <span style="font-family: inherit;"> If you are the Spring boot beginer and trying to run the application without configuring the datasource in your application then you can get below exception,</span></span></p><p><span style="background-color: #f8f8f8; color: #333333; font-family: monospace; font-size: 14.08px; white-space: pre;">***************************
APPLICATION FAILED TO START
***************************
Description:
Cannot determine embedded database driver </span><span class="hljs-class" style="box-sizing: inherit; color: #333333; font-family: monospace; font-size: 14.08px; white-space: pre;"><span class="hljs-keyword" style="box-sizing: inherit; font-weight: 700;">class</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">for</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">database</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">type</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">NONE</span>
<span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">Action</span>:
<span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">If</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">you</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">want</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">an</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">embedded</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">database</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">please</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">put</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">a</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">supported</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">one</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">on</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">the</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">classpath</span>.</span></p><p><span class="hljs-class" style="box-sizing: inherit; color: #333333; font-family: monospace; font-size: 14.08px; white-space: pre;"> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">If</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">you</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">have</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">database</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">settings</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">to</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">be</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">loaded</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">from</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">a</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">particular</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">profile</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">you</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">may</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">need</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">to</span> </span></p><p><span class="hljs-class" style="box-sizing: inherit; color: #333333; font-family: monospace; font-size: 14.08px; white-space: pre;"><span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">active</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">it</span> (<span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">no</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">profiles</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">are</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">currently</span> <span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">active</span>).
- [<span class="hljs-title" style="box-sizing: inherit; color: #445588; font-weight: 700;">report</span>:42] - [] - [] </span></p><p><span style="font-family: inherit; font-size: medium;">The above issue we can solve in two ways as follows,</span></p><p><b><span style="font-family: inherit; font-size: large;">1) Fixing with application.properties file</span></b></p><p><span style="font-size: medium;">you can add the fallowing line in application.properties file,</span></p><pre style="background: rgb(255, 255, 255);">spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration</pre><p><span style="font-size: medium;"><br /></span></p><p><span style="font-family: inherit; font-size: large;"><b>2) Fixing with Configuration code</b></span></p><p><span style="font-family: inherit; font-size: medium;">You can add EnableAutoConfiguration annotation with DataSourceAutoConfiguration exclude like below,</span></p><pre style="background: rgb(255, 255, 255);"><span style="color: #7f9fbf; font-weight: bold;">@SpringBootApplication</span>
<span style="color: #7f9fbf; font-weight: bold;">@EnableAutoConfiguration (exclude = { DataSourceAutoConfiguration.class })</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: maroon; font-weight: bold;">class</span> SpringbootApplication <span style="color: purple;">{</span>
SpringApplication<span style="color: #808030;">.</span>run<span style="color: #808030;">(</span>SpringbootApplication<span style="color: #808030;">.</span>class<span style="color: #808030;">,</span> args<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span></pre><p><span style="font-family: inherit; font-size: medium;">or you can also add excludes to the SpringBootApplication annotation itself,</span></p><pre style="background: rgb(255, 255, 255);"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial;"><span style="color: #7f9fbf; font-weight: bold;">@SpringBootApplication (exclude = { DataSourceAutoConfiguration.class })</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: maroon; font-weight: bold;">class</span> SpringbootApplication <span style="color: purple;">{</span>
SpringApplication<span style="color: #808030;">.</span>run<span style="color: #808030;">(</span>SpringbootApplication<span style="color: #808030;">.</span>class<span style="color: #808030;">,</span> args<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span></pre><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial;"><span style="color: purple;"><br /></span></pre><div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; text-align: left;"><span style="color: #0c343d; font-family: inherit; font-size: medium;">Thank you for visiting blog.</span></div></pre>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com4tag:blogger.com,1999:blog-205030209684246887.post-66186542685970815992021-10-16T21:20:00.002+05:302023-09-05T21:29:37.947+05:30Spring Stereotype Annotations<div style="text-align: left;"> <span style="font-size: medium;"> In this post, we can learn what are the Stereotype annotations in Spring and also difference between @Component and @Bean annotations.</span></div><div style="text-align: left;"><span style="font-size: medium;"><br /></span></div><div style="text-align: left;"><span style="font-size: medium;">In Previous one of the post we discussed all <a href="http://adnjavainterview.blogspot.com/2017/11/spring-annotations.html" target="_blank">Spring Annotations</a>, so stereotype annotations also covered but we need to see which are those Spring annotations are stereotype annotations.</span></div><div style="text-align: left;"><span style="font-size: medium;"><br /></span></div><div style="text-align: left;"><span style="font-size: medium;"> The Stereotype annotations are special type of annotations and are used to create Spring beans automatically in the application context. The Stereotype annotations are @Component, @Controller, @Service and @Repository. @Component annotation is a parent or we can say generic stereotype. The other three annotations are derived from Component annotation.</span></div><div style="text-align: left;"><span style="font-size: medium;"><br /></span></div><div style="text-align: left;"><ul style="text-align: left;"><li><span style="font-size: medium;"><b>@Component :- </b>Used at class level that makes the class a component.</span></li></ul></div><div style="text-align: left;"><span> </span><span> </span><span> <span> <span> </span></span></span><span style="color: #aa22ff;">@Component</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"employee"</span><span style="color: #666666;">)</span></div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-size: medium;"><span style="color: green; font-weight: bold;"><span> </span><span> </span>class</span> <span style="color: blue; font-weight: bold;">Employee</span><span style="color: #666666;">{</span>
<span> </span><span> </span><span style="color: green; font-weight: bold;">private</span> String name<span style="color: #666666;">;</span>
<span> </span><span> </span><span style="color: #408080; font-style: italic;">// other fields </span>
<span style="color: #666666;"><span> </span><span> </span>}</span></span></pre><div style="text-align: left;"><span style="font-size: large;"><b><br /></b></span></div><div style="text-align: left;"><ul style="text-align: left;"><li><span style="font-size: large;"><b>@Controller</b>:- U</span><span style="font-size: medium;">sed to create Spring beans at the controller layer.</span></li></ul><div> <span> </span><span style="color: #aa22ff;">@Controller</span></div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-size: medium;"><span style="color: #aa22ff;"><span> <span> </span></span>@RequestMapping</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"employee"</span><span style="color: #666666;">)</span>
<span style="color: green; font-weight: bold;"><span> </span><span> </span>public</span> <span style="color: green; font-weight: bold;">class</span> <span style="color: blue; font-weight: bold;">EmployeeController</span> <span style="color: #666666;">{</span>
<span style="color: #666666;"><span> </span><span> </span>}</span></span></pre></div><div style="text-align: left;"><div style="font-size: large;"><ul style="text-align: left;"><li><b>@Service</b>:- Used to create Spring beans at the Service layer.</li></ul></div><div> <span> </span><span style="color: #aa22ff;">@Service</span></div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-size: medium;"><span style="color: green; font-weight: bold;"><span> </span><span> </span>public</span> <span style="color: green; font-weight: bold;">class</span> <span style="color: blue; font-weight: bold;">EmployeeService</span><span style="color: #666666;">{</span>
<span style="color: #666666;"><span> </span><span> </span>}</span></span></pre><div style="font-size: large;"><ul style="text-align: left;"><li><b>@Repository</b>:- Used to create Spring beans for the repositories at the DAO layer.</li></ul></div><div> <span> <span> </span></span><span style="color: #aa22ff;">@Repository</span></div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-size: medium;"><span style="color: green; font-weight: bold;"><span> </span><span> </span>public</span> <span style="color: green; font-weight: bold;">class</span> <span style="color: blue; font-weight: bold;">EmployeeRepository</span><span style="color: #666666;">{</span>
<span style="color: #666666;"><span> </span><span> </span>}</span></span></pre></div><div style="text-align: left;"><span style="font-size: medium;"><br /></span></div><div style="text-align: left;"><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhF9_HLrF5i1O2GyjOhOMFh6zPwhLB6LVfVZ2WGswDUdkFHScwi6KVPdEAgPBKdZ-JTXIXac6BxeJJRdtSGUh-KTt95sDOoPkcJBRb_HL8ujAucVD6drBiW_ipvUjeCh-MBAJTO_Sbc0Lk/s601/StereotypeAnnotations.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Stereotype Annotations" border="0" data-original-height="272" data-original-width="601" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhF9_HLrF5i1O2GyjOhOMFh6zPwhLB6LVfVZ2WGswDUdkFHScwi6KVPdEAgPBKdZ-JTXIXac6BxeJJRdtSGUh-KTt95sDOoPkcJBRb_HL8ujAucVD6drBiW_ipvUjeCh-MBAJTO_Sbc0Lk/s16000/StereotypeAnnotations.png" title="Stereotype Annotations" /></a></div><span style="font-size: medium;"><br /></span></div><div style="text-align: left;"><b><span style="font-size: large;"><br /></span></b></div><div style="text-align: left;"><b><span style="font-size: large;">Difference between Component and Bean annotation:-</span></b></div><div style="text-align: left;"><b><span style="font-size: large;"><br /></span></b></div><div style="text-align: left;"><b><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWrf-JJ56i-XEnojXEaZrSs-_IvMZuuIjHJhOjpQ9tuzJRyxjkSW36F9-OSMorgl1zE41WnQBizGwG9vQTT5ChR4lPgOPDExIeHELBjGAEaCQAKvbRxwT61N78p4gUJ1XwKdgHCdBAkGQ/s624/beanComponent.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Difference between bean and component annotation" border="0" data-original-height="321" data-original-width="624" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhWrf-JJ56i-XEnojXEaZrSs-_IvMZuuIjHJhOjpQ9tuzJRyxjkSW36F9-OSMorgl1zE41WnQBizGwG9vQTT5ChR4lPgOPDExIeHELBjGAEaCQAKvbRxwT61N78p4gUJ1XwKdgHCdBAkGQ/s16000/beanComponent.png" title="Difference between bean and component annotation" /></a></div><div class="separator" style="clear: both; text-align: center;"><br /></div><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: left;"><span style="font-size: large;">Related Posts:-</span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-size: medium;">1) <a href="https://adnjavainterview.blogspot.com/2017/11/spring-annotations.html" target="_blank">Spring Annotations</a></span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-size: medium;">2) <a href="https://adnjavainterview.blogspot.com/2018/12/the-springbootapplication-annotation.html" target="_blank">The @SpringBootApplication Annotation usage and example</a></span></div></b></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com7Bengaluru, Karnataka, India12.9715987 77.5945627-15.338635136178846 42.4383127 41.281832536178847 112.7508127tag:blogger.com,1999:blog-205030209684246887.post-61631980423607774832021-07-09T22:02:00.003+05:302021-07-10T06:30:42.989+05:30SOLID Design Principles with Examples<div><span style="font-family: arial; font-size: medium;"> </span><span style="font-family: inherit;"><span style="font-size: medium;"> </span><span style="font-size: medium;">In this post, we w'll learn the SOLID Principles with example for each principle and also to learn how to use these principles in our daily basis coding.</span></span></div><div><span style="font-family: inherit; font-size: medium;"> </span></div><div><span style="font-family: inherit; font-size: medium;"> SOLID Principles is a coding standard that all developers should follow these principles while developing any software. It was promoted by Robert C Martin and is used across the object-oriented design spectrum. When you apply SOLID principles properly in your software, it makes your code more extendable, logical, and easier to read.</span></div><div><span style="font-family: inherit; font-size: medium;"> </span></div><div><span style="font-family: inherit; font-size: medium;"> When the developer builds software following a bad design, the code can become inflexible and more brittle. Small changes in the software can result in bugs. For these reasons, we should follow SOLID Principles.</span></div><div><span style="font-family: inherit; font-size: medium;"><br /></span></div><div><span style="font-family: inherit; font-size: medium;"> It takes some time to understand, but when you follow these principles, it will improve the code quality and help you to understand the most well designed software.</span></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQrupyNq_KRquN232qT5DIslzcTDJyHv1_K-9XjfblWbcyT8oy624I30Eyw2wieIczF1dasSXr9K0PhYhb8U_Nvu8Rxn2Ckx6COwZdsYe0fKw34Ya7qJnO_HgaoJNwJdsoo9x8gDeps84/s588/SolidPrinciple.png" style="margin-left: 1em; margin-right: 1em;"><img alt="Solid Design Principles in java" border="0" data-original-height="498" data-original-width="588" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQrupyNq_KRquN232qT5DIslzcTDJyHv1_K-9XjfblWbcyT8oy624I30Eyw2wieIczF1dasSXr9K0PhYhb8U_Nvu8Rxn2Ckx6COwZdsYe0fKw34Ya7qJnO_HgaoJNwJdsoo9x8gDeps84/s16000/SolidPrinciple.png" title="Solid Design Principles" /></a></div><br /><div><br /></div><div><span style="font-family: inherit; font-size: medium;">Let us discuss one by one in detail,</span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: inherit; font-size: large;"><b>1) Single Responsibility Principle</b></span></div><div><span style="font-family: inherit; font-size: large;"><b><br /></b></span></div><div><span style="font-size: medium;"> The Single Responsibility Principle(SRP) says that a class should have only one reason to change, which means every class should have a single responsibility or single job or single purpose.</span></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-size: medium;"> If single class handles multiple functionalities then in future if we do any modification then it will impact whole class not only the enhanced functionality so need to avoid this.</span></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-size: medium;"> The below example - Customer has the customer details like name, dateOfBirth, age, address, pinCode, phoneNumber and countryCode. As SRP principle states that class should have single responsibility but here if we do any validations on phoneNumber or address we need to do modify the complete Customer class and also it has different responsibility. So it voilates the Single Responsibility Principle.</span></div><div><span style="font-size: medium;"><br /></span></div><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKcsHjleFoIfGFVrGJ4OqViCG_0pHoDjC7z2JDry-NTDhGrpq1O24cwK5sLDveqdUPA9vqB89X1oqunG3bO-HUW8juX4lSf6K-Um1HD9_yrAbDYkJ0jkvStgIE1qEVLv1TcjYv16er4ik/s468/SRPRuleBreakEx.png" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" data-original-height="350" data-original-width="468" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKcsHjleFoIfGFVrGJ4OqViCG_0pHoDjC7z2JDry-NTDhGrpq1O24cwK5sLDveqdUPA9vqB89X1oqunG3bO-HUW8juX4lSf6K-Um1HD9_yrAbDYkJ0jkvStgIE1qEVLv1TcjYv16er4ik/s16000/SRPRuleBreakEx.png" /></a></div><br /><span style="font-size: large;"><br /></span></div><div><span style="font-size: large;"> </span></div><div><br /></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: arial; font-size: medium;"><br /></span></div><div><span style="font-family: inherit; font-size: medium;">The above class voilates the Single Responsibility Principle as we have address and phone detail fields are present in the same class.</span></div><div><span style="font-family: inherit; font-size: medium;"><br /></span></div><div><span style="font-size: medium;"> So those fields need to seperate into different classes and have to call in the customer class as follow.</span></div><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5qHjT1J7CH7nQ65j_RP0kmFSqV42bFczFVxgN-GWlGxTPJQTfCGLpYsvX_L9QEJdKjQ2VCUAcpPW6VmJWgaJoRI2IJFaP7Kt995VP7mdCaMHkNEKzFuxY4T7epWtsQ7uuOrrUwcekimo/s412/addressClass.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="412" data-original-width="369" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg5qHjT1J7CH7nQ65j_RP0kmFSqV42bFczFVxgN-GWlGxTPJQTfCGLpYsvX_L9QEJdKjQ2VCUAcpPW6VmJWgaJoRI2IJFaP7Kt995VP7mdCaMHkNEKzFuxY4T7epWtsQ7uuOrrUwcekimo/s16000/addressClass.png" /></a></div><br /><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVbu8NkZIhZ2blJHFcDfL5zJ3_CtzbVieVOFijSz55hzMf1RDq3DuKtz2dnU0AXR-FATAQr5ycc260WEB9XwV7E7vgZ1cUr7ERh0ZgIvfXa6g2m5ZJsbZPoPh75kUKUS4RM2_AwnIg6kE/s429/phone.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="396" data-original-width="429" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVbu8NkZIhZ2blJHFcDfL5zJ3_CtzbVieVOFijSz55hzMf1RDq3DuKtz2dnU0AXR-FATAQr5ycc260WEB9XwV7E7vgZ1cUr7ERh0ZgIvfXa6g2m5ZJsbZPoPh75kUKUS4RM2_AwnIg6kE/s16000/phone.png" /></a></div><span style="font-family: inherit; font-size: medium;"> The updated customer class is below,</span></div><div><span style="font-family: arial;"><br /></span></div><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_BRYW2Yk-uIeYgbLaog-GAgbWDblL25jSHpocDkfnWzFJX-fZandpau2vLTeq8_PnlKYm9LCSAZfRmC97kbIEsp2yKe-EC1W-LzG3z-xYXqGEL9Ir51uf9zLEUyUgMpquI73k0SLoG0g/s340/SRPPrincipleSampleEx.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="307" data-original-width="340" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_BRYW2Yk-uIeYgbLaog-GAgbWDblL25jSHpocDkfnWzFJX-fZandpau2vLTeq8_PnlKYm9LCSAZfRmC97kbIEsp2yKe-EC1W-LzG3z-xYXqGEL9Ir51uf9zLEUyUgMpquI73k0SLoG0g/s16000/SRPPrincipleSampleEx.png" /></a></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: inherit; font-size: medium;"> If any enhancement comes in the future like do some validation on phoneNumber or address then we do modification on Phone or Address class so we can not do any modification on Customer class so this is what the SRP principle says(every class has single responsibility).</span></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: inherit; font-size: medium;"><br /></span></div><br /><span style="font-family: inherit; font-size: large;"><b>2) Open/Closed Principle</b></span></div><div><span style="font-family: inherit; font-size: large;"><b><br /></b></span></div><div><span style="font-family: inherit; font-size: medium;"><b> </b><span style="font-family: inherit;">The open/closed principle(OCP) states that a module or entity should be open for extension but closed for modification. It is one of the famous solid principles and very important in object oriented design principle.</span></span></div><div><span style="font-family: inherit; font-size: medium;"><span style="font-family: inherit;"><br /></span></span></div><div><span><span><div style="font-size: large;"> <i> <span style="color: #2b00fe;">Bertrand Meyer says that "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”.</span></i></div><div style="font-size: large;"><i><br /></i></div><div style="font-size: large;"><b>Open for extension</b> means we need to design the software modules/classes in such a way that the new responsibilities or functionalities should be added easily when new requirements come.</div><div style="font-size: large;">On other hand, <b>Closed for modification</b> means we should not modify the class/module until we find some bugs.</div><div style="font-size: large;"><br /></div><div style="font-size: large;"><b>Example of voilation of Open/Closed Principle:--</b></div><div style="font-size: large;"><br /></div><div style="font-size: large;"> In the below Payment class, there is paymentType parameter, based on this we have payment processing logic for different payment types. But in future if any enhancement comes for new payment type then we need to modify the Payment class so this voilates OCP principle.</div><div style="font-size: large;"><br /></div><div><div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: inherit; font-size: large;"><span style="color: green;">public</span> <span style="color: green;">class</span> <span style="color: blue;">Payment</span> <span style="color: #666666;">{</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">paymentProcessing</span><span style="color: #666666;">(</span><span style="color: #b00040;">int</span> amount<span style="color: #666666;">,</span> String paymentMode<span style="color: #666666;">)</span> <span style="color: #666666;">{</span>
<span style="color: green;">if</span> <span style="color: #666666;">(</span>paymentMode<span style="color: #666666;">.</span><span style="color: #7d9029;">equals</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"Cash"</span><span style="color: #666666;">))</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//payment process for cash</span>
<span style="color: #666666;">}</span> <span style="color: green;">else</span> <span style="color: green;">if</span> <span style="color: #666666;">(</span>paymentMode<span style="color: #666666;">.</span><span style="color: #7d9029;">equals</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"CreditCard"</span><span style="color: #666666;">))</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//payment process for creditcard</span>
<span style="color: #666666;">}</span> <span style="color: green;">else</span> <span style="color: green;">if</span> <span style="color: #666666;">(</span>paymentMode<span style="color: #666666;">.</span><span style="color: #7d9029;">equals</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"debitCard"</span><span style="color: #666666;">))</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//payment process for debitCard</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span></span></pre></div></div><div style="font-size: large;"><span style="font-size: medium;"><span><br /></span></span></div><b style="font-size: large;">Example to followed the Open/Closed Principle:-</b></span></span></div><div><span style="font-size: medium;"><span><br /></span></span></div><div><span style="font-size: medium;"><span>In the above example voilates OCP principle, need to follow OCP Principle as follows,</span></span></div><div><span style="font-size: medium;"><span><br /></span></span></div><div><span><span style="font-family: inherit; font-size: large;"><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: green;">public</span> <span style="color: green;">interface</span> <span style="color: blue;">PaymentType</span> <span style="color: #666666;">{</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">processPayment</span><span style="color: #666666;">();</span>
<span style="color: #666666;">}</span>
<span style="color: green;">class</span> <span style="color: blue;">Cash</span> <span style="color: green;">implements</span> PaymentType <span style="color: #666666;">{</span>
<span style="color: #aa22ff;">@Override</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">processPayment</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//add cash processing logic</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span>
<span style="color: green;">class</span> <span style="color: blue;">DebitCard</span> <span style="color: green;">implements</span> PaymentType <span style="color: #666666;">{</span>
<span style="color: #aa22ff;">@Override</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">processPayment</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//add debitcard processing logic</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span></pre></span></span></div><div><span style="font-size: medium;"><span><br /></span></span></div><div><span style="font-size: medium;"><span> In the above example, created interface PaymentType and implemented classes are Cash and DebitCard so here if in future any enhancement for new paymentType then no need to modify the existing classes/interface instead create new implemented class and implement the required payment process logic.</span></span></div><div><span><span><br /><div style="font-size: large;"><br /></div><div><span style="font-family: inherit; font-size: large;"><b>3) Liskov Substitution Principle(LSP)</b></span></div><div><span style="font-family: inherit; font-size: large;"><b><br /></b></span></div><div><span style="font-size: medium;"> Liskov Substitution Principle(LSP) is a third SOLID design principle and this defined by <b>Barbara Liskov</b>. It extends the Open/Closed principle and enables you to replace objects of a parent class with objects of a subclass without breaking the application. This requires all subclasses to behave in the same way as the parent class.</span></div><div><span style="font-size: medium;"><br /></span></div><div><div><span style="font-size: medium;">The Principle states that, <i style="font-weight: bold;">objects of a superclass shall be replaceable with objects of its subclass without breaking the application.</i></span></div><div><span style="font-size: medium;"><b><i>That requires the objects of your subclasses to behave in the same way as the objects of your superclasses.</i></b></span></div></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-size: medium;"><i><b>Example</b></i></span>:-</div><div><br /></div><div><span style="font-size: medium;"> In the below example, the Square is derived class extends Rectangle class, where we can set the height and width.</span></div><div><span style="font-size: medium;"><br /></span></div><div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-size: large;"><span style="color: green;">public</span> <span style="color: green;">class</span> <span style="color: blue;">Rectangle</span> <span style="color: #666666;">{</span>
<span style="color: green;">private</span> <span style="color: #b00040;">double</span> height<span style="color: #666666;">;</span>
<span style="color: green;">private</span> <span style="color: #b00040;">double</span> width<span style="color: #666666;">;</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">setHeight</span><span style="color: #666666;">(</span><span style="color: #b00040;">double</span> h<span style="color: #666666;">)</span> <span style="color: #666666;">{</span> height <span style="color: #666666;">=</span> h<span style="color: #666666;">;</span> <span style="color: #666666;">}</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">setWidht</span><span style="color: #666666;">(</span><span style="color: #b00040;">double</span> w<span style="color: #666666;">)</span> <span style="color: #666666;">{</span> width <span style="color: #666666;">=</span> w<span style="color: #666666;">;</span> <span style="color: #666666;">}</span>
<span style="color: #666666;">...</span>
<span style="color: #666666;">}</span>
<span style="color: green;">public</span> <span style="color: green;">class</span> <span style="color: blue;">Square</span> <span style="color: green;">extends</span> Rectangle <span style="color: #666666;">{</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">setHeight</span><span style="color: #666666;">(</span><span style="color: #b00040;">double</span> h<span style="color: #666666;">)</span> <span style="color: #666666;">{</span>
<span style="color: green;">super</span><span style="color: #666666;">.</span><span style="color: #7d9029;">setHeight</span><span style="color: #666666;">(</span>h<span style="color: #666666;">);</span>
<span style="color: green;">super</span><span style="color: #666666;">.</span><span style="color: #7d9029;">setWidth</span><span style="color: #666666;">(</span>w<span style="color: #666666;">);</span>
<span style="color: #666666;">}</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">setWidth</span><span style="color: #666666;">(</span><span style="color: #b00040;">double</span> h<span style="color: #666666;">)</span> <span style="color: #666666;">{</span>
<span style="color: green;">super</span><span style="color: #666666;">.</span><span style="color: #7d9029;">setHeight</span><span style="color: #666666;">(</span>h<span style="color: #666666;">);</span>
<span style="color: green;">super</span><span style="color: #666666;">.</span><span style="color: #7d9029;">setWidth</span><span style="color: #666666;">(</span>w<span style="color: #666666;">);</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span></span></pre></div><div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-size: medium;"> The above code not follow's LSP because you cannot replace the Rectangle base class with its derived class Square. The Square class has extra constraints, i.e., the height and width must be the same. Therefore, substituting Rectangle with Square class may result in unexpected behavior.</span></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-size: medium;"> This principle is just an extension of the Open Close Principle and it means that we must make sure that new derived classes are extending the base classes without changing their behavior.</span></div><div><br /></div></div><div style="font-size: large;"><br /></div><div><span style="font-family: inherit; font-size: large;"><b>4) Interface Segregation Principle</b></span></div><div><span style="font-family: inherit; font-size: large;"><b><br /></b></span></div><div><div><span style="font-size: medium;">According to Robert Martin,</span></div><div><span style="font-size: medium;"><b><br /></b></span></div><div><span style="font-size: medium;"><b> </b>The Interface Segregation Interface Principle states that,<b> Clients should not be forced to depend upon interfaces that they do not use.</b></span></div><div><span style="font-size: medium;"><b><br /></b></span></div><div><span style="font-size: medium;"><div> The main aim of this principle is to break the larger application interfaces into smaller one and each interface serves the single purpose/responsibility, it's similar to single responsiblity principle. In Clients code I mean implemented classes, if any unimplemented methods we need to override those methods also, those are really not needed so this voilates Interface Segregation Principle.</div><div><br /></div><div>Precise application design and correct abstraction is the key behind the Interface Segregation Principle. Though it'll take more time and effort in the design phase of an application and might increase the code complexity, in the end, we get a flexible code.</div></span></div></div><div style="font-size: large;"><br /></div><div><span style="font-family: inherit; font-size: medium;">Let us see below example,</span></div><div><span style="font-family: inherit; font-size: medium;"><br /></span></div><div><span style="font-family: inherit; font-size: medium;">The HotelOrder has an interface and there are four abstract methods, onlineOrder() , walkInCustomerOrder(), onlinePayment() and cashPayment(). </span></div><div><span style="font-family: inherit; font-size: medium;"><br /></span></div></span></span></div><div><span style="font-family: inherit; font-size: large;"><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: green;">public</span> <span style="color: green;">interface</span> <span style="color: blue;">HotelOrder</span> <span style="color: #666666;">{</span>
<span style="color: green;"><span> </span>public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">onlineOrder</span><span style="color: #666666;">();</span>
<span style="color: green;"><span> </span>public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">walkInCustomerOrder</span><span style="color: #666666;">();</span>
<span style="color: green;"><span> </span>public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">onlinePayment</span><span style="color: #666666;">();</span>
<span style="color: green;"><span> </span>public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">cashPayment</span><span style="color: #666666;">();</span>
<span style="color: #666666;">}</span></pre></span><span style="font-size: medium;"><br /></span></div><div><span style="font-size: large;"> </span><span style="font-family: inherit; font-size: medium;">There are two implemented classes for HotelOrder interface i.e WalkInCustomerOrder and OnlineCustomerOrder. For WalkInCustomerOrder implementation of onlineOrder() method is not needed but HotelOrder is an interface and we need to implement all abstract methods even those methods do not use. Samething for OnlineCustomerOrder, for online customers implementation class, don't need to implement the abstract method(walkInCustomerOrder()). </span></div><div><span style="font-family: inherit; font-size: medium;"><br /></span></div><div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: inherit; font-size: large;"><span style="color: green;">public</span> <span style="color: green;">class</span> <span style="color: blue;">OnlineCustomerOrder</span> <span style="color: green;">implements</span> HotelOrder <span style="color: #666666;">{</span>
<span style="color: #aa22ff;">@Override</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">onlineOrder</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">// code logic for online order</span>
<span style="color: #666666;">}</span>
<span style="color: #aa22ff;">@Override</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">walkInCustomerOrder</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//this method implementation is not needed for </span>
<span style="color: #408080; font-style: italic;">//online customers</span>
<span style="color: green;">throw</span> <span style="color: green;">new</span> <span style="color: blue;">UnsupportedOperationException</span><span style="color: #666666;">();</span>
<span style="color: #666666;">}</span>
<span style="color: #aa22ff;">@Override</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">onlinePayment</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//write a code for online payment</span>
<span style="color: #408080; font-style: italic;">//like calling payment gateway(payment stuffs)</span>
<span style="color: #666666;">}</span>
<span style="color: #aa22ff;">@Override</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">cashPayment</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//this method is not needed for online order customers</span>
<span style="color: #408080; font-style: italic;">//just throw an exception</span>
<span style="color: green;">throw</span> <span style="color: green;">new</span> <span style="color: blue;">UnsupportedOperationException</span><span style="color: #666666;">();</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span>
<span style="color: green;">public</span> <span style="color: green;">class</span> <span style="color: blue;">WalkInCustomerOrder</span> <span style="color: green;">implements</span> HotelOrder<span style="color: #666666;">{</span>
<span style="color: #aa22ff;">@Override</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">onlineOrder</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//this method implementation is not needed</span>
<span style="color: green;">throw</span> <span style="color: green;">new</span> <span style="color: blue;">UnsupportedOperationException</span><span style="color: #666666;">();</span>
<span style="color: #666666;">}</span>
<span style="color: #aa22ff;">@Override</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">walkInCustomerOrder</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//logic for walkIn Customer order</span>
<span style="color: #666666;">}</span>
<span style="color: #aa22ff;">@Override</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">onlinePayment</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//this method implementation is not needed</span>
<span style="color: green;">throw</span> <span style="color: green;">new</span> <span style="color: blue;">UnsupportedOperationException</span><span style="color: #666666;">();</span>
<span style="color: #666666;">}</span>
<span style="color: #aa22ff;">@Override</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">cashPayment</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//logic for cash payments</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span></span></pre></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-family: inherit; font-size: medium;"> <span style="font-family: inherit;"> The above code violates Interface Segregation Principle, as principle says client should not override methods those they do not use.</span></span></div><div><span style="font-family: inherit; font-size: medium;"><span style="font-family: inherit;"><br /></span></span></div><div><span style="font-family: inherit; font-size: medium;"><b>Code to follow Interface Segregation Principle:-</b></span></div><div><span style="font-family: inherit; font-size: medium;"><b><br /></b></span></div><div><span style="font-size: medium;">Create two interfaces one with WalkIn customer and second with Online Customer, In walkin customer interface define the walkin customer related methods like walkInCustomerOrder() and cashPayment() and for online customer interface define the onlinePayment() and onlineOrder() methods so that it follows the principle and no need to override unnecessary methods in the implemented classes.</span></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-family: inherit; font-size: large;"><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: green;">public</span> <span style="color: green;">interface</span> <span style="color: blue;">OnlineCustomerHotelOrder</span> <span style="color: #666666;">{</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">onlineOrder</span><span style="color: #666666;">();</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">onlinePayment</span><span style="color: #666666;">();</span>
<span style="color: #666666;">}</span></pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: #666666;"><br /></span></pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><pre style="line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: inherit;"><span style="color: green;">public</span> <span style="color: green;">interface</span> <span style="color: blue;">WalkInCustomerHotelOrder</span> <span style="color: #666666;">{</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">walkInCustomerOrder</span><span style="color: #666666;">();</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">cashPayment</span><span style="color: #666666;">();</span>
<span style="color: #666666;">}</span></span></pre></pre></span></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-family: inherit; font-size: medium;">Implemented classes for each order,</span></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-family: inherit; font-size: large;"><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: green;">public</span> <span style="color: green;">class</span> <span style="color: blue;">OnlineCustomerOrder</span> <span style="color: green;">implements</span> OnlineCustomerHotelOrder <span style="color: #666666;">{</span>
<span style="color: #aa22ff;">@Override</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">onlineOrder</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">// code logic for online order</span>
<span style="color: #666666;">}</span>
<span style="color: #aa22ff;">@Override</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">onlinePayment</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//write a code for online payment</span>
<span style="color: #408080; font-style: italic;">//like calling payment gateway(payment stuffs)</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span>
<span style="color: green;">public</span> <span style="color: green;">class</span> <span style="color: blue;">WalkInCustomerOrder</span> <span style="color: green;">implements</span> WalkInCustomerHotelOrder<span style="color: #666666;">{</span>
<span style="color: #aa22ff;">@Override</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">walkInCustomerOrder</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//logic for walkIn Customer order</span>
<span style="color: #666666;">}</span>
<span style="color: #aa22ff;">@Override</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">cashPayment</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//logic for cash payments</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span></pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: #666666;"><br /></span></pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><pre style="line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><div style="color: black; font-family: "Times New Roman"; font-size: medium; white-space: normal;"><span style="font-family: inherit; font-size: large;"></span></div><div><span style="font-family: inherit; font-size: large;"><b><br /></b></span></div></pre></pre></span>
<b><span style="font-family: inherit; font-size: large;">5) Dependency Inversion Principle</span></b>
</div><div><b><span style="font-family: inherit; font-size: large;"><br /></span></b></div><div><span style="font-family: inherit; font-size: medium;">The Dependency Inversion Principle is also one of Solid Principle and it concers the relationship between modules and classes.</span></div><div><br /></div><div><span style="font-family: inherit; font-size: medium;">According to Robert Martin,</span></div><div><span style="font-family: inherit; font-size: medium;"><br /></span></div><div><span style="font-family: inherit; font-size: medium;">The Dependency Inversion Principle says that,</span></div><div><span style="font-family: inherit; font-size: medium;"><div><ul style="text-align: left;"><b><li>High-level modules should not depend on low-level modules. Both should depend on abstractions.</li><li>Abstractions should not depend on details. Details should depend on abstractions.</li></b></ul></div></span></div><div><span style="font-family: inherit; font-size: medium;">Lets see below example,</span></div><div><span style="font-family: inherit; font-size: medium;"><br /></span></div><div><span style="font-family: inherit; font-size: medium;">The TaxCalculator is main class to calculate the different types of tax like GST, Income Tax, Professional tax and so on. In the below example, I have given two types of taxes GST and Income tax and corresponding implemented classes for each tax types.</span></div><div><span style="font-family: inherit; font-size: medium;"><br /></span></div><div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: inherit; font-size: large;"><span style="color: green;">public</span> <span style="color: green;">class</span> <span style="color: blue;">TaxCalculator</span> <span style="color: #666666;">{</span>
<span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: #b00040;">void</span> <span style="color: blue;">taxCalculator</span><span style="color: #666666;">(</span>String mode<span style="color: #666666;">)</span> <span style="color: #666666;">{</span>
<span style="color: green;">if</span> <span style="color: #666666;">(</span>mode<span style="color: #666666;">.</span><span style="color: #7d9029;">equals</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"GST"</span><span style="color: #666666;">))</span> <span style="color: #666666;">{</span>
GSTTaxCalculator<span style="color: #666666;">.</span><span style="color: #7d9029;">calculate</span><span style="color: #666666;">();</span>
<span style="color: #666666;">}</span> <span style="color: green;">else</span> <span style="color: green;">if</span> <span style="color: #666666;">(</span>mode<span style="color: #666666;">.</span><span style="color: #7d9029;">equals</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"IncomeTax"</span><span style="color: #666666;">))</span> <span style="color: #666666;">{</span>
IncomeTaxCalculator<span style="color: #666666;">.</span><span style="color: #7d9029;">calculate</span><span style="color: #666666;">();</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span>
<span style="color: green;">class</span> <span style="color: blue;">GSTTaxCalculator</span> <span style="color: #666666;">{</span>
<span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: #b00040;">void</span> <span style="color: blue;">calculate</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">// logic to calculate GST tax</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span>
<span style="color: green;">class</span> <span style="color: blue;">IncomeTaxCalculator</span> <span style="color: #666666;">{</span>
<span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: #b00040;">void</span> <span style="color: blue;">calculate</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//logic to calculate IncomeTax</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span></span></pre></div><div><br /></div><div><span style="font-family: inherit; font-size: medium;">In the code, in future if we need to implement any new tax type then need to modify the main class so it voilates the Dependency Inversion Principle. The implemented class depends on main module/class so that also voilates the principle.</span></div><div><br /></div><div><span style="font-family: inherit; font-size: medium;">Need to modify the abstract class not the main class and also implemented class/module depends on abstraction not on main module/class.</span></div><div><span style="font-family: inherit; font-size: medium;"><br /></span></div><div><span style="font-family: inherit; font-size: medium;"><b>Code to follow Dependency Inversion Principle:-</b></span></div><div><span style="font-family: inherit; font-size: medium;"><b><br /></b></span></div><div><span style="font-size: medium;">Create an interface Itax and both IncomeTaxCalculator and GSTTaxCalculator implements Itax interface.</span></div><div><span style="font-size: medium;"><br /></span></div><div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: inherit; font-size: large;"><span style="color: green;">public</span> <span style="color: green;">class</span> <span style="color: blue;">TaxCalculator</span> <span style="color: #666666;">{</span>
<span style="color: green;">public</span> <span style="color: green;">static</span> <span style="color: #b00040;">void</span> <span style="color: blue;">taxCalculator</span><span style="color: #666666;">(</span>Itax itax<span style="color: #666666;">)</span> <span style="color: #666666;">{</span>
itax<span style="color: #666666;">.</span><span style="color: #7d9029;">calculate</span><span style="color: #666666;">();</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span>
<span style="color: green;">interface</span> <span style="color: blue;">Itax</span> <span style="color: #666666;">{</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">calculate</span><span style="color: #666666;">();</span>
<span style="color: #666666;">}</span>
<span style="color: green;">class</span> <span style="color: blue;">GSTTaxCalculator</span> <span style="color: green;">implements</span> Itax<span style="color: #666666;">{</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">calculate</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">// logic to calculate GST tax</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span>
<span style="color: green;">class</span> <span style="color: blue;">IncomeTaxCalculator</span> <span style="color: green;">implements</span> Itax<span style="color: #666666;">{</span>
<span style="color: green;">public</span> <span style="color: #b00040;">void</span> <span style="color: blue;">calculate</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//logic to calculate IncomeTax</span>
<span style="color: #666666;">}</span>
<span style="color: #666666;">}</span></span></pre></div><div><span style="font-family: inherit; font-size: medium;"><b><br /></b></span></div><div><span style="font-family: inherit; font-size: medium;">If any question or clarification or suggestion put comment in the comment section or send an email to me anilnivargi49@gmail.com</span></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-size: medium;">Thank you for visting blog.</span></div><div><br /></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Bengaluru, Karnataka, India12.9715987 77.5945627-15.338635136178846 42.4383127 41.281832536178847 112.7508127tag:blogger.com,1999:blog-205030209684246887.post-17996876659010815302021-05-01T12:29:00.002+05:302021-05-01T12:31:07.240+05:30What is Effectively Final in Java 8<p><span style="font-size: medium;"> In Java 8, introduced one new concept called "effectively final" variable.</span></p><p><span style="font-size: medium;"><b>A variable or parameter whose value is never changed after it is initialized is effectively final. </b></span></p><p><span style="font-size: medium;"> There is no need to explicitly declare such a variable as final, although if you declare final it won't cause any issue. This effectively final variable can be used inside anonymous class or lambda expression and can not change this variable value inside anonymous class or lambda expression.</span></p><p><span style="font-size: medium;">Example of effectively final:-</span></p><p><span style="font-size: medium;"></span></p><pre style="color: #333333; font-size: 16px; line-height: 20px; margin-bottom: 0px; margin-top: 0px;">String str <span style="color: #666666;">=</span> <span style="color: #ba2121;">"Acuver Consulting"</span><span style="color: #666666;">;</span>
List<span style="color: #666666;"><</span>String<span style="color: #666666;">></span> skuList <span style="color: #666666;">=</span> Arrays<span style="color: #666666;">.</span><span style="color: #7d9029;">asList</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"MTCCC"</span><span style="color: #666666;">,</span> <span style="color: #ba2121;">"MTBBB"</span><span style="color: #666666;">);</span>
skuList<span style="color: #666666;">.</span><span style="color: #7d9029;">stream</span><span style="color: #666666;">().</span><span style="color: #7d9029;">forEach</span><span style="color: #666666;">(</span>s <span style="color: #666666;">-></span>
System<span style="color: #666666;">.</span><span style="color: #7d9029;">out</span><span style="color: #666666;">.</span><span style="color: #7d9029;">println</span><span style="color: #666666;">(</span>str<span style="color: #666666;">)</span>
<span style="color: #666666;">);</span></pre><div><br /></div><div><span style="font-size: medium;">In above example, str variable is Effectively Final variable.</span></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-size: medium;">Example of non effective final variable,</span></div><div><span style="font-size: medium;"><br /></span></div><div><span style="font-size: medium;"><pre style="color: #333333; font-size: 16px; line-height: 20px; margin-bottom: 0px; margin-top: 0px;">String str <span style="color: #666666;">=</span> <span style="color: #ba2121;">"Acuver Consulting"</span><span style="color: #666666;">;</span>
str <span style="color: #666666;">=</span> str<span style="color: #666666;">.</span><span style="color: #7d9029;">concat</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"Pvt Ltd"</span><span style="color: #666666;">);</span>
List<span style="color: #666666;"><</span>String<span style="color: #666666;">></span> skuList <span style="color: #666666;">=</span> Arrays<span style="color: #666666;">.</span><span style="color: #7d9029;">asList</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"a"</span><span style="color: #666666;">,</span> <span style="color: #ba2121;">"b"</span><span style="color: #666666;">);</span>
skuList<span style="color: #666666;">.</span><span style="color: #7d9029;">stream</span><span style="color: #666666;">().</span><span style="color: #7d9029;">forEach</span><span style="color: #666666;">(</span>s <span style="color: #666666;">-></span>
System<span style="color: #666666;">.</span><span style="color: #7d9029;">out</span><span style="color: #666666;">.</span><span style="color: #7d9029;">println</span><span style="color: #666666;">(</span>str<span style="color: #666666;">)</span> <span style="color: #408080; font-style: italic;">//Compile Time Error </span>
<span style="color: #666666;">);</span></pre></span></div><div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-size: medium;"><span style="color: #666666;"><br /></span></span></pre></div><div><span style="font-size: medium;"> When we use non effective final variable inside lambda expression or inside anonymous class, then will throw a Compile Time Error as,</span></div><div><div><span style="font-size: medium;"><i>"Local variable str defined in an enclosing scope must be final or effectively final"</i></span></div></div><div><br /></div><div><span style="font-size: medium;"> Difference between final and effective final variable is final variable we can explicitly declare variable with final keyword and can not reassign variable value but effective final variable no need to declare variable with final keyword and value can not be changed once it's initialized. If we change the value i.e mutable variable can not access inside the lambda expression.</span></div><div><br /></div><div><span style="font-family: arial; font-size: medium;"><b><br /></b></span></div><div><span style="font-family: arial; font-size: medium;"><b>Related Posts:-</b></span></div><div><span style="font-family: arial;"><b>1) <a href="https://adnjavainterview.blogspot.com/2018/09/java-8-features-with-examples.html" target="_blank">Java 8 features with examples</a></b></span></div><div><span style="font-family: arial;"><b>2) <a href="https://adnjavainterview.blogspot.com/2019/04/java-8-foreach-method-with-examples.html" target="_blank">Java 8 forEach method with examples</a></b></span></div><div><span style="font-family: arial;"><b>3) <a href="https://adnjavainterview.blogspot.com/2019/05/method-references-in-java-8.html">Method References in Java 8</a></b></span></div><div><span style="font-family: arial;"><b>4) <a href="https://adnjavainterview.blogspot.com/2019/05/java-8-consumer-interface-with-examples.html" target="_blank">Java 8 - Consumer Interface with examples</a></b></span></div><div><span style="font-family: arial;"><b>5) <a href="https://adnjavainterview.blogspot.com/2019/05/java-8-biconsumer-bifunction-bipredicate-interface.html" target="_blank">Java 8 - BiConsumer, BiFunction and BiPredicate Interface Example</a></b></span></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com1tag:blogger.com,1999:blog-205030209684246887.post-25494601599903611132021-04-13T21:06:00.030+05:302023-09-10T15:57:07.241+05:30Java Collection Framework Best Practices<p><span style="font-size: medium;"> <span style="font-family: inherit;"> </span><span style="font-family: arial;"> In this article we can discuss the best practices of java collection and sample examples for each usages.</span></span></p><p><span style="font-size: large;"><b>1) Choose the right collection</b></span></p><p><span style="font-size: medium;"> <span style="font-family: arial;"> This is the most important step before using any collection. Depending upon functionality or problem solve to decide the right collection to use in the code implementation. There are multiple criteria to choose right collection - </span></span></p><p><span style="font-family: arial; font-size: medium;">1) collection allows duplicates or not </span></p><p><span style="font-family: arial; font-size: medium;">2) accepts null or not </span></p><p><span style="font-family: arial; font-size: medium;">3) search based on index</span></p><p><span style="font-family: arial; font-size: medium;">4) Supports concurrency.</span></p><p><span style="font-family: arial; font-size: medium;">and also the developer should know the performance impact on each collection usages.</span></p><p><span style="font-size: medium;"><br /></span></p><div style="text-align: left;"><span style="font-family: inherit; font-size: large;"><b>2) Use interface type when declaring any collection.</b></span></div><p><span style="font-family: arial; font-size: medium;">When you declare or create any collection, prefer to use interface type instead of specific collection class.</span></p><p><span style="font-family: arial; font-size: medium;">Example:-</span></p><p><span style="font-size: medium;"><span style="background-color: #f8f8f8;">List</span><span style="background-color: #f8f8f8; color: #666666;"><</span><span style="background-color: #f8f8f8;">String</span><span style="background-color: #f8f8f8; color: #666666;">></span><span style="background-color: #f8f8f8;"> listOfItems </span><span style="background-color: #f8f8f8; color: #666666;">=</span><span style="background-color: #f8f8f8;"> </span><span style="background-color: #f8f8f8; color: green; font-weight: bold;">new</span><span style="background-color: #f8f8f8;"> ArrayList</span><span style="background-color: #f8f8f8; color: #666666;"><>();</span></span></p><p><span style="font-family: arial; font-size: medium;">instead of,</span></p><p><span style="font-size: medium;"></span></p><p><span style="font-size: medium;"><span style="background-color: #f8f8f8;">ArrayList</span><span style="background-color: #f8f8f8; color: #666666;"><</span><span style="background-color: #f8f8f8;">String</span><span style="background-color: #f8f8f8; color: #666666;">></span><span style="background-color: #f8f8f8;"> listOfItems </span><span style="background-color: #f8f8f8; color: #666666;">=</span><span style="background-color: #f8f8f8;"> </span><span style="background-color: #f8f8f8; color: green; font-weight: bold;">new</span><span style="background-color: #f8f8f8;"> ArrayList</span><span style="background-color: #f8f8f8; color: #666666;"><>();</span></span></p><p><span style="font-family: arial; font-size: medium;">Using interface type is more flexible and convenient because we can change concreate implementation as needed.</span></p><p><span style="font-size: medium;">For example, </span><span style="font-family: inherit; font-size: medium;"><span style="background-color: #f8f8f8;">List</span><span style="background-color: #f8f8f8; color: #666666;"><</span><span style="background-color: #f8f8f8;">String</span><span style="background-color: #f8f8f8; color: #666666;">></span><span style="background-color: #f8f8f8;"> listOfItems </span><span style="background-color: #f8f8f8; color: #666666;">=</span><span style="background-color: #f8f8f8;"> </span><span style="background-color: #f8f8f8; color: green; font-weight: bold;">new</span><span style="background-color: #f8f8f8;"> LinkedList</span><span style="background-color: #f8f8f8; color: #666666;"><>();</span></span></p><p><span style="font-size: medium;"><br /></span></p><p><span style="font-size: large;"><b>3) Method return type should be interface instead of collection class.</b></span></p><p><span style="font-size: medium;"><span style="font-family: inherit;"> </span><span style="font-family: arial;"> Best practice to return type of method should be interface instead of collection class. It is more flexible because if any changes done inside the method should not impact on the caller.</span></span></p><div style="text-align: left;"><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: inherit; font-size: medium;"><span style="color: green;"><b> </b><span style="font-family: inherit;">public</span></span><span style="font-family: inherit;"> List<span style="color: #666666;"><</span>String<span style="color: #666666;">></span> <span style="color: blue;">getAllItems</span><span style="color: #666666;">()</span> <span style="color: #666666;">{</span>
</span></span></pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: inherit; font-size: medium;"> List<span style="color: #666666;"><</span>String<span style="color: #666666;">></span> listOfItems <span style="color: #666666;">=</span> <span style="color: green;">new</span> ArrayList<span style="color: #666666;"><>();</span>
<span style="color: #408080; font-style: italic;"> </span></span></pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: inherit; font-size: medium;"><span style="color: #408080; font-style: italic;"> //in future if we change arrayList to linkedList it doesn't impact on caller</span>
<span style="color: #408080; font-style: italic;"> // get all items - fetch items from database</span>
<span style="color: green;"> return</span> listOfItems<span style="color: #666666;">;</span>
<span style="color: #666666;"> }</span></span></pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: inherit; font-size: medium;"><span style="color: #666666;"><br /></span></span></pre></div><p><span style="font-family: inherit; font-size: large;"><b>4) Use generic type and diamond operator</b></span></p><p><span style="font-size: medium;"> <span style="font-family: arial;">Use generic type when declare any collection otherwise chances of throwing ClassCastException at run time.</span></span></p><p><span style="font-family: arial; font-size: medium;">Example : - </span></p>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-family: arial;">List listOfItems <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><>();</span>
listOfItems<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"item"</span><span style="color: #333333;">);</span>
listOfItems<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">);</span></span><span style="font-size: 14px;">
</span></pre></div>
<p><span style="font-family: arial; font-size: medium;">so avoid using above declaration, apply generics,</span></p>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;">List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> listOfItems <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><>();</span>
listOfItems<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"company"</span><span style="color: #333333;">);</span>
listOfItems<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">12</span><span style="color: #333333;">);</span> <span style="color: #888888;">//compile time error</span>
</span></pre></div>
<p><span style="font-size: medium;"> Use diamond operator(<>),</span></p><p><span style="font-family: arial; font-size: medium;"><span>This operator <> is called the diamond operator. Without diamond operator we have to write declaration twice so using this operator </span><span>no need to write declaration twice as follows,</span></span></p><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: inherit; font-size: medium;">List<span style="color: #666666;"><</span>String<span style="color: #666666;">></span> listOfItems <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">new</span> ArrayList<span style="color: #666666;"><</span>String<span style="color: #666666;">>();</span></span></pre><p><span style="font-family: arial; font-size: medium;">With <> operator,</span></p><p></p><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: inherit; font-size: medium;">List<span style="color: #666666;"><</span>String<span style="color: #666666;">></span> listOfItems <span style="color: #666666;">=</span> <span style="color: green; font-weight: bold;">new</span> ArrayList<span style="color: #666666;"><</span><span style="color: #666666;">>();</span></span></pre><div><span style="font-family: inherit; font-size: medium;"><span style="color: #666666;"><br /></span></span></div><div><span style="font-family: inherit; font-size: medium;"><span style="color: #666666;"><br /></span></span></div><div style="text-align: left;"><span style="font-family: inherit; font-size: large;"><b><span>5) </span>Prefer to use Collection isEmpty() or CollectionUtils.isEmpty() methods instead of size() method</b></span></div><div style="text-align: left;"><span style="font-family: inherit; font-size: large;"><b><br /></b></span></div><div style="text-align: left;"><div style="font-size: large;"><span style="font-family: arial;">Checking the emptiness of collection, avoid using size method like,</span></div><div style="font-family: inherit; font-size: large;"><br /></div><div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: arial; font-size: medium;"><span style="color: green; font-weight: bold;">if </span> <span style="color: #666666;">(</span>listOfItems<span style="color: #666666;">.</span><span style="color: #7d9029;">size</span> <span style="color: #666666;">></span> <span style="color: #666666;">0) </span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//write logic if list is not empty</span>
<span style="color: #666666;">}</span></span></pre></div><div style="font-family: inherit; font-size: large;"><br /></div><div style="font-size: large;"><span style="font-family: arial;">Prefer to use collection or collection util methods,</span></div><div style="font-family: inherit; font-size: large;"><br /></div><div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: arial; font-size: medium;"><span style="color: green; font-weight: bold;">if</span> <span style="color: #666666;">(!</span>listOfItems<span style="color: #666666;">.</span><span style="color: #7d9029;">isEmpty</span><span style="color: #666666;">())</span> <span style="color: #666666;">{</span>
<span style="color: #408080; font-style: italic;">//write logic if list is not empty</span>
<span style="color: #666666;">}</span></span></pre></div><div style="font-family: inherit; font-size: large;"><br /></div><div style="font-size: large;"><span style="font-family: arial;">or use collection util(apache) method,</span></div><div style="font-family: inherit; font-size: large;"><br /></div><div style="font-size: large;"><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: arial;"><span style="font-size: medium;"><span style="color: green; font-weight: bold;">if</span> <span style="color: #666666;">(CollectionUtils.isNot</span></span>Empty(listOfItems))<span> </span><span style="color: #666666;">{</span></span></pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: arial; font-size: medium;"> <span style="color: #408080; font-style: italic;">//write logic if list is not empty</span>
<span style="color: #666666;">}</span></span></pre></div><div style="font-family: inherit; font-size: large;"><br /></div><div style="font-family: inherit; font-size: large;"><br /></div><div style="font-family: inherit;"><span style="font-family: inherit; font-size: large;"><b>6) Specify initial capacity of a collection if possible</b></span></div><div style="font-family: inherit;"><span style="font-family: inherit; font-size: large;"><b><br /></b></span></div><div><span><div><span style="font-size: medium;"><b style="font-family: inherit;"> </b><span style="font-family: arial;">When we process a batch of records then prefer to specify the collection initial capacity as a batch size so </span></span><span style="font-family: arial; font-size: medium;">it avoids every time to resize the collection capacity when it exceeds the default capacity.</span></div><div style="font-family: inherit;"><span style="font-family: inherit; font-size: medium;"><br /></span></div><div style="font-family: inherit;"><span style="font-family: inherit; font-size: medium;"><span style="background-color: #f8f8f8;">List</span><span style="background-color: #f8f8f8; color: #666666;"><</span><span style="background-color: #f8f8f8;">String</span><span style="background-color: #f8f8f8; color: #666666;">></span><span style="background-color: #f8f8f8;"> listOfItems </span><span style="background-color: #f8f8f8; color: #666666;">=</span><span style="background-color: #f8f8f8;"> </span><span style="background-color: #f8f8f8; color: green; font-weight: bold;">new</span><span style="background-color: #f8f8f8;"> ArrayList</span><span style="background-color: #f8f8f8; color: #666666;"><>(500);</span></span></div><div style="font-family: inherit;"><br /></div><div><span style="font-family: arial; font-size: medium;">This creates an array list of 500 elements initially.</span></div><div style="font-family: inherit;"><span style="font-family: inherit; font-size: medium;"><br /></span></div><div><span style="font-family: inherit; font-size: large;"><b>7) Return an empty array or collection instead of a null value for methods that return an array or collection</b></span></div><div><span style="font-family: inherit; font-size: large;"><b><br /></b></span></div><div><div><span style="font-family: inherit; font-size: medium;"> </span><span style="font-family: arial; font-size: medium;"> If the method is collection valued then return empty collection instead of returning null because the client side no need to handle null check it's kind of extra</span></div><div><span style="font-family: arial; font-size: medium;">code to write in the client side. </span></div><div><span style="font-family: inherit; font-size: medium;"><br /></span></div><div>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="font-size: medium;"><span style="color: #888888;">//Avoid this</span>
<span style="color: #008800; font-weight: bold;">public</span> List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> <span style="color: #0066bb; font-weight: bold;">getAllPreSaleItems</span><span style="color: #333333;">(</span>String itemType<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> preSaleItems <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>itemType<span style="color: #333333;">.</span><span style="color: #0000cc;">equalsIgnoreCase</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"preSale"</span><span style="color: #333333;">))</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">//preSaleItems = fetch all presale items from database</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> preSaleItems<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span></span><span style="font-size: 14px;">
</span></pre></div>
<pre style="line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: arial; white-space: normal;"><br /></span></pre><pre style="line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: arial; font-size: medium; white-space: normal;">Best practice to return empty list as, </span></pre><pre style="line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: arial; white-space: normal;"><br /></span></pre>
<!--HTML generated using hilite.me--><div style="background: rgb(255, 255, 255); border-width: .1em .1em .1em .1em; border: 0.1em solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;"><pre style="line-height: 125%; margin: 0px;"><span style="color: #888888; font-size: 14px;">//best practice to return empty list</span><span style="font-size: 14px;">
</span><span style="font-size: medium;"><span style="color: #008800; font-weight: bold;">private</span> List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> <span style="color: #0066bb; font-weight: bold;">getAllPreSaleItems</span><span style="color: #333333;">(</span>String itemType<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> preSaleItems <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>itemType<span style="color: #333333;">.</span><span style="color: #0000cc;">equalsIgnoreCase</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"preSale"</span><span style="color: #333333;">))</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">//preSaleItems = fetch all presale items from database</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>preSaleItems <span style="color: #333333;">==</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> Collections<span style="color: #333333;">.</span><span style="color: #0000cc;">EMPTY_LIST</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">return</span> preSaleItems<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span></span><span style="font-size: 14px;">
</span></pre></div>
<div><span style="font-family: inherit; font-size: large;"><b><br /></b></span></div><div><span style="font-family: inherit; font-size: large;"><b>8) Do not use the classic for loop</b></span></div><div><span style="font-family: inherit; font-size: large;"><b><br /></b></span></div><div><div><span style="font-family: arial; font-size: medium;">Classic for loop,</span></div><div style="font-family: inherit; font-size: large;"><br /></div><div style="font-family: inherit; font-size: large;"><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;">List<span style="color: #666666;"><</span>String<span style="color: #666666;">></span> listOfItems <span style="color: #666666;">=</span> Arrays<span style="color: #666666;">.</span><span style="color: #7d9029;">asList</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"a"</span><span style="color: #666666;">,</span> <span style="color: #ba2121;">"b"</span><span style="color: #666666;">,</span> <span style="color: #ba2121;">"c"</span><span style="color: #666666;">);</span>
<span style="color: green; font-weight: bold;">for</span> <span style="color: #666666;">(</span><span style="color: #b00040;">int</span> i<span style="color: #666666;">=0;</span> i<span style="color: #666666;"><</span>listOfItems<span style="color: #666666;">.</span><span style="color: #7d9029;">size</span><span style="color: #666666;">();</span> i<span style="color: #666666;">++)</span> <span style="color: #666666;">{</span>
System<span style="color: #666666;">.</span><span style="color: #7d9029;">out</span><span style="color: #666666;">.</span><span style="color: #7d9029;">println</span><span style="color: #666666;">(</span>listOfItems<span style="color: #666666;">(</span>i<span style="color: #666666;">));</span>
<span style="color: #666666;">}</span></pre><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="color: #666666;"><br /></span></pre></div><div><span style="font-family: arial; font-size: medium;">Better to use for-each loop,</span></div><div style="font-family: inherit; font-size: large;"><br /></div><div style="font-family: inherit; font-size: large;"><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;">List<span style="color: #666666;"><</span>String<span style="color: #666666;">></span> listOfItems <span style="color: #666666;">=</span> Arrays<span style="color: #666666;">.</span><span style="color: #7d9029;">asList</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"a"</span><span style="color: #666666;">,</span> <span style="color: #ba2121;">"b"</span><span style="color: #666666;">,</span> <span style="color: #ba2121;">"c"</span><span style="color: #666666;">);</span>
<span style="color: green; font-weight: bold;">for</span> <span style="color: #666666;">(</span>String item <span style="color: #666666;">:</span> listOfItems<span style="color: #666666;">)</span> <span style="color: #666666;">{</span>
System<span style="color: #666666;">.</span><span style="color: #7d9029;">out</span><span style="color: #666666;">.</span><span style="color: #7d9029;">println</span><span style="color: #666666;">(</span>item<span style="color: #666666;">);</span>
<span style="color: #666666;">}</span></pre></div><div style="font-family: inherit; font-size: large;"><br /></div><div style="font-size: large;"><span style="font-family: arial;">The advantage of the for-each loop is that it eliminates the possibility of bugs and makes the code more readable. <span>It traverses each element one by one. </span></span></div><div style="font-family: inherit; font-size: large;"><br /></div><div style="font-family: inherit;"><span style="font-family: inherit; font-size: large;"><b>9) Prefer to use forEach() with Lambda expressions(Java 8 onwards)</b></span></div><div style="font-family: inherit;"><span style="font-family: inherit; font-size: large;"><b><br /></b></span></div><div><span style="font-size: medium;"><div><span style="font-family: inherit;"> </span><span style="font-family: arial;"> In forEach() method use java 8 lambda expression, so that the code even more compact, more flexible and more powerful.</span></div><div><span style="font-family: arial;"><br /></span></div><div><span style="font-family: arial;">Example:-</span></div><div style="font-family: inherit;"><br /></div><div><pre style="color: #333333; line-height: 16.25px; margin-bottom: 0px; margin-top: 0px;"><span style="font-family: arial;">List<span style="color: #666666;"><</span>String<span style="color: #666666;">></span> skuList <span style="color: #666666;">=</span> Arrays<span style="color: #666666;">.</span><span style="color: #7d9029;">asList</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"MTCCC"</span><span style="color: #666666;">,</span> <span style="color: #ba2121;">"MTAAA"</span><span style="color: #666666;">,</span> <span style="color: #ba2121;">"PT1111"</span><span style="color: #666666;">);</span>
skuList<span style="color: #666666;">.</span><span style="color: #7d9029;">forEach</span><span style="color: #666666;">(</span>sku <span style="color: #666666;">-></span> System<span style="color: #666666;">.</span><span style="color: #7d9029;">out</span><span style="color: #666666;">.</span><span style="color: #7d9029;">println</span><span style="color: #666666;">(</span>sku<span style="color: #666666;">));</span></span></pre></div><div style="font-family: inherit;"><br /></div></span></div></div><div><br /></div><div><span style="font-family: inherit; font-size: large;"><b>10) </b></span><span style="font-size: large;"><b>Prefer concurrent collections over synchronized wrappers</b></span></div><div><span style="font-family: inherit; font-size: large;"><b><br /></b></span></div><div><span><div><span style="font-size: medium;"> </span><span style="font-family: arial; font-size: medium;"> <span>When we develop any multi-threaded applications, consider using concurrent collections in the java.util.concurrent package instead of using the synchronized </span></span></div><div><span style="font-family: arial; font-size: medium;"><span>collections generated </span><span>by the Collections.synchronizedXXX() methods.</span></span></div><div><span style="font-family: arial; font-size: medium;"> It’s because the concurrent collections are designed to provide maximum performance in concurrent applications, by implementing different synchronization mechanisms like copy-on-write, compare-and-swap and special locks. </span></div><div><span style="font-family: arial; font-size: medium;"> </span></div><div><span style="font-family: arial; font-size: medium;">Example of concurrent package classes are <b><i>ConcurrentHashMap</i></b>, <b><i>CopyOnWriteArrayList</i></b>, <b><i>ConcurrentSkipListMap </i></b>and <b><i>PriorityBlockingQueue</i></b>.<br /></span></div><div style="font-size: large;"><span style="font-family: inherit;"><br /></span></div><div><span style="font-family: arial; font-size: large;"><b><br /></b></span></div><div><span style="font-family: arial; font-size: large;"><b>Related Posts:-</b></span></div><div><span style="font-family: arial; font-size: medium;"><b>1) <a href="http://adnjavainterview.blogspot.com/2019/10/unsupportedoperationexception-when-adding-or-removing-element-to-collection-in-java.html" target="_blank">UnsupportedOperationException when adding or removing element to a Collection in java</a></b></span></div><div><span style="font-family: arial; font-size: medium;"><b>2) <a href="http://adnjavainterview.blogspot.com/2019/10/why-treeset-does-not-allow-null-values.html" target="_blank">Why TreeSet does not allow null value in Java?</a></b></span></div><div><span style="font-family: arial; font-size: medium;"><b>3) <a href="http://adnjavainterview.blogspot.com/2017/12/how-to-remove-duplicates-from-arraylist.html" target="_blank">How to Remove duplicates from ArrayList in Java</a></b></span></div></span></div></div></div></span></div></div>Anil Nivargihttp://www.blogger.com/profile/06734257637895367389noreply@blogger.com0Sangli, Maharashtra, India16.8523973 74.5814773-15.61077250247839 39.4252273 49.31556710247839 109.7377273