Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Lambda

Stream API

Introduction to Streams:

Streams in Java provide a way to process collections of objects in a functional style. Streams allow you to express complex data manipulations concisely and clearly. They can be processed in parallel to enhance performance.

Stream Operations (Intermediate and Terminal Operations):

  • Intermediate Operations: Intermediate operations return a new stream and allow you to perform operations on the original stream's elements. Examples include filter(), map(), flatMap(), distinct(), etc.

    Example of Intermediate Operation:

    List<String> words = Arrays.asList("hello", "world", "java");
    List<String> longWords = words.stream()
                                .filter(word -> word.length() > 4)
                                .collect(Collectors.toList());
  • Terminal Operations: Terminal operations produce a result or a side-effect and terminate the stream. Examples include collect(), forEach(), reduce(), count(), sum(), etc.

    Example of Terminal Operation:

    List<String> words = Arrays.asList("hello", "world", "java");
    long count = words.stream()
                     .filter(word -> word.length() > 4)
                     .count();

Stream Creation and Processing:

  • Creating Streams:

    List<String> list = Arrays.asList("a", "b", "c");
    Stream<String> streamFromList = list.stream();
    
    Stream<String> streamFromArray = Arrays.stream(new String[] {"a", "b", "c"});
    
    Stream<String> streamFromStreamOf = Stream.of("a", "b", "c");
  • Processing Streams:

    List<String> words = Arrays.asList("hello", "world", "java");
    
    // Using filter and forEach
    words.stream()
         .filter(word -> word.length() > 4)
         .forEach(System.out::println);
    
    // Using map and collect
    List<String> upperCaseWords = words.stream()
                                     .map(String::toUpperCase)
                                     .collect(Collectors.toList());

Collectors:

Collectors provide a convenient way to transform the elements of a stream into different data structures or to perform a final reduction on the elements of a stream.

Example of Using Collectors:

import java.util.List;
import java.util.stream.Collectors;

List<String> words = Arrays.asList("hello", "world", "java");
List<String> result = words.stream()
                         .map(String::toUpperCase)
                         .collect(Collectors.toList());

Stream Performance and Optimization:

Streams can be optimized using parallel processing for improved performance on multi-core processors. You can convert a stream to a parallel stream using the parallel() method.

Example of Parallel Stream:

List<String> words = Arrays.asList("hello", "world", "java");
long count = words.parallelStream()
                  .filter(word -> word.length() > 4)
                  .count();

Using parallel streams can lead to significant performance improvements for computationally intensive operations. However, it's important to be aware of thread safety when using parallel streams.

The Stream API in Java provides a powerful way to work with collections and data manipulation. Understanding how to create streams, perform operations, and optimize their usage can lead to more efficient and readable code.

PreviousFunctional Programming ConceptsNextError Handling in Lambda Expressions

Last updated 1 year ago