Java Streams Overview
Java Streams Overview
In Java, Streams are part of the `java.util.stream` package and provide a powerful way to perform
functional-style operations on sequences of elements, such as collections. Streams enable you to
process data in a declarative way, leveraging operations like filtering, mapping, and reducing. They
support both sequential and parallel execution, making them efficient for large data processing tasks.
1. Types of Streams
2. Creating a Stream
You can create a stream from different data sources like collections, arrays, or I/O channels.
From a collection:
java
From an array:
java
3. Stream Operations
Intermediate Operations: These operations transform a stream into another stream. They are
lazy and not executed until a terminal operation is invoked.
`filter`: Filters elements based on a condition.
java
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/27
java
stream.map(String::toUpperCase)
.forEach(System.out::println);
java
stream.forEach(System.out::println);
java
java
4. Example
java
5. Parallel Streams
For large data, parallel streams can be more efficient by utilizing multiple processors. This can be done
with `.parallelStream()`:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/27
java
list.parallelStream()
.filter(s -> s.startsWith("a"))
.forEach(System.out::println);
6. Best Practices
Avoid side-effects: Streams should ideally be free of side-effects for better readability and
performance.
Use parallelism judiciously: While parallel streams can speed up operations, they may not always
provide a performance boost, especially for small datasets or operations with significant overhead.
Streams offer a clean, functional way to manipulate data, reducing the boilerplate code for iterating and
processing collections.
When a concrete method of an abstract class is overridden in a subclass, the following happens:
If the abstract class contains a concrete method (i.e., a method with a body), it can be inherited by
subclasses.
Subclasses are not required to override the concrete method because the method is already
implemented in the abstract class.
However, subclasses can override this concrete method if they want to change its behavior.
When a concrete method is overridden in a subclass, the subclass provides its own implementation
of the method.
The method signature (name, parameters, return type) in the subclass must match the signature of
the method in the abstract class.
The overridden method in the subclass will be invoked instead of the inherited concrete method
when the method is called on an instance of the subclass.
3. Example
Consider the following example where an abstract class has a concrete method, and a subclass
overrides it:
java
4. Key Points
Customization: You might override the concrete method to customize the behavior of that
method in the subclass.
Maintainability: Keeping a default implementation in the abstract class but allowing subclasses to
override it gives flexibility without forcing every subclass to reimplement the same functionality.
To summarize: When a concrete method in an abstract class is overridden, the subclass provides its own
implementation of that method, and the overridden version will be used when invoked on instances of
the subclass.
In Java, the term static class typically refers to nested static classes, which are classes defined within
another class but declared with the `static` modifier. A static class in Java is not associated with
instances of the outer class, meaning it can be instantiated without creating an instance of the outer
class.
1. Defined Inside Another Class: A static class is defined inside another class but is independent of
the outer class's instance variables or methods.
2. Does Not Have a Reference to the Outer Class Instance: Unlike non-static nested classes (also
called inner classes), a static class cannot access non-static members (fields or methods) of the
outer class directly because it doesn't have an implicit reference to an instance of the outer class.
However, it can access static members of the outer class.
3. Can Be Instantiated Without Outer Class: A static nested class can be instantiated without an
instance of the outer class. You can create an object of the static nested class directly using the
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/27
outer class's name.
4. Usage for Logical Grouping: Static nested classes are often used when a class logically belongs to
the outer class but doesn't require access to instance-level data. This helps in better organization
and grouping of related classes.
Syntax:
java
class OuterClass {
static class StaticNestedClass {
void display() {
System.out.println("This is a static nested class.");
}
}
}
java
class OuterClass {
private static String outerStaticField = "Outer Static Field";
Key Points:
1. Can Access Static Members: A static nested class can access the static fields and methods of the
outer class directly.
2. Does Not Have a Reference to Outer Class Instance: A static nested class cannot access instance
variables or methods of the outer class unless you explicitly pass an instance of the outer class to
it.
3. Instance Creation: You do not need an instance of the outer class to create an instance of the
static nested class.
Grouping Related Classes: When a class is tightly bound to its enclosing class, but it doesn't need
to access the outer class's instance members, using a static nested class makes sense for logical
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/27
grouping.
Utility Classes: Static nested classes are often used for utility or helper classes that are used only
within the context of the outer class.
Static Nested Class: Refers to a static class defined inside another class.
Inner Class: A non-static nested class that has an implicit reference to an instance of the outer
class.
Regular (Top-Level) Classes: These are defined outside any other class and are independent of
other classes. Static nested classes are typically used for logical grouping of related classes.
Static Methods: Static classes are different from static methods. A static method can be invoked
without an instance of the class, while a static nested class is a separate class defined within
another class.
Conclusion:
A static class in Java refers to a nested static class, which is a class inside another class that can be
instantiated without an instance of the outer class. Static nested classes are useful for grouping related
classes without needing to instantiate the outer class.
1. Encapsulation
Concept: Encapsulation is the bundling of data (variables) and methods that operate on the data into a
single unit or class. It hides the internal state of an object and only exposes a controlled interface.
Real-World Example:
Bank Account: A bank account has private data (e.g., balance, account number) and provides
methods like `deposit()` and `withdraw()`. The internal state of the balance is hidden, and you can
only interact with it through the public methods.
Code Example:
java
class BankAccount {
private double balance; // private data
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/27
public void withdraw(double amount) { // public method
if (amount <= balance) {
balance -= amount;
}
}
2. Abstraction
Concept: Abstraction involves hiding the complex implementation details and showing only the
necessary functionality. This allows users to interact with an object at a high level.
Real-World Example:
Remote Control: A remote control abstracts away the internal working of a television. You only
need to know how to press buttons to control the TV (e.g., power, volume, channel), without
needing to understand how the signals are transmitted to the TV.
Code Example:
java
3. Inheritance
Concept: Inheritance allows a class to inherit properties and behaviors (methods) from another class.
This promotes code reusability and establishes a relationship between parent (superclass) and child
(subclass).
Real-World Example:
Vehicles: A general vehicle class can have common properties like `speed`, `fuelType`, and
methods like `start()` or `stop()`. Specific vehicle types (e.g., `Car`, `Truck`) can inherit these
properties and methods while adding their own specific behaviors.
Code Example:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/27
java
class Vehicle {
String fuelType;
int speed;
4. Polymorphism
Concept: Polymorphism allows objects to be treated as instances of their parent class, with the ability to
call methods that are overridden in child classes. It enables different behaviors for the same method in
different objects.
Real-World Example:
Shapes: Different types of shapes (circle, square, triangle) can have a method `draw()`, but each
shape will implement its own version of `draw()` to display the shape in a specific way.
Code Example:
java
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/27
System.out.println("Drawing Triangle");
}
}
class Main {
public static void main(String[] args) {
Shape shape1 = new Circle();
Shape shape2 = new Square();
Shape shape3 = new Triangle();
Concept: Composition is a design principle where one object contains another, allowing for more
flexible and reusable code. Instead of creating an inheritance hierarchy, objects can be composed of
other objects.
Real-World Example:
Computer: A computer can be composed of various components like a `CPU`, `RAM`, `HardDrive`,
etc., rather than inheriting from these components.
Code Example:
java
class CPU {
void process() {
System.out.println("CPU processing");
}
}
class RAM {
void storeData() {
System.out.println("RAM storing data");
}
}
class Computer {
private CPU cpu;
private RAM ram;
public Computer() {
cpu = new CPU();
ram = new RAM();
}
void run() {
cpu.process();
ram.storeData();
}
}
class Main {
public static void main(String[] args) {
Computer myComputer = new Computer();
myComputer.run();
}
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/27
6. Example Using All OOP Concepts:
Concepts:
Encapsulation: A `Smartphone` class hides the internal battery status and provides methods to
charge or use the phone.
Abstraction: The `Smartphone` class abstracts the complex processes of turning on/off and
charging the device.
Inheritance: `Smartphone` can inherit from a general `Device` class.
Polymorphism: Different devices (e.g., `Smartphone`, `Tablet`, `Laptop`) can have a common
`turnOn()` method with different implementations.
Code Example:
java
class Device {
void turnOn() {
System.out.println("Turning on device");
}
}
public Smartphone() {
this.batteryLevel = 100;
}
void charge() {
batteryLevel = 100;
System.out.println("Charging smartphone...");
}
@Override
void turnOn() {
System.out.println("Turning on smartphone...");
}
}
class Main {
public static void main(String[] args) {
Device smartphone = new Smartphone();
Device tablet = new Tablet();
Conclusion:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 10/27
OOP concepts like Encapsulation, Abstraction, Inheritance, and Polymorphism are not just abstract
programming principles but can be easily related to real-world objects and systems. By applying these
concepts, software development becomes more modular, maintainable, and easier to understand.
In Object-Oriented Programming (OOP), inheritance allows one class (the subclass or child class) to
inherit the properties and behaviors (fields and methods) of another class (the superclass or parent
class). There are different types of inheritance in OOP, each of which is used to establish different
relationships between classes. Below are the primary types of inheritance:
1. Single Inheritance
Definition: Single inheritance occurs when a subclass inherits from one parent class. This is the most
straightforward form of inheritance, where a class derives from a single class, inheriting its attributes
and methods.
Real-World Example:
A `Dog` class can inherit from an `Animal` class. The `Dog` class would inherit properties (like `name`,
`age`) and behaviors (like `eat()`, `sleep()`) from the `Animal` class.
Code Example:
java
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
2. Multilevel Inheritance
Definition: Multilevel inheritance occurs when a class inherits from a class that is itself derived from
another class. In other words, it forms a chain of inheritance.
Real-World Example:
A `Child` class inherits from a `Parent` class, which in turn inherits from a `Grandparent` class. The
`Child` class inherits properties and methods from both the `Parent` and `Grandparent` classes.
Code Example:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 11/27
java
class Grandparent {
void show() {
System.out.println("I am a grandparent");
}
}
Definition: In multiple inheritance, a subclass can inherit from more than one superclass. However, in
Java, multiple inheritance of classes is not allowed due to the diamond problem (ambiguity in the
inheritance hierarchy). However, multiple inheritance of interfaces is supported, allowing a class to
implement multiple interfaces.
Real-World Example:
A `Smartphone` can be both a `Camera` and a `Phone`, meaning it can inherit functionalities from
both.
java
interface Camera {
void takePhoto();
}
interface Phone {
void makeCall();
}
4. Hierarchical Inheritance
Definition: In hierarchical inheritance, multiple subclasses inherit from a single parent class. This means
that one superclass can have many subclasses, all of which inherit its properties and methods.
Real-World Example:
A `Vehicle` class can be inherited by multiple classes like `Car`, `Bike`, and `Truck`, where each
subclass represents a different type of vehicle, but they share common properties like `fuelType`
and `speed`.
Code Example:
java
class Vehicle {
void start() {
System.out.println("Vehicle starting");
}
}
5. Hybrid Inheritance
Definition: Hybrid inheritance is a combination of two or more types of inheritance. In Java, hybrid
inheritance can occur when a class inherits from multiple interfaces and a class (which is a form of
multiple and hierarchical inheritance combined). However, direct multiple inheritance of classes is not
allowed in Java.
Real-World Example:
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 13/27
A `SmartDevice` can inherit from both the `Phone` class and implement interfaces such as `Camera`
and `TouchScreen`.
java
interface Camera {
void takePhoto();
}
interface TouchScreen {
void touch();
}
class Phone {
void makeCall() {
System.out.println("Making a call");
}
}
Definition: This is an advanced form of inheritance where a class combines behaviors from multiple
sources to create a new class. It's not directly supported in Java in terms of class inheritance, but is
mimicked by interfaces.
Real-World Example:
A `SmartDevice` might inherit both `Camera` and `TouchScreen` functionalities, but may also
implement additional interfaces for other features like `Bluetooth` or `Wi-Fi`.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 14/27
Type Description Example
Multiple Inheritance `Smartphone` implements both
A subclass implements multiple interfaces.
(via Interfaces) `Camera` and `Phone`
Hierarchical Multiple subclasses inherit from a single `Car`, `Bike`, and `Truck` inherit
Inheritance parent class. from `Vehicle`
`SmartDevice` extends `Phone` and
Combination of more than one type of
Hybrid Inheritance implements `Camera` and
inheritance.
`TouchScreen`
Conclusion:
Inheritance in OOP allows classes to reuse and extend functionality from other classes. The different
types of inheritance (single, multilevel, multiple, hierarchical, hybrid) provide flexibility in how objects
and classes can be related to each other, promoting code reuse and modularity.
The OSI (Open Systems Interconnection) model is a conceptual framework used to understand and
implement network protocols in seven distinct layers. Each layer serves a specific purpose and
communicates with the layers directly above and below it. Here's a breakdown of the seven layers of
the OSI model:
1. Physical Layer
Function: Deals with the physical connection between devices and the transmission of raw binary
data over a medium (e.g., cables, wireless signals).
Key Responsibilities:
Data encoding and signal generation.
Transmission of bits over the physical medium.
Specification of hardware (e.g., cables, switches, connectors).
Examples:
Ethernet cables, fiber optics, coaxial cables, hubs.
Function: Ensures error-free data transfer between adjacent nodes over the physical layer. It
organizes raw bits into frames and handles error detection and correction.
Key Responsibilities:
Framing of data packets.
Error detection/correction (e.g., CRC).
Media Access Control (MAC) for shared communication.
Sub-layers:
MAC (Media Access Control): Handles access to the physical medium.
LLC (Logical Link Control): Ensures error-free communication.
Examples:
MAC addresses, Ethernet, Wi-Fi (IEEE 802.11).
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 15/27
3. Network Layer
Function: Handles logical addressing, routing, and forwarding of data across different networks. It
ensures data packets are delivered to the correct destination.
Key Responsibilities:
Logical addressing (IP addresses).
Routing and forwarding of data.
Fragmentation and reassembly of packets.
Examples:
Internet Protocol (IP), ICMP (ping), routers.
4. Transport Layer
Function: Ensures reliable delivery of data between systems. It segments data into smaller
packets, handles error recovery, and manages data flow.
Key Responsibilities:
Segmentation and reassembly of data.
Flow control (preventing congestion).
Error handling (acknowledgments, retransmissions).
Examples:
TCP (Transmission Control Protocol), UDP (User Datagram Protocol).
5. Session Layer
6. Presentation Layer
Function: Ensures data is in a readable format for the application layer. It handles data encoding,
encryption, and compression.
Key Responsibilities:
Data translation (e.g., EBCDIC to ASCII).
Data encryption and decryption (e.g., SSL/TLS).
Data compression (e.g., JPEG, MPEG).
Examples:
SSL (Secure Sockets Layer), TLS (Transport Layer Security), file formats (JPEG, GIF).
7. Application Layer
Function: Provides services for end-user applications to interact with the network. This layer
directly interfaces with user software.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 16/27
Key Responsibilities:
Network services (e.g., email, file transfer).
User interface for network-based applications.
Examples:
HTTP, FTP, SMTP, DNS, Telnet.
2. Data Link Framing, error detection/correction, MAC addresses Ethernet, Wi-Fi, switches, bridges
1. Physical Hardware, bit transmission over medium Cables, hubs, fiber optics, modems
Data is created at the Application Layer and passed down through the layers, being packaged and
modified as needed.
At the Physical Layer, it is transmitted as raw bits over the communication medium.
The process is reversed at the receiving end, where the raw bits are passed back up through the
layers until the data reaches the Application Layer of the destination.
1. Application Layer
Function:
Provides network services directly to user applications.
Handles data formatting, encryption, and transmission to the transport layer.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 17/27
Key Responsibilities:
Network-based application services like file transfer, email, and web browsing.
Examples of Protocols:
HTTP (Web Browsing)
SMTP (Email)
DNS (Domain Name Resolution)
FTP (File Transfer)
2. Transport Layer
Function:
Ensures end-to-end communication, reliability, and data integrity.
Manages segmentation, error detection, retransmission, and flow control.
Key Responsibilities:
Reliable delivery via acknowledgment and retransmission (TCP).
Unreliable delivery for faster communication (UDP).
Protocols:
TCP (Transmission Control Protocol): Reliable, connection-oriented protocol.
UDP (User Datagram Protocol): Fast, connectionless protocol.
Real-World Example:
Streaming a video (uses UDP for speed).
Downloading a file (uses TCP for reliability).
3. Internet Layer
Function:
Handles logical addressing and routing of data packets between networks.
Defines how packets are addressed and forwarded to their destination.
Key Responsibilities:
Assigning and using IP addresses.
Packet fragmentation and reassembly.
Ensuring packets reach the correct destination.
Protocols:
IP (Internet Protocol): Logical addressing and routing.
ICMP (Internet Control Message Protocol): Diagnostics (e.g., ping).
ARP (Address Resolution Protocol): Resolves IP to MAC addresses.
Real-World Example:
Routers forwarding packets based on IP addresses.
Function:
Manages hardware addressing and defines how data is physically sent across the network.
Key Responsibilities:
Encapsulation of IP packets into frames.
Physical transmission of frames over the network medium (e.g., cables, wireless).
Error detection and correction at the hardware level.
Examples of Technologies:
Ethernet, Wi-Fi, DSL, 5G.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 18/27
MAC (Media Access Control) and physical hardware.
1. Sender Side:
The Application Layer sends data (e.g., a webpage request).
The Transport Layer (TCP/UDP) segments the data and ensures reliability.
The Internet Layer assigns logical IP addresses for routing.
The Network Access Layer converts the data to signals and sends it physically.
2. Receiver Side:
The Network Access Layer receives raw signals and reconstructs frames.
The Internet Layer determines the correct IP address and forwards the data.
The Transport Layer reassembles the data and ensures reliability.
The Application Layer presents the data to the user (e.g., loading a webpage).
The TCP/IP model is the foundation of modern network communication, including the Internet, and is
widely used across various technologies.
In Java, Stream classes are part of the `java.util.stream` package introduced in Java 8. They are used
to process collections of data in a functional and declarative way, enabling operations like filtering,
mapping, and reducing without explicit iteration.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 19/27
Key Stream Classes in Java
1. Stream
The base interface for processing sequences of elements.
Used for processing non-primitive types like `String`, `Integer`, `Object`, etc.
Provides methods for intermediate and terminal operations.
2. IntStream
A specialized stream for processing `int` values.
Offers methods like `sum()`, `average()`, and `range()` for numeric data.
3. LongStream
A specialized stream for processing `long` values.
Provides methods for operations like summation and range generation.
4. DoubleStream
A specialized stream for processing `double` values.
Includes statistical operations like `sum()`, `average()`, and more.
5. Collectors
A utility class that provides implementations for reduction operations such as grouping,
joining, and collecting into lists or maps.
Declarative: Focuses on what to do (e.g., `filter(e -> e > 10)`) rather than how to do it.
Lazy Evaluation: Operations on streams are not executed until a terminal operation is invoked.
Parallel Processing: Streams can run operations in parallel to utilize multi-core processors
efficiently.
1. Intermediate Operations
Transform or filter the data but do not produce results immediately (lazy evaluation).
Examples:
`filter()`: Filters elements based on a predicate.
`map()`: Transforms elements.
`sorted()`: Sorts elements.
`distinct()`: Removes duplicates.
2. Terminal Operations
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 20/27
Examples of Stream Classes
java
import java.util.*;
import java.util.stream.*;
2. IntStream Example
java
import java.util.stream.IntStream;
3. DoubleStream Example
java
import java.util.stream.DoubleStream;
4. LongStream Example
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 21/27
java
import java.util.stream.LongStream;
1. Collecting Elements
java
import java.util.*;
import java.util.stream.*;
2. Joining Strings
java
import java.util.*;
import java.util.stream.*;
1. Simplifies Code: Reduces boilerplate for operations like filtering and mapping.
2. Functional Programming: Encourages a functional programming style with lambdas.
3. Efficiency: Supports parallel processing and lazy evaluation.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 22/27
4. Readable and Concise: Operations like filtering, sorting, and grouping are straightforward.
Streams are a powerful tool for handling data collections and allow developers to write efficient and
readable code.
Here is a Java program to reverse a string without using `StringBuilder` or `StringBuffer`. The
program uses a manual approach like a `for` loop or recursion.
java
return result;
}
}
Explanation:
java
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 23/27
return new String(chars);
}
}
Explanation:
3. Using Recursion
java
Explanation:
4. Using a Stack
java
import java.util.Stack;
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 24/27
}
return reversed.toString();
}
}
Explanation:
java
import java.util.stream.Collectors;
Explanation:
These methods demonstrate different ways to reverse a string in Java without using `StringBuilder`.
The approach depends on your preferences and performance considerations. For simple use cases, the
for loop or character array swapping methods are efficient.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 25/27
java
1. Not a Keyword:
is a method name, not a reserved keyword. You can use `main` as an identifier in other
`main`
contexts (e.g., variable names or class names), but doing so can lead to confusion.
2. Special Role in Java Programs:
The JVM calls the `main` method to start the program execution. If the `main` method is
absent or has an incorrect signature, the program won't run.
3. Standard Signature:
The `main` method must always be defined as:
java
java
css
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 26/27
What Happens if `main` is Missing?
If there is no `main` method with the correct signature, the program will throw a runtime error like:
typescript
Error: Main method not found in class ClassName, please define the main method as:
public static void main(String[] args)
In conclusion, `main` is not a keyword but a special method name that serves as the starting point for
Java applications.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 27/27