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

Streams API Program in Java

The document provides examples of using Java's Stream API to perform various operations on lists of integers, including filtering even numbers, squaring values, sorting, and counting elements. It demonstrates both sequential and parallel stream operations, as well as terminal operations like collecting results and checking conditions. The examples highlight the versatility and efficiency of the Stream API in processing collections.

Uploaded by

kapooranurag18
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 views8 pages

Streams API Program in Java

The document provides examples of using Java's Stream API to perform various operations on lists of integers, including filtering even numbers, squaring values, sorting, and counting elements. It demonstrates both sequential and parallel stream operations, as well as terminal operations like collecting results and checking conditions. The examples highlight the versatility and efficiency of the Stream API in processing collections.

Uploaded by

kapooranurag18
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

import [Link].

*;

import [Link].*;

public class StreamAPIExample

public static void main(String[] args)

// Creating List

List<Integer> numbers = [Link](10, 25, 30, 45, 50, 60, 75);

[Link]("Original List:");

[Link](numbers);

// 1. Filter even numbers

[Link]("\nEven Numbers:");

[Link]()

.filter(n -> n % 2 == 0)

.forEach([Link]::println);

// 2. Square of numbers using map()

[Link]("\nSquare of Numbers:");

[Link]()

.map(n -> n * n)

.forEach([Link]::println);

// 3. Sorted numbers

[Link]("\nSorted List:");

[Link]()
.sorted()

.forEach([Link]::println);

// 4. Numbers greater than 40 and store in new list

List<Integer> result =

[Link]()

.filter(n -> n > 40)

.collect([Link]());

[Link]("\nNumbers greater than 40:");

[Link](result);

// 5. Count numbers greater than 30

long count =

[Link]()

.filter(n -> n > 30)

.count();

[Link]("\nCount of numbers greater than 30:");

[Link](count);

// 6. Find Maximum number

int max =

[Link]()

.max(Integer::compare)

.get();

[Link]("\nMaximum Number:");
[Link](max);

// 7. Find Minimum number

int min =

[Link]()

.min(Integer::compare)

.get();

[Link]("\nMinimum Number:");

[Link](min);

// 8. Parallel Stream example

[Link]("\nParallel Stream (multiply by 2):");

[Link]()

.map(n -> n * 2)

.forEach([Link]::println);

}
Output:

Original List:
[10, 25, 30, 45, 50, 60, 75]

Even Numbers:
10
30
50
60

Square of Numbers:
100
625
900
2025
2500
3600
5625
Sorted List:
10
25
30
45
50
60
75

Numbers greater than 40:


[45, 50, 60, 75]

Count of numbers greater than 30:


4

Maximum Number:
75

Minimum Number:
10

Parallel Stream (multiply by 2):


100
90
150
120
50
60
20

2. all stream api operations


import [Link].*;
import [Link].*;

public class StreamAPIComplete


{
public static void main(String[] args)
{
List<Integer> numbers = [Link](10, 15, 20, 25, 30, 35,
40, 45, 50);

[Link]("Original List:");
[Link](numbers);

// 1. filter() – Intermediate Operation


[Link]("\nEven Numbers:");
[Link]()
.filter(n -> n % 2 == 0)
.forEach([Link]::println);

// 2. map() – Intermediate Operation


[Link]("\nSquare of Numbers:");
[Link]()
.map(n -> n * n)
.forEach([Link]::println);
// 3. sorted() – Intermediate Operation
[Link]("\nSorted Numbers:");
[Link]()
.sorted()
.forEach([Link]::println);

// 4. distinct() – Intermediate Operation


List<Integer> duplicateList = [Link](10,20,20,30,30,40);
[Link]("\nDistinct Elements:");
[Link]()
.distinct()
.forEach([Link]::println);

// 5. limit() – Intermediate Operation


[Link]("\nFirst 3 Elements:");
[Link]()
.limit(3)
.forEach([Link]::println);

// 6. skip() – Intermediate Operation


[Link]("\nAfter skipping first 3:");
[Link]()
.skip(3)
.forEach([Link]::println);

// 7. collect() – Terminal Operation


List<Integer> result =
[Link]()
.filter(n -> n > 30)
.collect([Link]());

[Link]("\nNumbers greater than 30:");


[Link](result);

// 8. count() – Terminal Operation


long count =
[Link]()
.filter(n -> n > 25)
.count();

[Link]("\nCount >25:");
[Link](count);

// 9. min() – Terminal Operation


int min =
[Link]()
.min(Integer::compare)
.get();

[Link]("\nMinimum Value:");
[Link](min);

// 10. max() – Terminal Operation


int max =
[Link]()
.max(Integer::compare)
.get();

[Link]("\nMaximum Value:");
[Link](max);

// 11. reduce() – Terminal Operation


int sum =
[Link]()
.reduce(0, (a,b) -> a+b);

[Link]("\nSum of all elements:");


[Link](sum);

// 12. anyMatch() – Terminal Operation


boolean check =
[Link]()
.anyMatch(n -> n > 45);

[Link]("\nAny number >45:");


[Link](check);

// 13. allMatch() – Terminal Operation


boolean allPositive =
[Link]()
.allMatch(n -> n > 0);

[Link]("\nAll numbers positive:");


[Link](allPositive);

// 14. forEach() – Terminal Operation


[Link]("\nPrinting using forEach:");
[Link]()
.forEach(n -> [Link](n + " "));

// 15. Parallel Stream


[Link]("\n\nParallel Stream:");
[Link]()
.forEach([Link]::println);
}
}
Output:
Original List:
[10, 15, 20, 25, 30, 35, 40, 45, 50]

Even Numbers:
10
20
30
40
50

Square of Numbers:
100
225
400
625
900
1225
1600
2025
2500

Sorted Numbers:
10
15
20
25
30
35
40
45
50

Distinct Elements:
10
20
30
40

First 3 Elements:
10
15
20

After skipping first 3:


25
30
35
40
45
50

Numbers greater than 30:


[35, 40, 45, 50]

Count >25:
5

Minimum Value:
10

Maximum Value:
50

Sum of all elements:


270

Any number >45:


true

All numbers positive:


true
Printing using forEach:
10 15 20 25 30 35 40 45 50

Parallel Stream:
30
35
45
50
40
20
25
10
15

You might also like