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

(3 Pages) Java 8 Stream API Cheat Sheet

The document provides a series of Java Stream API operations on lists of strings and integers, including printing elements, converting to lower case, counting, removing duplicates, filtering, sorting, and aggregating data. It also includes operations on a list of employee maps, such as filtering by designation, extracting salaries, grouping by designation, and calculating averages. Overall, it demonstrates various functional programming techniques in Java for data manipulation and analysis.

Uploaded by

Mca Mgl
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)
1 views3 pages

(3 Pages) Java 8 Stream API Cheat Sheet

The document provides a series of Java Stream API operations on lists of strings and integers, including printing elements, converting to lower case, counting, removing duplicates, filtering, sorting, and aggregating data. It also includes operations on a list of employee maps, such as filtering by designation, extracting salaries, grouping by designation, and calculating averages. Overall, it demonstrates various functional programming techniques in Java for data manipulation and analysis.

Uploaded by

Mca Mgl
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
You are on page 1/ 3

List<String> strs=Arrays.

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

Print all elements

strs.stream().forEach(System.out::println);

Convert all to LowerCase

List<String> result=strs.stream().map(x->x.toLowerCase()).collect(Collectors.toList());

Count no of elements

long result=strs.stream().count();

Remove Duplicates

List<String> result=strs.stream().distinct().collect(Collectors.toList());

Filter String Starting with S

List<String> result=strs.stream().filter(x->x.startsWith("S")).collect(Collectors.toList());

Count Duplicates

Map<String, Long> result = strs.stream()=.collect(Collectors.groupingBy(Function.identity(),


Collectors.counting()));

Sort alphabetically

List<String> result= strs.stream().sorted(String.CASE_INSENSITIVE_ORDER).collect(Collectors.toList());

find Longest String

Optional<String> result=strs.stream().max(Comparator.comparingInt(x->x.length()));

Join string

String result=strs.stream().collect(Collectors.joining(","));

Group by length

Map<Object, List<String>> result=strs.stream().collect(Collectors.groupingBy(x->x.length()));

Find first string length=6

Optional<String> result=strs.stream().filter(x->x.length()>6).findFirst();

Capitalize first letter of each string

List<String> result=strs.stream().map(x-
>x.substring(0,1).toUpperCase()+x.substring(1).toLowerCase()).collect(Collectors.toList());
List<Integer> numbers=Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12);

Filter even numbers

List<Integer> result=numbers.stream().filter(x->x%2==0).collect(Collectors.toList());

Filter odd numbers

List<Integer> result=numbers.stream().filter(x->x%2!=0).collect(Collectors.toList());

Find sum of all numbers

long result=numbers.stream().mapToInt(x->x.intValue()).sum();

Find Max Number

Optional<Integer> result=numbers.stream().max(Integer::compareTo));

Find Min Number

Optional<Integer> result=numbers.stream().min(Integer::compareTo);

Find Second Highest

long result=numbers.stream().sorted(Comparator.reverseOrder()).distinct().skip(1).findFirst().orElse(0);

Find Second Last

Long result=numbers.stream().sorted(Comparator.naturalOrder()).distinct().skip(1).findFirst().orElse(0);

Count how many numbers are greater than 5

long result=numbers.stream().filter(x->x>5).count();

Sum of all numbers greater than 5

long result=numbers.stream().filter(x->x>5).mapToInt(x->x.intValue()).sum();

Multiply all numbers

long result=numbers.stream().reduce(1,(x,y)->x*y);

Check if any number is divisible by 7

boolean result=numbers.stream().anyMatch(x->x%7==0);

Partition into even and odd

Map<Boolean, List<Integer>> result = numbers.stream().collect(Collectors.partitioningBy(n -> n % 2 == 0));


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

emps.stream().map(emp -> emp.get("name")).forEach(System.out::println);

Filter all Developers

List<Map<String, Object>> result=emps.stream().filter(x-


>"Developer".equals(x.get("desg"))).collect(Collectors.toList());

Extract salaries as a list

List<Object> result=emps.stream().map(x->x.get("sal")).collect(Collectors.toList());

Total salary of all employees

long result=emps.stream().mapToInt(x->(Integer)x.get("sal")).sum();

Group employees by designation

Map<String, List<Map<String, Object>>> result=emps.stream().collect(Collectors.groupingBy(x-


>(String)x.get("desg")));

Get employee with max salary

Optional<Map<String, Object>> result=emps.stream().max(Comparator.comparingInt(x->(Integer)x.get("sal")));

Sort employees by salary

List<Map<String, Object>> result=emps.stream().sorted(Comparator.comparingInt(x-


>(Integer)x.get("sal"))).collect(Collectors.toList());

Average salary

double result=emps.stream().mapToInt(x->(Integer) x.get("sal")).average().orElse(0);

Names of employees with salary > 20,000

List<Object> result=emps.stream().filter(x->(Integer)x.get("sal")>20000).map(x-
>x.get("name")).collect(Collectors.toList());

Check if any employee is HR

boolean result=emps.stream().anyMatch(x->x.get("desg").equals("HR"));

You might also like