0% found this document useful (0 votes)
2 views2 pages

Method Reference and Stream API

The document explains Method Reference and Stream API in Java 8, highlighting that method references allow referring to methods without invoking them, enhancing code readability. It details the four types of method references and describes the Stream API as a tool for processing collections efficiently, supporting operations like filtering and mapping. Additionally, it outlines the types of stream operations and provides examples of their usage.

Uploaded by

PRIYANKA SHINDE
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Method Reference and Stream API

The document explains Method Reference and Stream API in Java 8, highlighting that method references allow referring to methods without invoking them, enhancing code readability. It details the four types of method references and describes the Stream API as a tool for processing collections efficiently, supporting operations like filtering and mapping. Additionally, it outlines the types of stream operations and provides examples of their usage.

Uploaded by

PRIYANKA SHINDE
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Method Reference and Stream API in Java 8

Method Reference in Java 8


 Method reference is used to refer to a method without calling it.
 It is a short form of lambda expression.
 It improves code readability and reusability.
 The symbol '::' is used for method reference.
 It does not execute the method directly; it only refers to it.
 The method is executed when called through a functional interface or directly by name.
 There are four types of method references:
 1. Reference to a static method → ClassName::methodName
 2. Reference to an instance method → object::methodName
 3. Reference to an instance method of a class → ClassName::methodName
 4. Reference to a constructor → ClassName::new

 example

 [Link]([Link]::println);

Stream API in Java 8


Stream API is used to process collections of data in a simple and efficient way.
It helps to perform operations like filtering, mapping, sorting, and reducing.
It supports functional-style programming on collections.
Stream does not store data; it processes data from sources like lists, arrays, etc.
Stream operations are of two types:
1. Intermediate operations (e.g., filter(), map(), sorted()) – return a new stream.
2. Terminal operations (e.g., forEach(), collect(), count()) – produce the final result.
Stream can be created from collections using the stream() method.
It makes code shorter, cleaner, and easier to read.
It allows processing data in parallel to improve performance.

 common stream API


 filter() – filters elements based on a condition.

 map() – transforms elements.

 forEach() – iterates through elements.

 collect() – collects results into a list or set.



 List<Integer> numbers = [Link](1, 2, 3, 4, 5);
 [Link]()
 .filter(n -> n % 2 == 0)
 .forEach([Link]::println);

You might also like