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

Real-World Use Cases and Examples

Examples of Lambda Usage in Real Projects:

  1. Web Development:

    • Routing in Web Frameworks: Lambda expressions are often used in web frameworks like Spring Boot for defining routes and handling HTTP requests.

    // Spring Web Example
    @RestController
    public class HelloController {
        @GetMapping("/hello")
        public String hello() {
            return "Hello, World!";
        }
    }
  2. Data Processing:

    • Batch Processing: Lambdas are utilized in batch processing applications for filtering, mapping, and reducing large datasets efficiently.

    List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
    names.stream().filter(name -> name.startsWith("A")).forEach(System.out::println);
    // Output: Alice
  3. Concurrency and Parallelism:

    • Parallel Stream Processing: Lambda expressions enable parallel processing of collections, leveraging multi-core processors.

    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
    int sum = numbers.parallelStream().mapToInt(Integer::intValue).sum();
    System.out.println("Sum: " + sum); // Output: Sum: 55

Case Studies and Problem Solving with Lambdas:

  1. Problem: Computing Average Age:

    • Description: Given a list of people, compute the average age of all adults (age >= 18) using lambda expressions.

    class Person {
        private String name;
        private int age;
    
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public int getAge() {
            return age;
        }
    }
    
    public class AverageAgeExample {
        public static void main(String[] args) {
            List<Person> people = Arrays.asList(
                new Person("Alice", 25),
                new Person("Bob", 17),
                new Person("Charlie", 30)
            );
    
            double averageAge = people.stream()
                .filter(person -> person.getAge() >= 18)
                .mapToInt(Person::getAge)
                .average()
                .orElse(0.0);
    
            System.out.println("Average Age of Adults: " + averageAge); // Output: Average Age of Adults: 27.5
        }
    }
  2. Problem: Finding Longest Word:

    • Description: Given a list of words, find and print the longest word using lambda expressions.

    import java.util.Arrays;
    import java.util.List;
    
    public class LongestWordExample {
        public static void main(String[] args) {
            List<String> words = Arrays.asList("apple", "banana", "orange", "kiwi");
    
            String longestWord = words.stream()
                .max((word1, word2) -> word1.length() - word2.length())
                .orElse("");
    
            System.out.println("Longest Word: " + longestWord); // Output: Longest Word: banana
        }
    }

These examples demonstrate real-world applications of lambda expressions in various domains, including web development, data processing, concurrency, and problem-solving scenarios. By leveraging the concise syntax and functional programming capabilities of lambdas, developers can write expressive and efficient code for a wide range of tasks.

PreviousBest Practices and Design Patterns

Last updated 1 year ago