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

Java Stream API Cheat Sheet

The document provides various Java Stream operations on lists of strings and integers, including filtering, mapping, counting, and sorting. It also includes operations on a list of employee maps, such as filtering by designation, extracting salaries, and calculating total and average salaries. Each operation is demonstrated with code snippets that illustrate how to manipulate and analyze the data effectively.

Uploaded by

telugudummy106
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)
2 views3 pages

Java Stream API Cheat Sheet

The document provides various Java Stream operations on lists of strings and integers, including filtering, mapping, counting, and sorting. It also includes operations on a list of employee maps, such as filtering by designation, extracting salaries, and calculating total and average salaries. Each operation is demonstrated with code snippets that illustrate how to manipulate and analyze the data effectively.

Uploaded by

telugudummy106
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

List<String> strs=Arrays.

asList("Subham","Rakes","cHATBOT","sRS","Social Regitry","sRS");

Print all elements

[Link]().forEach([Link]::println);

Convert all to LowerCase

List<String> result=[Link]().map(x->[Link]()).collect([Link]());

Count no of elements

long result=[Link]().count();

Remove Duplicates

List<String> result=[Link]().distinct().collect([Link]());

Filter String Starting with S

List<String> result=[Link]().filter(x->[Link]("S")).collect([Link]());

Count Duplicates

Map<String, Long> result = [Link]()=.collect([Link]([Link](),


[Link]()));

Sort alphabetically

List<String> result= [Link]().sorted(String.CASE_INSENSITIVE_ORDER).collect([Link]());

find Longest String

Optional<String> result=[Link]().max([Link](x->[Link]()));

Join string

String result=[Link]().collect([Link](","));

Group by length

Map<Object, List<String>> result=[Link]().collect([Link](x->[Link]()));

Find first string length=6

Optional<String> result=[Link]().filter(x->[Link]()>6).findFirst();

Capitalize first letter of each string

List<String> result=[Link]().map(x-
>[Link](0,1).toUpperCase()+[Link](1).toLowerCase()).collect([Link]());
List<Integer> numbers=[Link](1,2,3,4,5,6,7,8,9,10,11,12);

Filter even numbers

List<Integer> result=[Link]().filter(x->x%2==0).collect([Link]());

Filter odd numbers

List<Integer> result=[Link]().filter(x->x%2!=0).collect([Link]());

Find sum of all numbers

long result=[Link]().mapToInt(x->[Link]()).sum();

Find Max Number

Optional<Integer> result=[Link]().max(Integer::compareTo));

Find Min Number

Optional<Integer> result=[Link]().min(Integer::compareTo);

Find Second Highest

long result=[Link]().sorted([Link]()).distinct().skip(1).findFirst().orElse(0);

Find Second Last

Long result=[Link]().sorted([Link]()).distinct().skip(1).findFirst().orElse(0);

Count how many numbers are greater than 5

long result=[Link]().filter(x->x>5).count();

Sum of all numbers greater than 5

long result=[Link]().filter(x->x>5).mapToInt(x->[Link]()).sum();

Multiply all numbers

long result=[Link]().reduce(1,(x,y)->x*y);

Check if any number is divisible by 7

boolean result=[Link]().anyMatch(x->x%7==0);

Partition into even and odd

Map<Boolean, List<Integer>> result = [Link]().collect([Link](n -> n % 2 == 0));


List<Map<String,Object>> emps=[Link](
[Link]("id","1","name","Subham","desg","Developer","sal",10000),
[Link]("id","2","name","Riju","desg","HR","sal",20000),
[Link]("id","3","name","Sankha","desg","Tester","sal",25000),
[Link]("id","4","name","xyz","desg","DBA","sal",20000),
[Link]("id","5","name","abc","desg","Developer","sal",40000));
Print all names

[Link]().map(emp -> [Link]("name")).forEach([Link]::println);

Filter all Developers

List<Map<String, Object>> result=[Link]().filter(x-


>"Developer".equals([Link]("desg"))).collect([Link]());

Extract salaries as a list

List<Object> result=[Link]().map(x->[Link]("sal")).collect([Link]());

Total salary of all employees

long result=[Link]().mapToInt(x->(Integer)[Link]("sal")).sum();

Group employees by designation

Map<String, List<Map<String, Object>>> result=[Link]().collect([Link](x-


>(String)[Link]("desg")));

Get employee with max salary

Optional<Map<String, Object>> result=[Link]().max([Link](x->(Integer)[Link]("sal")));

Sort employees by salary

List<Map<String, Object>> result=[Link]().sorted([Link](x-


>(Integer)[Link]("sal"))).collect([Link]());

Average salary

double result=[Link]().mapToInt(x->(Integer) [Link]("sal")).average().orElse(0);

Names of employees with salary > 20,000

List<Object> result=[Link]().filter(x->(Integer)[Link]("sal")>20000).map(x-
>[Link]("name")).collect([Link]());

Check if any employee is HR

boolean result=[Link]().anyMatch(x->[Link]("desg").equals("HR"));

You might also like