0% found this document useful (0 votes)
3 views5 pages

Stream

The document explains the implementation and functionality of various functional interfaces in Java, such as Predicate, Consumer, and ToIntFunction, used in stream operations. It details how methods like filter, forEach, mapToInt, and sorted work with these interfaces to process data in streams. Additionally, it outlines stream creation methods, intermediate and terminal operations, and important concepts related to stream processing and sorting in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Stream

The document explains the implementation and functionality of various functional interfaces in Java, such as Predicate, Consumer, and ToIntFunction, used in stream operations. It details how methods like filter, forEach, mapToInt, and sorted work with these interfaces to process data in streams. Additionally, it outlines stream creation methods, intermediate and terminal operations, and important concepts related to stream processing and sorting in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

filter(i -> i > 10); // we are invoking

filter's argument is "Predicate" functional interface with


boolean test(T t) abstract method

Filter - predicate interfac


now we need to understand what must be the logic inside "filter()" method ?

Predicate's implementation

class PredicateImpl implements Predicate


{
public boolean test(Integer t)
{
if t>10
return true;
}
}

filter(Predicate ref)
{
traverse the stream
pass each and every number to "test" method

[Link](number);
if true store these numbers in a new Stream
finally return that stream

}
forEach([Link]::println); we are invoking

forEach() method's argument is "Consumer" functional interface with

void accept(T t) // abstract method


Performs this operation on the given argument.

implementation of Consumer Foreach = Consumer - ac


class ConsumerImpl implements Consumer
{
void accept(T t)
{
[Link](t);
this is because u have asked accept to print an element provided
[Link]::println
}
}

how forEach() method must have been designed

forEach(Consumer ref)
{
traverse through the stream
pass each and every element to "accept()" method like this:

[Link](element);

}
mapToInt(i -> i) we are invoking

since we are passing lambda expression, mapToInt() method must have functional interface as an
argument.

it's "ToIntFunction" with

M
int applyAsInt(T value) abstract method

how mapToInt must be working?

class ToIntFunctionImp implements ToIntFunction


{
int applyAsInt(Integer value)
{
converts value into "int"
and return
}
}

code inside "mapToInt()" method

mapToInt(ToIntFunction ref)
{
traverse the stream which has got numbers 12 and 20

invoke
[Link](12)
12 will come back as "int"
store it inside "IntStream"
[Link](20)
20 will come back as "int"
store it inside "IntStream"

finally return this "IntStream"


}
sorted((a,b)->[Link](a)) // we are invoking

sorted() method's argument is


Comparator functional interface which has got
int compare(String a,String b) abstract method

implementation of Comparator

class ComparatorImpl implements Comparator


{
public int compare(String a,String b)
{
return [Link](a);
}
}

sorted(Comparator ref)
{
traverse the stream
and pass 2 elements every time to "compare" method of Comparator implementation

[Link](first element,second element)


}
1. Stream Creation Methods
1. stream() → create stream from collection
2. [Link]() → create stream from fixed values
3. [Link]() → stream from array
4. [Link](a,b) → integers from a to b-1
5. [Link](a,b) → integers from a to b
6. [Link]() → infinite stream using
Supplier
2. Intermediate Operations (returns Stream)
1. filter(predicate) → select elements based on
condition
2. map(func) → transform element to another form
3. mapToInt(func) → convert object stream →
IntStream
4. mapToObj(func) → convert primitive stream →
object stream
5. flatMap(func) → one element → multiple
elements stream, then flatten
6. sorted() → ascending sorting
7. sorted(comparator) → custom sorting (reverse
etc.)
8. distinct() → removes duplicates (stateful)
9. skip(n) → skip first n elements
10. limit(n) → take only first n elements
11. peek(action) → debugging (see elements in
between)
12. boxed() → primitive stream → wrapper stream
(IntStream → Stream)
13. chars() → converts String → IntStream of unicode
values
3. Terminal Operations (ends stream + gives result/output)
1. forEach(action) → print / perform action on each
element
2. collect(collector) → convert stream →
List/Set/Map etc.
• [Link]()
• [Link]()
• [Link](key)
• [Link](predicate)
• [Link](keyMapper,
valueMapper, mergeFunction)
3. reduce(identity, accumulator) → combine all into
single value
4. reduce(accumulator) → returns Optional
5. count() → number of elements
6. sum() → sum of IntStream values
7. findFirst() → first element (Optional)
8. ifPresent(action) → run action only if Optional has
value
9. orElse(defaultValue) → return default if Optional
empty
4. Comparator / Sorting Methods Used
1. [Link]() → reverse sorting
2. compareTo() → natural order comparison
(String/Integer etc.)
5. Important Interview Keywords
1. Intermediate ops = lazy (execute only when
terminal op called)
2. Terminal ops = trigger execution
3. flatMap = used for converting nested stream →
single stream
4. distinct/sorted = stateful operations (need
memory/buffering)

From <[Link]

From <[Link]

You might also like