java 11 beyond basics
java 11 beyond basics
through the advanced concepts in Java along with the usage of different tools in the development of
an enterprise application such as logging, testing, etc.
func:
work with Higher-Order functions like Lambdas and Streams which will simplify most of your
business logic by the size and complexity. Scores of lines will get reduced to fewer lines, the code as a
whole will become readable and the modularity also increases.
Lambda expressions
there should be only one abstract method in it. But an interface can contain more than one abstract
method, to make sure that not more than one abstract methods exist inside a functional interface
@FunctionalInterface
interface MyInterface {
(int num1, int num2) -> System.out.println(num1+ " " +num2); // Bad practice
good practice to not use lambda expressions whenever a large block of code has to be executed.
A Stream represents a sequence of elements from a source and supports various data processing
operations
provide the support to parallel threading of the code, which breaks the bigger problem into smaller
chunks. These smaller problems are solved separately and their solutions are combined together to
obtain the final solution.
Concept of Functional Programming: Functions that can either accept other functions as parameters
or return other functions as parameters are called Higher Order Functions (HOF).
Higher-Order Functions can be either User-Defined or Inbuilt. Inbuilt Higher-Order Functions are
available through Streams in Java 8 and onwards.
class Employee{
super();
this.name = name;
this.salary = salary;
placesToVisit.add("Chicago");
placesToVisit.add("Venice");
placesToVisit.add("Tokyo");
placesToVisit.add("San Francisco");
placesToVisit.add("Kyoto");
placesToVisit.add("Abu Dhabi");
3. filter(): used to return a new filtered stream based on conditions given for filtering
placesToVisit.add("Chicago");
placesToVisit.add("Venice");
placesToVisit.add("Tokyo");
placesToVisit.add("San Francisco");
placesToVisit.add("Kyoto");
placesToVisit.add("Abu Dhabi");
4. sorted(): used to sort the elements in a Stream. Argument for this method is optional.
placesToVisit.add("Chicago");
placesToVisit.add("Venice");
placesToVisit.add("Tokyo");
placesToVisit.add("San Francisco");
placesToVisit.add("Kyoto");
placesToVisit.add("Abu Dhabi");
placesToVisit.add("Chicago");
placesToVisit.add("Venice");
placesToVisit.add("Tokyo");
placesToVisit.add("San Francisco");
placesToVisit.add("Kyoto");
placesToVisit.add("Abu Dhabi");
5. Collect(): stores the modified stream as a new collection type (it can be list, map etc.)
number.add(2);
number.add(3);
number.add(4);
number.add(5);
System.out.println(doubled);
different operations of Stream individually:
placesToVisit.add("Chicago");
placesToVisit.add("Venice");
placesToVisit.add("Tokyo");
placesToVisit.add("San Francisco");
placesToVisit.add("Kyoto");
placesToVisit.add("Abu Dhabi");
placesToVisit.add("Chicago");
placesToVisit.add("Venice");
placesToVisit.add("Tokyo");
placesToVisit.add("San Francisco");
placesToVisit.add("Kyoto");
placesToVisit.add("Abu Dhabi");
placesToVisit.stream() //stream()
Operations such as map(), filter(), sorted() comprise the intermediate operations, intermediate
operations return a stream after its execution.
Operations such as forEach() and/or collect() comprise the terminal operations, as these operations
do not return any stream, they deliver the final stream by either by displaying them (forEach()) or by
storing them in a collection (collect()).
Problem Statement
Stream of random integers. Our goal is to display a sorted stream consisting of cubes of all even
integers in the given stream. We have the following chained stream operations as our solution.
.sorted()
because until the terminal operation is invoked, none of the intermediate operations is executed.
Non-Reusability of streams
a runtime exception will be thrown. This is because once a stream is operated on it cannot be
operated on once more.
properties file
Properties file is a text file used to store any kind of textual information in the form of key-value pairs
used for
1. by using a third-party library called Apache Commons(provides classes that can be used to
perform certain operations on a ".properties" file. It contains a PropertiesConfiguration class
which is used to read a properties file).
Logging
1. Any event that is to the interest of the programmer or administrator can be logged.
2. Relevant messages can be recorded in the event of exceptions and errors.
For the ease of the developer, there are many logging APIs available. Based on the requirement, the
developer can choose any of them. Few popular APIs for logging are,
using the combination of Apache Commons Logging API and Apache Log4J.
Apache Commons Logging is a logging utility that is Java-based and provides the implementation of
loggers and also supports the different Logging APIs.
Log4j is written in Java and is open source. It provides mechanisms to directly log information to a
variety of destinations such as database, file, console, etc. It also logs information in various
formatting styles such as HTML, XML, Log, etc. We are using an updated version of Log4J, i.e., Log4j2.
Log4j2 has 3 components
1. Appender – Specifies where the logging output should be stored. It can be the console, an
external file, any database, etc.
2. Layout– Specifies in what format should the information be logged. It can be a simple layout
which logs the level followed by the message, or we can specify a pattern for it to follow, etc.
3. Logger – Accepts all the information to be logged and redirects it to an appropriate
Appender.
Logging Levels used to differentiate between the severity between different logs. Logging Levels are
provided by the class.
Testing of single small units of code such as a method or a class and asserting certain behavior is
called unit testing. It is done by the person who develops the code.
Manual
Which is time-consuming
Automated
There are several frameworks available for automating unit tests. JUnit and TestNG are among the
popular ones.
JUnit
JUnit is an open-source unit testing framework for Java. It provides classes to write and run
automated tests.
It provides :
JUnit5
JUnit Platform, is the project which helps in executing the tests on the Java Virtual Machine(JVM). It
also provides the definition for TestEngine API (which discovers test cases in a project and executes
them).
JUnit Jupiter is the main project which helps you write test cases in accordance to JUnit 5. It also
provides TestEngine definition for running Jupiter based test cases. (Note: The name for this came up
to being Jupiter, because, Jupiter is the only planet in our solar system whose name starts with 'J',
and also it is the 5th planet is our solar system, indicating the version of JUnit, ergo, JUnit 5.)
JUnit Vintage provides the definition for TestEngine that supports the previous versions of JUnit, like
JUnit 3 or JUnit 4.
Without the annotation, neither can the JUnit Framework recognise the method, nor can it execute
it.
Since JUnit 5 has the support for JUnit 4, there will be two instances for @Test annotation. Please
import the annotation from org.junit.jupiter.api.Test. The other @Test which comes from
org.junit.Test creates test case methods for JUnit 4 Framework.
Once you execute the test case, the JUnit framework will take over, call the method to be executed
and execute it. You will then be able to see an output.
To specify the output to the test case method, we will use a special class called Assertions (imported
from org.junit.jupiter.api.Assertions). This class provides various methods, using which we can
specify the expected output to the test case. Few of the important methods are,
assertEquals(Object expected, Object actuals) - This method accepts two parameters and passes the
test case only if both of the parameters are equal
assertNotEquals(Object expected, Object actuals) - This method accepts two parameters and passes
the test case only if both of the parameters are not equal
assertTrue(boolean condition) - This method accepts a boolean return condition and passes the test
case only if the return value is true.
assertFalse(boolean condition) - This method accepts a boolean return condition and passes the test
case only if the return value is false.
assertNull(Object actual) - This method accepts an object and passes the test case only if the object
is null.
assertNotNull(Object actual) - This method accepts an object and passes the test case only if the
object is not null.
Note: Executable interface is a functional interface, which accepts no parameters and returns the
value returned by the invoked function. To implement the Executable interface, we will use a Lambda
function.
3.1 Assignment 1 – Problem Statement, Solution, Assessment, and Conclusion
Problem Statement: InfyAcademy requires assistance in adding candidates and generating their
performance reports. This assignment focuses on implementing the necessary modules to facilitate
these functionalities. The specific tasks include converting conditional and iterative statements to
ternary operators or stream functions and ensuring that exceptions are appropriately logged and
propagated.
Solution:
Validator Class: Implements various validation methods for candidate attributes, ensuring
that only valid data is processed.
4o
What are assignment 1,2,3 doing , difference between them and the function of it
ChatGPT
Problem Statement: InfyAcademy requires the development of modules to add candidates and
generate candidate reports. This assignment involves converting conditional and iterative statements
to ternary operators or stream functions, improving the efficiency and readability of the code.
Solution:
Function:
Adding Candidates: Validate inputs and add valid candidates to the database.
Generating Candidate Reports: Retrieve and process candidate data to generate reports.
Solution:
Validator Class: Log all exceptions thrown by validation methods and propagate them.
Function:
Exception Handling: Implement logging and ensure exceptions are properly propagated.
Problem Statement: The third assignment involves rectifying bugs in the Validator and Service
classes and implementing detailed validation logic. The task also includes ensuring proper logging
and exception handling.
Solution:
Validator Class: Implement detailed validation logic for various candidate attributes.
o Validate candidate name, ID, department, exam date, exam marks, and result.
CandidateServiceImpl Class: Implement business logic for adding candidates and calculating
grades.
Function:
Detailed Validation: Ensure all candidate attributes are validated according to specified
rules.
Business Logic Implementation: Add candidates to the database and calculate grades based
on marks.
Differences and Functions
Differences:
1. Assignment 1:
2. Assignment 2:
3. Assignment 3:
Functions:
1. Adding Candidates: Validate inputs and add valid candidates to the database.
2. Generating Candidate Reports: Retrieve and process candidate data to generate reports.
5. Detailed Validation: Ensure all candidate attributes are validated according to specified
rules.
6. Business Logic Implementation: Add candidates to the database and calculate grades based
on marks.