02 Month
01 Hours
Spring | SpringBoot |
---|---|
Spring is good for j2ee/JDBC application. | SpringBoot is good for restful web services. |
Dependency injection is an important part | Springboot Autoconfiguration is important aspect |
Spring is structure based framework | Springboot is annotation based framework |
Spring is loosely coupled application | SpringBoot is stand alone application |
Spring use boilerplate code to make application loosely coupled | SpringBoot does not use boilerplate code |
Spring boot is xml based application where property must be defined under pom.xml | SpringBoot is starter based application where starter itself take care for configuration |
Embadded server such as tomcat ,jetty must be defined explicitly | SpringBoot already included with tomcat embedded server no need to define explicitly |
@RestController | @Controller |
---|---|
It is combination of controller and response body do not need to define explicitly | Controller convert data to human readable format, Response body need to defined explicitly |
Convert data to json and xml both | Convert data to xml format only |
It is a generic annotation using with class and method in order to map the data to the controller
When used with class it must include URL of API
,but when used with method of rest API
it must include Request method along with it.
@RequestMapping | @GetMapping |
---|---|
RequestMapping is generic annotation, which is used map the web request. | @Getmapping is class level annotation with specific to get call. |
Along with RequestMapping we need to define request method | @GetMapping do not need request method separately. |
This annotation is combination of three annotation @EnableAutoConfiguration, @componentScan and @configuration ,it is used by spring boot in main class of application.
✓ @EnableAutoConfiguration: – it auto configures the class according to starter, create the bean and register it to the jar
✓ @componentScan: - Scan the package for us configure component, service and repository
✓ @configuration: – Handel basic configuration to maintain the code flow
This annotation is used to detect property which must be checked by JVM at the time of compilation.
For changing the port no in Springboot we can change it application.
properties file present in resources folder by server. port = 8032
Springboot support three embedded servers i.e.
Apache Tomcat is default server that comes along with Springboot. For changing it to tomcat to jetty
spring.datasource.url= jdbc:mysql://localhost:3306/medicomart
spring.datasource.username=root
spring.datasource.password=admin
spring.jpa.generate-ddl=true
Intermediate | Terminal |
---|---|
Intermediate operations return a stream | Terminal operations return non-stream values like primitive or object or collection |
Chained together to form a pipeline of operations | Terminal operations can not be chained together |
Lezy by nature | Execute as soon as created |
They don’t produce end result. | They produce end result. |
|
|
Map | Flatmap |
---|---|
processes stream of values. | processes stream of stream of values. |
It does only mapping. | It performs mapping as well as flattening. |
It’s mapper function produces single value for each input value. |
It’s mapper function produces multiple values for each input value. |
It is a One-To-One mapping. | It is a One-To-Many mapping |
Data Transformation : From Stream to Stream | Data Transformation : From Stream of Stream to Stream |
List<String> namesOfInstitutes = instituteList.stream().map(Institute:: getName).collect(Collectors.toList());
Set<String> locationsOfInstitutes = instituteList.stream() .flatMap(institute -> institute.getLocations().stream()). collect(Collectors.toSet());
Collection | Stream |
---|---|
Part of JDK 1.2 | Part of JDK 1.8 |
Used to store different type of object in a single entity. | Streams are mainly used to perform operations on data. |
used for finite value of data. | used for infinite value of data. |
add or remove elements from collections | can’t add or remove elements from streams |
Collections can be traversed multiple times | Streams are traversable only once. |
Collections are eagerly constructed. | Streams are lazily constructed. |
Example :-List, Set, Map | Example : –filtering, mapping |
Iterator | Splitor |
---|---|
iterator is part of JDK 1.2 | splitor is part of JDK1.8 |
iterator part of collection Api | Splitor is part of steam Api |
Only Sequential order | Both Parallel and Sequential order |
it is pure Iterator | it is unpure Iterator |
Joining the strings or concatenating the strings is the frequent
operation while programming any kind of applications.
import java.util.StringJoiner; public class Java8StringJoiner { public static void main(String[] args) { StringJoiner stringJoiner = new StringJoiner(" | "); stringJoiner.add("Facebook"); stringJoiner.add("Twitter"); stringJoiner.add("YouTube"); stringJoiner.add("WhatsApp"); stringJoiner.add("LinkedIn"); System.out.println(stringJoiner); } } Output : Facebook | Twitter | YouTube | WhatsApp | LinkedIn
import java.util.StringJoiner; public class Java8StringJoiner { public static void main(String[] args) { StringJoiner stringJoiner = new StringJoiner(", ", "[", "]"); stringJoiner.add("Facebook"); stringJoiner.add("Twitter"); stringJoiner.add("YouTube"); stringJoiner.add("WhatsApp"); stringJoiner.add("LinkedIn"); System.out.println(stringJoiner); } } Output : [Facebook, Twitter, YouTube, WhatsApp, LinkedIn]
public class Java8StringJoinMethod { public static void main(String[] args) { String joinedString = String.join(" | ", "Facebook", "Twitter", "YouTube", "WhatsApp", "LinkedIn"); System.out.println(joinedString); } } Output : Facebook | Twitter | YouTube | WhatsApp | LinkedIn
public class Java8StringJoinMethod { public static void main(String[] args) { String[] strArray = new String[] {"Facebook", "Twitter", "YouTube", "WhatsApp", "LinkedIn"}; String joinedString = String.join(" | ", strArray); System.out.println(joinedString); } } Output : Facebook | Twitter | YouTube | WhatsApp | LinkedIn
import java.util.Arrays; import java.util.List; public class Java8StringJoinMethod { public static void main(String[] args) { List<String> listOfStrings = Arrays.asList("Facebook", "Twitter", "YouTube", "WhatsApp", "LinkedIn"); String joinedString = String.join(" | ", listOfStrings); System.out.println(joinedString); } } output: Facebook | Twitter | YouTube | WhatsApp | LinkedIn
Method reference used to refer to the method of functional interface.
It can be used to replace Lambda Expression while referring to a method.
Lembda Expression | Equivalent Method Reference |
---|---|
(String s)->integer.parseInt | integer::parseInt |
(String s)->s.toLowerCase | String::toLowerCase |
(int i)->System.out.println(i) | System.out::println |
(Student s)->s.getName(); | Student::getName |
()->s.getName() | s::getName |
()->new Student() | Student::name |
import java.util.function.Function; public class Java8MethodReferences { public static void main(String[] args) { //Calling parseInt() method using lambda Function < String, Integer > lambdaFunction = (String s) - > Integer.parseInt(s); System.out.println(lambdaFunction.apply("12")); //Calling parseInt() method using method reference Function < String, Integer > referenceFunction = Integer::parseInt; System.out.println(referenceFunction.apply("12")); } }