Powered by Blogger.

Java 8 Introduced Key Features

No comments :
Java 8 introduced several key features that significantly enhanced the language and its libraries. Here are the principal features:
Lambda Expressions:
Allow you to treat functionality as a method argument, or to create small function-like objects more concisely.
Syntax: 
               (parameters) -> expression or (parameters) -> { statements; }
Functional Interfaces:
An interface with just one abstract method, used as the type for lambda expressions and method references.
Example: 
                java.util.function.Predicate; 
                java.util.function.Function;
Stream API:
Introduces a new abstraction for working with sequences of elements and performing operations like filter, map, and reduce on them.
Example: 
                List myList = ...; myList.stream().filter(s -> s.startsWith("a")).forEach(System.out::println);
Default Methods:
Allow you to add new methods to interfaces without breaking the existing implementations.
Example: 
                    interface MyInterface { 
                     default void newMethod() {
                              ............ // write Method body 
                                  } }
Optional:
A container object which may or may not contain a non-null value, to avoid `NullPointerException`.
Example: 
                Optional opt = Optional.of("value"); 
                opt.ifPresent(System.out::println);
New Date and Time API:
A comprehensive new date and time library under `java.time` package, which is more flexible and easier to use than the old java.util.Date and java.util.Calendar classes.
Nashorn JavaScript Engine:
Replaces the old Rhino JavaScript engine, allowing for the embedding of JavaScript code within Java applications.
Method References:
Provide a way to refer to methods without invoking them, which can be used for constructing lambda expressions.
Example: ClassName::methodName.

These features aim to make Java code more expressive, readable, and efficient.

How to Mapping code logic using Stream API

No comments :
Stream API in Java provides a powerful way to work with collections and other data sources. Mapping is one of the key operations available in the Stream API, used to transform elements of a stream. Here's a step-by-step guide on how to use the map function with the Stream API:
Import Required Classes :

First, make sure to import the necessary classes:
import java.util.Arrays;
import java.util.List; 
import java.util.stream.Collectors; 
import java.util.stream.Stream;

Create a Stream:
You can create a stream from various sources, like collections, arrays, or generator functions. For example, creating a stream from a list:
List names = Arrays.asList("a14mk", "manoj", "Jack"); 
Stream nameStream = names.stream();
Apply the map Function:
Use the map function to transform each element of the stream. For instance, to convert each name to uppercase:
Stream upperCaseNames = nameStream.map(String::toUpperCase);
Collect the Results:
Finally, you can collect the transformed stream back into a list or another collection:
List upperCaseNameList = upperCaseNames.collect(Collectors.toList());
Complete Example:
Here’s a complete example demonstrating how to use the map function with the Stream API:

Mapping with Complex Objects:
If you are working with a list of objects, you can use the map function to transform specific fields or create new objects. For example:

In this example, the map function is used to extract the names from a list of Person objects.

Inversion of Control (IoC) in Spring Frameworks

1 comment :
IoC stands for "Inversion of Control", a principle in software engineering where the control of objects or portions of a program is transferred to a framework or container. This concept is fundamental to achieving loose coupling in object-oriented design, which makes applications more modular, flexible, and easier to maintain.
Key Concepts of Inversion of Control : 
  • Dependency Injection (DI)
  1. The most common implementation of IoC. 
  2. Dependencies (objects that a class requires to function) are injected into the class rather than the class creating them itself. 
  3.  Can be implemented in three main ways: constructor injection, setter injection, and interface injection.
  • Service Locator
  1.  An alternative to dependency injection. 
  2.  A central registry (the service locator) provides dependencies upon request. 
  3.  Can lead to less transparency in dependency management compared to DI.
How IoC Works:
  •   Without IoC.

  •  With IoC (Dependency Injection).


In the example above, with IoC, the `Car` class does not create the `Engine` instance itself. Instead, it receives it from an external source (typically an IoC container).
Benefits of IoC:
Decoupling: Reduces the dependency between components, making the system more modular. Testability: Easier to test components in isolation since dependencies can be easily mocked or stubbed. Flexibility: Easier to swap out implementations of dependencies without modifying the dependent class. 
 Maintainability: Simplifies code management and maintenance by following the single responsibility principle. 
IoC Containers: IoC containers are frameworks that manage the creation and injection of dependencies. Some popular IoC containers are:
Spring (Java): Provides comprehensive support for dependency injection and IoC. 
PicoContainer (Java): Lightweight and simple to use. 
Unity (C#): A dependency injection container from Microsoft. 
Autofac (C#): A popular IoC container for .NET applications.
 
Example in Spring Framework:

Spring Framework is a well-known example of an IoC container. Here’s how dependency injection is typically configured in a Spring application:
 
XML Configuration:



Java Configuration:



Annotation-Based Configuration:


In these examples, the Spring container manages the lifecycle and injection of the `Engine` dependency into the `Car` class. In summary, IoC is a design principle that helps to create flexible, maintainable, and testable software systems by inverting the control of dependency management. Dependency injection is the most common pattern used to implement IoC, and it is supported by various frameworks and containers across different programming languages.