Tuesday, 28 January 2025

Resiliency Paterns

 

Resiliency Patterns in Microservices

Resiliency patterns help microservices remain reliable and available even when failures occur. These patterns ensure that a system can handle faults gracefully, preventing cascading failures and improving user experience.


1. Circuit Breaker Pattern

When to Use?

  • When a service call is failing repeatedly.
  • When a dependent service is slow or unresponsive.
  • To prevent excessive retries that can overload the system.

Why Use It?

  • Prevents system overload by stopping calls to a failing service.
  • Helps recover gracefully by allowing time for the failed service to restart.

Example:

  • A payment service in an e-commerce system relies on a third-party payment gateway.
  • If the gateway is down, the Circuit Breaker trips and blocks further requests, preventing unnecessary failures.
  • Once the gateway recovers, the Circuit Breaker resets and allows requests again.

Tools: Netflix Hystrix, Resilience4j


2. Retry Pattern

When to Use?

  • When temporary failures occur due to network issues or rate limits.
  • When the failure is intermittent and expected to recover soon.

Why Use It?

  • Automatically retries failed operations instead of failing immediately.
  • Reduces temporary errors from affecting the user experience.

Example:

  • A weather app makes API calls to a weather provider.
  • If a request fails due to network timeout, it retries after a short delay before showing an error to the user.

Tools: Spring Retry, Polly (.NET), Resilience4j


3. Bulkhead Pattern

When to Use?

  • When different services or operations should be isolated to prevent cascading failures.
  • When multiple components share resources like threads or database connections.

Why Use It?

  • Prevents one failing service from taking down the entire system.
  • Ensures that critical services continue running even if non-critical ones fail.

Example:

  • A food delivery app has:
    • Order Service
    • Restaurant Search Service
    • User Profile Service
  • If Restaurant Search is overwhelmed with requests, Bulkhead ensures it doesn’t consume all resources, keeping Order Processing unaffected.

Tools: Netflix Hystrix, Istio Service Mesh


4. Fallback Pattern

When to Use?

  • When a dependent service is unavailable, but a default response can be provided.
  • When some functionality is better than complete failure.

Why Use It?

  • Improves user experience by providing a degraded but usable service.
  • Helps maintain system functionality during failures.

Example:

  • A flight booking system calls an external Seat Availability API.
  • If the API is down, a fallback response shows “Availability data is currently unavailable, please try again later” instead of an error.

Tools: Resilience4j, Spring Cloud Hystrix


5. Timeouts Pattern

When to Use?

  • When calling a service that may respond slowly.
  • When preventing a request from hanging indefinitely.

Why Use It?

  • Ensures slow services don’t block system resources.
  • Prevents user frustration due to long waits.

Example:

  • A banking app requests a user's transaction history.
  • If the request takes longer than 3 seconds, it times out and returns a default response to avoid locking the user interface.

Tools: Spring Boot, Netflix Ribbon


6. Rate Limiting Pattern

When to Use?

  • When preventing excessive API requests from overloading services.
  • When managing quota-based API consumption.

Why Use It?

  • Protects services from DDoS attacks and spikes in traffic.
  • Ensures fair usage across clients.

Example:

  • A stock trading platform limits each user to 100 API calls per minute.
  • If a user exceeds the limit, they receive an error message: "Rate limit exceeded, try again later."

Tools: Kong API Gateway, AWS API Gateway, Nginx


7. Idempotency Pattern

When to Use?

  • When ensuring duplicate requests don’t cause unintended effects.
  • When dealing with financial transactions or order processing.

Why Use It?

  • Prevents accidental duplicate processing.
  • Ensures consistency even if a request is retried due to failures.

Example:

  • A payment service processes a request to charge $100.
  • If a network issue causes the client to retry the request, the system checks if it was already processed to avoid charging twice.

Tools: Unique Request IDs, Idempotency Keys (Stripe, PayPal)


8. Shadow Traffic Testing Pattern

When to Use?

  • When testing a new service version without affecting real users.
  • When ensuring a system can handle increased load before deployment.

Why Use It?

  • Identifies potential failures before releasing changes.
  • Helps validate resiliency under real traffic conditions.

Example:

  • A ride-sharing app launches a new Matching Algorithm.
  • It receives duplicate traffic alongside the existing system but doesn’t affect real users, allowing engineers to measure impact safely.

Tools: AWS Traffic Mirroring, Nginx Traffic Splitting


Final Thoughts

Resiliency patterns help microservices handle failures effectively and maintain a seamless user experience. Here’s a quick summary of when to use each pattern:

By implementing these patterns, you can build a fault-tolerant, scalable, and robust microservices architecture.

Saturday, 18 January 2025

Micro services design patterns

 Microservices design patterns help address common challenges in building and maintaining microservice architectures. Here are several widely recognized patterns with examples:


1. Decomposition Patterns

These patterns help in splitting a monolithic application into microservices.

Domain-Driven Design (DDD): Dividing the system based on business domains. Each microservice corresponds to a specific domain.

Example: An e-commerce platform might have different microservices for Order, Payment, and Inventory based on the domain logic.



API Gateway: A single entry point that routes requests to appropriate microservices.

Example: In an online retail system, all requests pass through the API Gateway, which then forwards the request to services like Customer Service, Product Service, and Order Service.



2. Integration Patterns


These patterns manage how microservices communicate and exchange data.

Service Discovery: Allows microservices to dynamically discover and communicate with each other without hardcoding addresses.

Example: A User Service might use a service registry like Consul to discover the Notification Service dynamically.


Event-Driven Architecture: Microservices communicate asynchronously using events.


Example: When a customer places an order, the Order Service might publish an OrderPlaced event, which the Inventory Service listens to for stock updates.


Synchronous vs Asynchronous Communication:

Synchronous: Direct, real-time communication, often using REST or gRPC.


Asynchronous: Event-driven messaging (e.g., Kafka, RabbitMQ).

Example: A Payment Service might call a Bank API (synchronous), whereas a Shipping Service might listen for an Order Shipped event (asynchronous).


3. Data Management Patterns


These patterns focus on how data is stored and shared between microservices.

Database per Service: Each microservice manages its own database.


Example: The Order Service might use a relational database, while the Inventory Service uses a NoSQL database.


Saga Pattern: 

Handles long-running transactions by breaking them into smaller, distributed steps across multiple services.


Example: A Booking Service initiates a saga where the Payment Service is called first, and if successful, the Notification Service is triggered to confirm the booking. If any step fails, compensating transactions are performed to undo the effects.

CQRS (Command Query Responsibility Segregation): Separate models for reading and writing data to improve performance and scalability.


Example: A Customer Service might have one model for handling customer updates (commands) and another for querying customer information.


4. Reliability Patterns

These patterns deal with ensuring high availability and fault tolerance in microservices.


Circuit Breaker: Prevents cascading failures by detecting failures in communication with downstream services and avoiding repeated attempts.


Example: The Payment Service can implement a circuit breaker to stop trying to connect to a Bank Service if it has failed several times, thus avoiding a full system crash.


Retry Pattern: Automatically retrying failed requests with exponential backoff.

Example: The Payment Service retries connecting to an external payment gateway if a network failure occurs.

Bulkhead Pattern: Isolates services or components so that failures in one part do not affect the entire system.


Example: A Search Service and a Payment Service are isolated in different containers, so issues in one don't bring down the other.



5. Observability Patterns

These patterns ensure that microservices' health and activity are monitored.


Logging: Centralized logging from all services for monitoring and debugging.

Example: Services like Order Service and Payment Service log their activity to a centralized system like ELK stack (Elasticsearch, Logstash, Kibana).


Distributed Tracing: Tracing the journey of a request across multiple services.

Example: A user request to place an order is tracked from the front-end, to the Order Service, to the Payment Service, and back, with tools like Jaeger or Zipkin.


Health Check: Each service provides a health endpoint to monitor its status.

Example: A Product Service exposes a /health endpoint, which can be polled by a monitoring tool to ensure it's working.


6. Security Patterns


These patterns are focused on securing the communication and data in a microservices system.


API Gateway Security: All authentication and authorization can be centralized at the API Gateway.


Example: The API Gateway verifies JWT tokens before forwarding requests to the internal microservices.

OAuth2 and OpenID Connect: Used for securing APIs and microservices with centralized authentication.


Example: A user signs in via an Identity Provider (e.g., Google or Facebook), and the API Gateway issues a token that can be used across all services.

7. Deployment Patterns


These patterns address how microservices are deployed and scaled.


Sidecar Pattern: Deploys auxiliary services alongside the main service to provide additional functionality, such as logging, monitoring, or security.


Example: A User Service is deployed alongside a sidecar container that handles logging and monitoring using Fluentd or Prometheus.



Strangler Fig Pattern: Gradually replaces an old monolithic system with microservices by incrementally rewriting parts of the system.


Example: In an old e-commerce system, the Checkout Service is replaced with a new microservice while the rest of the system continues to run.

Blue/Green Deployment: Deploys a new version of a service while the old one is still running, and switches traffic once the new version is ready.

Example: An updated version of the Order Service is deployed and tested while the old version remains active. Once the new version is confirmed to work, traffic is switched to it.

These patterns provide a comprehensive approach to designing, deploying, and maintaining a resilient and

 scalable microservices architecture. Each pattern solves a specific problem and can be combined with others to create a robust system.


Monday, 13 January 2025

Reduce method in java8

 The reduce() method in Java 8 Streams is a terminal operation that takes a binary operator and applies it repeatedly to the elements of the stream to reduce the stream to a single result. It's a very powerful method for performing aggregation operations like summing, multiplying, concatenating, and many others

Signature of reduce():

Optional<T> reduce(BinaryOperator<T> accumulator);

T reduce(T identity, BinaryOperator<T> accumulator);

1. Optional<T> reduce(BinaryOperator<T> accumulator):

This version of reduce() does not have an identity element and returns an Optional<T>. This is because the result might be empty if the stream is empty.


2. T reduce(T identity, BinaryOperator<T> accumulator):

This version includes an identity value, which is a default value that is returned if the stream is empty. This version returns the result directly, not wrapped in an Optional.


BinaryOperator:


A BinaryOperator<T> is a functional interface that takes two arguments of type T and returns a result of type T.


Example Usage of reduce():


Example 1: Sum of Numbers


Let’s start by using reduce() to calculate the sum of numbers in a list.


import java.util.*;

import java.util.stream.*;


public class Main {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);


        // Using reduce to sum all elements in the list

        int sum = numbers.stream()

                          .reduce(0, (a, b) -> a + b); // Identity is 0, accumulator is a + b


        System.out.println("Sum: " + sum); // Output: 15

    }

}


Explanation:


The identity value is 0, so if the stream is empty, the result will be 0.


The accumulator function (a, b) -> a + b sums the elements in the stream.



Example 2: Multiplication of Numbers


Here’s how you can use reduce() to multiply the elements of a stream.


import java.util.*;

import java.util.stream.*;


public class Main {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);


        // Using reduce to multiply all elements in the list

        int product = numbers.stream()

                             .reduce(1, (a, b) -> a * b); // Identity is 1, accumulator is a * b


        System.out.println("Product: " + product); // Output: 120

    }

}


Explanation:


The identity value is 1 because multiplying by 1 does not change the result.


The accumulator function (a, b) -> a * b multiplies the elements in the stream.



Example 3: Concatenating Strings


You can also use reduce() to concatenate strings in a list.


import java.util.*;

import java.util.stream.*;


public class Main {

    public static void main(String[] args) {

        List<String> words = Arrays.asList("Java", "8", "Streams");


        // Using reduce to concatenate strings with a space

        String result = words.stream()

                             .reduce("", (a, b) -> a + " " + b).trim(); // Identity is "", accumulator is a + " " + b


        System.out.println("Concatenated: " + result); // Output: Java 8 Streams

    }

}


Explanation:


The identity value is an empty string "".


The accumulator function (a, b) -> a + " " + b adds a space between each string element.


The trim() removes the leading space.



Example 4: Finding Maximum Value


Let’s use reduce() to find the maximum value in a stream.


import java.util.*;

import java.util.stream.*;


public class Main {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(5, 12, 3, 7, 8);


        // Using reduce to find the maximum element

        int max = numbers.stream()

                         .reduce(Integer.MIN_VALUE, (a, b) -> a > b ? a : b); // Identity is Integer.MIN_VALUE


        System.out.println("Maximum value: " + max); // Output: 12

    }

}


Explanation:


The identity value is Integer.MIN_VALUE, ensuring that any value in the stream will be greater than it.


The accumulator function (a, b) -> a > b ? a : b compares the elements and keeps the larger one.



Example 5: Handling Optional with reduce()


If you don’t provide an identity value, reduce() will return an Optional<T> because the stream may be empty.


import java.util.*;

import java.util.stream.*;


public class Main {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);


        // Using reduce to find the sum of numbers with Optional return type

        Optional<Integer> sum = numbers.stream()

                                       .reduce((a, b) -> a + b);


        sum.ifPresent(value -> System.out.println("Sum: " + value)); // Output: Sum: 15

    }

}


Explanation:


Since there is no identity value, the result is wrapped in an Optional to handle the case where the stream might be empty.


We use ifPresent() to print the result.



Why Use reduce()?


Aggregation: It’s used for any kind of aggregation, such as summing, multiplying, or finding minimum/maximum values.


Immutable result: reduce() returns a single result (a reduced value), which can be useful when you need to compute a cumulative value.


Versatility: You can use it for a wide range of operations, from simple mathematical operations to complex transformations.



Common Use Cases of reduce():


Summing numbers.


Multiplying numbers.


Concatenating strings.


Finding minimum/maximum values.


Combining lists or collections into a single object.



Important Points:


1. Identity: The identity value should be chosen carefully as it serves as the starting point for the aggregation and also the default value if the stream is empty.



2. Optional: Without an identity, the result is wrapped in an Optional to handle the case of an empty stream.



3. Associativity: The reduce() operation should be associative, meaning the result should be the same regardless of the order in which the elements are combined. This is important when using parallel streams.




Conclusion:


The reduce() method is a

 powerful and flexible way to perform aggregation on streams in Java 8. Whether you're summing, multiplying, concatenating, or finding max/min values, reduce() is essential for many operations.


Sunday, 15 December 2024

Sed command

 

The sed command in Unix is a stream editor used for performing basic text transformations on an input stream (a file or input from a pipeline). It is often used for tasks such as search and replace, deleting lines, inserting lines, and more.

Syntax of sed command

sed [options] 'command' file
  • command: The operation to perform on the input text.
  • file: The file on which sed will operate. If no file is specified, sed reads from standard input (stdin).
  • options: Optional flags that modify sed's behavior.

Common sed Commands and Usage:

1. Search and Replace

The most common use of sed is for search and replace. It uses the syntax:

sed 's/old_text/new_text/' file
  • s/old_text/new_text/: This command searches for old_text and replaces it with new_text.

Example:


sed 's/apple/orange/' fruits.txt

This will replace the first occurrence of the word "apple" with "orange" in each line of fruits.txt


2 .Global Replacement

To replace all occurrences of a pattern in a line, use the g flag:



sed 's/old_text/new_text/g' file

Example:

sed 's/apple/orange/g' fruits.txt
  • This will replace all occurrences of "apple" with "orange" in each line of fruits.txt

Custom Comparator using java8 streams

 



l

Why override equals() and hashCode()?

  • equals(): Determines whether two objects are considered equal. When you add an object to a Set, the set uses this method to check if an object already exists in the set.
  • hashCode(): Provides a hash code that is used for efficient lookups in hash-based collections like HashSet. If you override equals(), you should also override hashCode() to maintain the general contract between these methods.



1 ) import java.util.HashSet;

import java.util.Set;


class Person {

    private String name;

    private int age;


    // Constructor

    public Person(String name, int age) {

        this.name = name;

        this.age = age;

    }


    // Getters

    public String getName() {

        return name;

    }


    public int getAge() {

        return age;

    }


    // Override equals() to compare name and age for equality

    @Override

    public boolean equals(Object o) {

        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        Person person = (Person) o;

        return age == person.age && name.equals(person.name);

    }


    // Override hashCode() to generate a consistent hash code based on name and age

    @Override

    public int hashCode() {

        return 31 * name.hashCode() + Integer.hashCode(age);

    }


    @Override

    public String toString() {

        return "Person{name='" + name + "', age=" + age + '}';

    }

}


public class Main {

    public static void main(String[] args) {

        // Create a Set of Person objects

        Set<Person> people = new HashSet<>();


        // Add some custom objects to the Set

        people.add(new Person("Alice", 30));

        people.add(new Person("Bob", 25));

        people.add(new Person("Alice", 30)); // Duplicate (same name and age)

        people.add(new Person("Charlie", 35));


        // Output the set to see unique objects

        for (Person person : people) {

            System.out.println(person);

        }

    }

}

 


2 )

import java.util.Set;

import java.util.TreeSet;

import java.util.Comparator;


class Person {

    private String name;

    private int age;


    // Constructor

    public Person(String name, int age) {

        this.name = name;

        this.age = age;

    }


    // Getters

    public String getName() {

        return name;

    }


    public int getAge() {

        return age;

    }


    @Override

    public String toString() {

        return "Person{name='" + name + "', age=" + age + '}';

    }

}


public class Main {

    public static void main(String[] args) {

        // Comparator to sort by name in ascending order

        Comparator<Person> nameComparator = (p1, p2) -> p1.getName().compareTo(p2.getName());


        // Create a TreeSet with a custom comparator (sorting by name)

        Set<Person> people = new TreeSet<>(nameComparator);


        // Add some custom objects to the Set

        people.add(new Person("Alice", 30));

        people.add(new Person("Bob", 25));

        people.add(new Person("Charlie", 35));


        // Output the set to see ordered elements

        for (Person person : people) {

            System.out.println(person);

        }

    }

}



3 ) 

import java.util.*;
import java.util.stream.Collectors;

class Person {
    private String name;
    private int age;

    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getters
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    // Override equals() and hashCode() for unique elements in Set
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && name.equals(person.name);
    }

    @Override
    public int hashCode() {
        return 31 * name.hashCode() + Integer.hashCode(age);
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + '}';
    }
}

public class Main {
    public static void main(String[] args) {
        List<Person> peopleList = Arrays.asList(
                new Person("Alice", 30),
                new Person("Bob", 25),
                new Person("Alice", 30),  // Duplicate
                new Person("Charlie", 35)
        );

        // Using Stream to add unique custom objects to a Set
        Set<Person> uniquePeople = peopleList.stream()
                .collect(Collectors.toSet()); // Collect into a Set to ensure uniqueness

        // Output the unique Set
        uniquePeople.forEach(System.out::println);
    }
}


4 ) 
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<Person> peopleList = Arrays.asList(
                new Person("Alice", 30),
                new Person("Bob", 25),
                new Person("Charlie", 35)
        );

        // Using Stream to sort by name (custom Comparator)
        List<Person> sortedByName = peopleList.stream()
                .sorted(Comparator.comparing(Person::getName)) // Sorting by name
                .collect(Collectors.toList());

        // Output the sorted list
        sortedByName.forEach(System.out::println);
    }
}

5 ) import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<Person> peopleList = Arrays.asList(
                new Person("Alice", 30),
                new Person("Bob", 25),
                new Person("Charlie", 35)
        );

        // Using Stream to map Person objects to their names
        List<String> names = peopleList.stream()
                .map(Person::getName) // Map Person to String (name)
                .collect(Collectors.toList());

        // Output the mapped names
        names.forEach(System.out::println);
    }
}

Saturday, 14 December 2024

Find the even and odd numbers using streams and multiply even numbers with 3 and odd one with 2

 

Find the even and odd numbers in list along with multiply even number with 3 and odd number with 

To achieve this using Java 8 Streams, there are multiple ways to process the list of integers, separate the odd and even numbers, and perform the required transformations (multiplying even numbers by 3 and odd numbers by 2

1 )


import java.util.*;

import java.util.stream.*;


public class Main {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);


        List<Integer> result = numbers.stream()

            .map(n -> (n % 2 == 0) ? n * 3 : n * 2) // Multiply even by 3 and odd by 2

            .collect(Collectors.toList()); // Collect the results into a list


        System.out.println(result);

    }

}




2 ) import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
        List<Integer> result = new ArrayList<>();

        numbers.stream()
            .forEach(n -> {
                if (n % 2 == 0) {
                    result.add(n * 3); // Multiply even by 3
                } else {
                    result.add(n * 2); // Multiply odd by 2
                }
            });

        System.out.println(result);
    }
}


3 )  import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        List<Integer> result = numbers.stream()
            .flatMap(n -> Stream.of((n % 2 == 0) ? n * 3 : n * 2)) // FlatMap example
            .collect(Collectors.toList());

        System.out.println(result);
    }
}

4 ) import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        List<Integer> result = numbers.stream()
            .mapToInt(n -> (n % 2 == 0) ? n * 3 : n * 2) // Use mapToInt for primitive operations
            .boxed() // Box the result back into Integer
            .collect(Collectors.toList());

        System.out.println(result);
    }
}


5 ) import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
        
        Optional<List<Integer>> result = Optional.ofNullable(numbers)
            .filter(list -> !list.isEmpty())
            .map(list -> list.stream()
                .map(n -> (n % 2 == 0) ? n * 3 : n * 2)
                .collect(Collectors.toList()));

        result.ifPresent(System.out::println); // Output the result if present
    }
}

6) import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        Map<Boolean, List<Integer>> grouped = numbers.stream()
            .collect(Collectors.groupingBy(n -> n % 2 == 0)); // Group by even or odd

        List<Integer> result = new ArrayList<>();
        
        grouped.get(true).forEach(n -> result.add(n * 3)); // Multiply even by 3
        grouped.get(false).forEach(n -> result.add(n * 2)); // Multiply odd by 2

        System.out.println(result);
    }
}

7 ) 
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

        List<Integer> result = numbers.stream()
            .collect(Collectors.mapping(n -> (n % 2 == 0) ? n * 3 : n * 2, Collectors.toList()));

        System.out.println(result);
    }
}


Tuesday, 14 June 2022

Java8::Stream


Java8::Steeam ::

Stream :: A stream is a sequence of data elements supporting sequential and parallel aggregate operations.To perform a computation, stream operations are composed into a stream pipeline. A stream pipeline consists of a source (which might be an array, a collection, a generator function, an I/O channel, etc), zero or more intermediate operations (which transform a stream into another stream, such as filter(Predicate)), and a terminal operation (which produces a result or side-effect, such as count() or forEach(Consumer))

how do streams differ from collections?
Both are abstractions for a collection of data elements. Collections focus on storage of data elements
for efficient access whereas streams focus on aggregate computations on data elements from a data source that is typically, but not necessarily, collections.

features of streams, comparing them with collections when necessary:

 Streams have no storage A collection is an in-memory data structure that stores all its elements. All elements must exist in memory before they are added to the collection. A stream has no storage; it does not store elements. A stream pulls elements from a data source on-demand and passes them to a pipeline of operations for processing.
Streams can represent a sequence of infinite elements : A collection cannot represent a group of infinite elements whereas a stream can. A collection stores all its elements in memory, and therefore, it is not possible to have an infinite number of elements in a collection. Having a collection of an infinite  number of elements will require an infinite amount of memory and the storage process will continue forever. A stream pulls its elements from a data source that can be a collection, a function that generates data, an I/O channel, etc.
 The design of streams is based on internal iteration.
 Streams are designed to be processed in parallel with no additional work from the developers.

 Streams are designed to support functional programming.
 Streams support lazy operations.
 Streams can be ordered or unordered.

 Streams cannot be reused.

How Streams Work Internally

Streams process data in a pipeline consisting of:

  1. Source: Collection, arrays, or any sequence.

  2. Intermediate Operations: Transformations like filter() and map() (Lazy execution).

  3. Terminal Operation: Triggers execution, e.g., forEach(), reduce().

Streams do not store data and process elements one at a time, improving memory efficiency. They often use Spliterators and internal iteration mechanisms, which differ from external loops.


Creating Streams:
There are many ways to create streams. Many existing classes in the Java libraries have received new methods that return a stream. Based on the data source, stream creation can be categorized as follows:


Streams from values: The Stream interface contains the following two static of() methods to create a sequential Stream from a single value and multiple values:
• <T> Stream<T> of(T t)
• <T> Stream<T> of(T...values)

public class Stram {

public static void main(String[] args) {

Stream<String> singleValue=Stream.of("Strmewithsinglevalue");
Stream<String> MutpleValues=Stream.of("x","y","zz","aa");
Stream<Integer> MultPleIntValues=Stream.of(2,4,6,8,10);

}

}

The following snippet of code creates a stream of strings from a String array returned from the split() method of the String class:
String str = "Ken,Jeff,Chris,Ellen";
// The stream will contain fur elements: "Ken", "Jeff", "Chris", and "Ellen"

Stream<String> stream = Stream.of(str.split(","));

Empty Streams::
An empty stream is a stream with no elements. The Stream interface contains an empty() static method to create an empty sequential stream.
// Creates an empty stream of strings
Stream<String> stream = Stream.empty();

The IntStream, LongStream, and DoubleStream interfaces also contain an empty() static method to create an empty stream of primitive types.
// Creates an empty stream of integers

IntStream numbers = IntStream.empty();


The Stream interface also supports creating a stream using the builder pattern using the  Stream.Builder<T> interface whose instance represents a stream builder. The builder() static method of the Stream interface returns a stream builder.
// Gets a stream builder
Stream.Builder<String> builder = Stream.builder();
The Stream.Builder<T> interface contains the following methods:
• void accept(T t)
• Stream.Builder<T> add(T t)

• Stream<T> build()

Stream<String> stream = Stream.<String>builder()
.add("Ken")
.add("Jeff")
.add("Chris")

.add("Ellen")
.build();

(or )
// Obtain a builder
Stream.Builder<String> builder = Stream.builder();
// Add elements and build the stream
Stream<String> stream = builder.add("Ken")
.add("Jeff")
.add("Chris")
.add("Ellen")

.build();

Streams from Functions ::  An infinite stream is a stream with a data source capable of generating infinite number of elements. aying that the data source should be “capable of generating” infinite number of elements, rather the data source should have or contain an infinite number of elements. It is impossible to generate and store an infinite number of elements of any kind because of memory and time constraints. However, it is possible to have a function that can generate infinite number of values on demand.

The Stream interface contains the following two static methods to generate an infinite stream:
• <T> Stream<T> iterate(T seed, UnaryOperator<T> f)
• <T> Stream<T> generate(Supplier<T> s)
The iterator() method creates a sequential ordered stream whereas the generate() method creates a

sequential unordered stream.

Using the Stream.iterate() Method::
The iterator() method takes two arguments: a seed and a function. The first argument is a seed that is the first element of the stream. The second element is generated by applying the function to the first element. The third element is generated by applying the function on the second element and so on. Its elements are seed, f(seed), f(f(seed)), f(f(f(seed))), and so on.

// Creates a stream of natural numbers
Stream<Long> naturalNumbers = Stream.iterate(1L, n -> n + 1);
// Creates a stream of odd natural numbers

Stream<Long> oddNaturalNumbers = Stream.iterate(1L, n -> n + 2);

Stream<Integer> streamIterated = Stream.iterate(40, n -> n + 2).limit(20);

streamIterated.forEach(System.out::println);



























Streams from Arrays::
The Arrays class in the java.util package contains an overloaded stream() static method to create sequential streams from arrays. You can use it to create an IntStream from an int array, a LongStream from a long array, a DoubleStream from a double array, and a Stream<T> from an array of the reference type T. The following snippet of code creates an IntStream and a Stream<String> from an int array and a String array:

// Creates a stream from an int array with elements 1, 2, and 3
IntStream numbers = Arrays.stream(new int[]{1, 2, 3});
// Creates a stream from a String array with elements "Ken", and "Jeff"
Stream<String> names = Arrays.stream(new String[] {"Ken", "Jeff"});

Streams from Collections ::
The Collection interface contains the stream() and parallelStream() methods that create sequential and parallel streams from a Collection, respectively.

import java.util.ArrayList;
import java.util.List;

public class LstStrm {

public static void main(String args[]) {
List<String> names=new ArrayList<String>();
names.add("one");
names.add("two");
names.add("three");
names.add("four");
names.add("five");
System.out.println(" display list of elements in java6 ::"+names);
names.stream().forEach(x->System.out.println("displaying list elements in java8" +x));

}
}

Stream Operations ::
A stream supports two types of operations:
• Intermediate operations
• Terminal operations

Intermediate operations are also known as lazy operations. Terminal operations are also known as eager operations. Operations are known as lazy and eager based on the way they pull the data elements from the data source. A lazy operation on a stream does not process the elements of the stream until another eager operation is called on the stream.
Streams connect though a chain of operations forming a stream pipeline. A stream is inherently lazy until you call a terminal operation on it. An intermediate operation on a stream produces another stream. When you call a terminal operation on a stream, the elements are pulled from the data source and pass through the stream pipeline.
Each intermediate operation takes elements from an input stream and transforms the elements to produce an output stream. The terminal operation takes inputs from a stream and produces the result.


There are multiple ways to create a stream in Java 8. Here are some common approaches:

1 . From a Collection (List, Set, etc.)

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamFromCollection {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

        Stream<String> nameStream = names.stream(); // Create stream from List
        nameStream.forEach(System.out::println);
    }
}

2.  Using Stream.of()


import java.util.stream.Stream;

public class StreamOfExample {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("Apple", "Banana", "Cherry");

        stream.forEach(System.out::println);
    }
}

3 . Using Array 

import java.util.Arrays;
import java.util.stream.Stream;

public class StreamFromArray {
    public static void main(String[] args) {
        String[] fruits = {"Mango", "Orange", "Grapes"};

        Stream<String> fruitStream = Arrays.stream(fruits);
        fruitStream.forEach(System.out::println);
    }
}



4 . Using IntStream, LongStream, DoubleStream (Primitive Streams)

import java.util.stream.IntStream;

public class PrimitiveStreamExample {
    public static void main(String[] args) {
        IntStream.range(1, 6).forEach(System.out::println);
    }
}