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

Java 8 Stream API Practice With Real-World Examples

Uploaded by

efunkoya
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 views44 pages

Java 8 Stream API Practice With Real-World Examples

Uploaded by

efunkoya
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

public class Employee {

private String name;

private int age;

private double salary;

private String gender;

public Employee(String name, int age, double salary, String gender) {

[Link] = name;

[Link] = age;

[Link] = salary;

[Link] = gender;

public String getName() { return name; }

public int getAge() { return age; }

public double getSalary() { return salary; }

public String getGender() { return gender; }

@Override

public String toString() {

return "Employee{" +

"name='" + name + '\'' +

", age=" + age +

", salary=" + salary +

", gender='" + gender + '\'' +

'}';

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

public class EmployeeAdder {

public static List<Employee> addDetails() {

List<Employee> list = new ArrayList<>();

[Link](new Employee("Anna", 27, 50000.0, "Male"));

[Link](new Employee("Employee 2", 27, 51000.0, "Female"));

[Link](new Employee("Bob", 27, 52000.0, "Male"));

[Link](new Employee("Smith 4", 28, 53000.0, "Female"));

[Link](new Employee("Employee 5", 29, 53000.0, "Male"));

[Link](new Employee("Employee 6", 30, 55000.0, "Female"));

[Link](new Employee("Smith 7", 31, 56000.0, "Male"));

[Link](new Employee("Employee 8", 32, 57000.0, "Female"));

[Link](new Employee("Employee 9", 35, 58000.0, "Male"));

[Link](new Employee("Employee 10", 35, 59000.0, "Female"));

return list;

PROGRAM 1 — Female Employees


import [Link].*;

public class Program01_FemaleEmployees {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.filter(e -> [Link]().equals("Female"))


.forEach([Link]::println);

PROGRAM 2 — Age > 30


import [Link].*;

public class Program02_AgeGreaterThan30 {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.filter(e -> [Link]() > 30)

.forEach([Link]::println);

PROGRAM 3 — Salary > 50,000


import [Link].*;

public class Program03_SalaryGreaterThan50K {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.filter(e -> [Link]() > 50000)

.forEach([Link]::println);

}
PROGRAM 4 — List of Employee Names
import [Link].*;

public class Program04_EmployeeNames {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.map(Employee::getName)

.forEach([Link]::println);

PROGRAM 5 — Average Salary


import [Link].*;

public class Program05_AverageSalary {

public static void main(String[] args) {

List<Employee> list = [Link]();

double avg = [Link]()

.mapToDouble(Employee::getSalary)

.average()

.orElse(0);

[Link]("Average Salary = " + avg);

}
PROGRAM 6 — Maximum Salary
import [Link].*;

public class Program06_MaxSalary {

public static void main(String[] args) {

List<Employee> list = [Link]();

double max = [Link]()

.mapToDouble(Employee::getSalary)

.max()

.orElse(0);

[Link]("Max Salary = " + max);

PROGRAM 7 — Group by Gender


import [Link].*;

import [Link];

public class Program07_GroupByGender {

public static void main(String[] args) {

List<Employee> list = [Link]();

Map<String, List<Employee>> map =

[Link]().collect([Link](Employee::getGender));

[Link]((gender, employees) -> {

[Link](gender + " -> " + employees);


});

PROGRAM 8 — Count Male Employees


import [Link].*;

public class Program08_CountMaleEmployees {

public static void main(String[] args) {

List<Employee> list = [Link]();

long count = [Link]()

.filter(e -> [Link]().equals("Male"))

.count();

[Link]("Male Employees = " + count);

PROGRAM 9 — Sum of All Salaries


import [Link].*;

public class Program09_SumOfSalaries {

public static void main(String[] args) {

List<Employee> list = [Link]();

double sum = [Link]()

.mapToDouble(Employee::getSalary)

.sum();
[Link]("Total Salary Sum = " + sum);

PROGRAM 10 — Sort by Name


import [Link].*;

import [Link];

public class Program10_SortByName {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.sorted([Link](Employee::getName))

.forEach([Link]::println);

PROGRAM 11 — Sort by Age (Ascending)


import [Link].*;

public class Program11_SortByAge {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.sorted([Link](Employee::getAge))

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

PROGRAM 12 — Sort by Salary (Descending)


import [Link].*;

public class Program12_SortBySalaryDesc {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.sorted([Link](Employee::getSalary).reversed())

.forEach([Link]::println);

PROGRAM 13 — Oldest Employee


import [Link].*;

public class Program13_OldestEmployee {

public static void main(String[] args) {

List<Employee> list = [Link]();

Employee emp = [Link]()

.max([Link](Employee::getAge))

.orElse(null);

[Link](emp);

}
}

PROGRAM 14 — Group Employees by Age Range


import [Link].*;

import [Link];

public class Program14_GroupByAgeRange {

public static void main(String[] args) {

List<Employee> list = [Link]();

Map<String, List<Employee>> result =

[Link]()

.collect([Link](e -> {

int age = [Link]();

if (age >= 20 && age <= 30) return "20-30";

else if (age >= 31 && age <= 40) return "31-40";

else return "40+";

}));

[Link]((range, employees) ->

[Link](range + " -> " + employees));

PROGRAM 15 — Employees Age = 35


import [Link].*;

public class Program15_AgeEquals35 {

public static void main(String[] args) {


List<Employee> list = [Link]();

[Link]()

.filter(e -> [Link]() == 35)

.forEach([Link]::println);

PROGRAM 16 — Sum of Salaries by Gender


import [Link].*;

import [Link];

public class Program16_SalarySumByGender {

public static void main(String[] args) {

List<Employee> list = [Link]();

Map<String, Double> result =

[Link]()

.collect([Link](

Employee::getGender,

[Link](Employee::getSalary)

));

[Link]((gender, sum) ->

[Link](gender + " = " + sum));

}
PROGRAM 17 — Names Starting With 'E'
import [Link].*;

public class Program17_NamesStartWithE {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.filter(e -> [Link]().startsWith("E"))

.forEach([Link]::println);

PROGRAM 18 — Average Salary by Gender


import [Link].*;

import [Link];

public class Program18_AvgSalaryByGender {

public static void main(String[] args) {

List<Employee> list = [Link]();

Map<String, Double> avg =

[Link]()

.collect([Link](

Employee::getGender,

[Link](Employee::getSalary)

));

[Link]((gender, val) ->


[Link](gender + " = " + val));

PROGRAM 19 — Top N Highest Paid Employees (Top 5)


import [Link].*;

public class Program19_Top5HighestPaid {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.sorted([Link](Employee::getSalary).reversed())

.limit(5)

.forEach([Link]::println);

PROGRAM 20 — Distinct Ages


import [Link].*;

import [Link];

public class Program20_DistinctAges {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.map(Employee::getAge)

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

PROGRAM 21 — Three Lowest-Paid Employees


import [Link].*;

public class Program21_ThreeLowestPaid {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.sorted([Link](Employee::getSalary))

.limit(3)

.forEach(e -> [Link]([Link]()));

PROGRAM 22 — Sort by Name Length


import [Link].*;

public class Program22_SortByNameLength {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.sorted([Link](e -> [Link]().length()))

.forEach([Link]::println);

}
}

PROGRAM 23 — Group by Age Ranges (20–29, 30–39, 40+)


import [Link].*;

import [Link];

public class Program23_AgeRangeGrouping {

public static void main(String[] args) {

List<Employee> list = [Link]();

Map<String, List<Employee>> map =

[Link]().collect([Link](e -> {

int age = [Link]();

if (age >= 20 && age <= 29) return "20-29";

if (age >= 30 && age <= 39) return "30-39";

return "40+";

}));

[Link]((range, empList) -> [Link](range + " -> " + empList));

PROGRAM 24 — Average Salary of Employees ≤ 30


import [Link].*;

public class Program24_AvgSalaryBelow30 {

public static void main(String[] args) {

List<Employee> list = [Link]();


double avg = [Link]()

.filter(e -> [Link]() <= 30)

.mapToDouble(Employee::getSalary)

.average()

.orElse(0);

[Link]("Average Salary (<=30): " + avg);

PROGRAM 25 — Male Employees Salary ≥ 60000


import [Link].*;

public class Program25_MaleSalaryAbove60000 {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.filter(e -> [Link]().equals("Male") && [Link]() >= 60000)

.map(Employee::getName)

.forEach([Link]::println);

PROGRAM 26 — Youngest Female Employee


import [Link].*;

public class Program26_YoungestFemale {

public static void main(String[] args) {


List<Employee> list = [Link]();

Employee youngest = [Link]()

.filter(e -> [Link]().equals("Female"))

.min([Link](Employee::getAge))

.orElse(null);

[Link](youngest);

PROGRAM 27 — Reverse Order of Names


import [Link].*;

import [Link];

public class Program27_ReverseNames {

public static void main(String[] args) {

List<Employee> list = [Link]();

List<String> names = [Link]()

.map(Employee::getName)

.collect([Link]());

[Link](names);

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

}
PROGRAM 28 — Highest Salary Among Female Employees
import [Link].*;

public class Program28_HighestSalaryFemale {

public static void main(String[] args) {

List<Employee> list = [Link]();

Employee result = [Link]()

.filter(e -> [Link]().equals("Female"))

.max([Link](Employee::getSalary))

.orElse(null);

[Link](result);

PROGRAM 29 — Group by Gender then by Age (Multi-level Map)


import [Link].*;

import [Link];

public class Program29_GroupByGenderAndAge {

public static void main(String[] args) {

List<Employee> list = [Link]();

Map<String, Map<Integer, List<Employee>>> map =

[Link]()

.collect([Link](

Employee::getGender,

[Link](Employee::getAge)
));

[Link]((gender, ageMap) -> {

[Link](gender + ": " + ageMap);

});

PROGRAM 30 — Sum of Salaries Where Name Contains "Smith"


import [Link].*;

public class Program30_SmithSalarySum {

public static void main(String[] args) {

List<Employee> list = [Link]();

double sum = [Link]()

.filter(e -> [Link]().contains("Smith"))

.mapToDouble(Employee::getSalary)

.sum();

[Link]("Smith Salary Sum = " + sum);

PROGRAM 31 — Age 30–40 & Salary 50k–60k


import [Link].*;

public class Program31_Age30to40SalaryRange {

public static void main(String[] args) {


List<Employee> list = [Link]();

[Link]()

.filter(e -> [Link]() >= 30 && [Link]() <= 40)

.filter(e -> [Link]() >= 50000 && [Link]() <= 60000)

.forEach([Link]::println);

PROGRAM 32 — Total Number of Employees


import [Link].*;

public class Program32_TotalEmployees {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]("Total Employees = " + [Link]());

PROGRAM 33 — Most Common Age


import [Link].*;

import [Link];

public class Program33_MostCommonAge {

public static void main(String[] args) {

List<Employee> list = [Link]();

int mostCommon =
[Link]()

.collect([Link](Employee::getAge, [Link]()))

.entrySet()

.stream()

.max([Link]())

.get()

.getKey();

[Link]("Most Common Age = " + mostCommon);

PROGRAM 34 — Median Salary


import [Link].*;

public class Program34_MedianSalary {

public static void main(String[] args) {

List<Employee> list = [Link]();

List<Employee> sorted = [Link]()

.sorted([Link](Employee::getSalary))

.toList();

int size = [Link]();

if (size % 2 == 0) {

double mid = ([Link](size/2 - 1).getSalary() +

[Link](size/2).getSalary()) / 2.0;

[Link]("Median Salary = " + mid);

} else {
[Link]("Median Salary = " + [Link](size/2).getSalary());

PROGRAM 35 — Group by Age + Count


import [Link].*;

import [Link];

public class Program35_CountByAge {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.collect([Link](Employee::getAge, [Link]()))

.forEach((age, count) -> [Link](age + " -> " + count));

PROGRAM 36 — Employee With Longest Name


import [Link].*;

public class Program36_LongestName {

public static void main(String[] args) {

List<Employee> list = [Link]();

Employee result = [Link]()

.max([Link](e -> [Link]().length()))


.orElse(null);

[Link](result);

PROGRAM 37 — Sum of Salaries by Age


import [Link].*;

import [Link];

public class Program37_SalarySumByAge {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.collect([Link](Employee::getAge,

[Link](Employee::getSalary)))

.forEach((age, sum) -> [Link](age + " -> " + sum));

PROGRAM 38 — Sort by Age ASC then Salary DESC


import [Link].*;

public class Program38_SortAgeThenSalary {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()
.sorted([Link](Employee::getAge)

.thenComparing([Link](Employee::getSalary).reversed()))

.forEach([Link]::println);

PROGRAM 39 — Names With More Than One Word


import [Link].*;

public class Program39_NamesWithMultipleWords {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.filter(e -> [Link]().trim().contains(" "))

.forEach([Link]::println);

PROGRAM 40 — Two Highest Paid Female Employees


import [Link].*;

public class Program40_Top2FemaleHighestPaid {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.filter(e -> [Link]().equals("Female"))

.sorted([Link](Employee::getSalary).reversed())
.limit(2)

.forEach([Link]::println);

PROGRAM 41 — Highest Salary in Each Gender


import [Link].*;

import [Link];

public class Program41_HighestSalaryEachGender {

public static void main(String[] args) {

List<Employee> list = [Link]();

Map<String, Employee> map =

[Link]()

.collect([Link](

Employee::getGender,

e -> e,

(e1, e2) -> [Link]() >= [Link]() ? e1 : e2

));

[Link]((gender, emp) -> [Link](gender + " -> " + emp));

PROGRAM 42 — Employees With Unique Names


import [Link].*;

import [Link];
public class Program42_UniqueNames {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.collect([Link](Employee::getName, [Link]()))

.entrySet()

.stream()

.filter(e -> [Link]() == 1)

.forEach(e -> [Link]([Link]()));

PROGRAM 43 — Names Converted to Uppercase


import [Link].*;

public class Program43_NamesUppercase {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.map(e -> [Link]().toUpperCase())

.forEach([Link]::println);

PROGRAM 44 — Salary Range (Min-Max) for Each Age


import [Link].*;

import [Link];
public class Program44_SalaryRangeByAge {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.collect([Link](

Employee::getAge,

[Link]([Link](), empList -> {

double min = [Link]()

.mapToDouble(Employee::getSalary).min().orElse(0);

double max = [Link]()

.mapToDouble(Employee::getSalary).max().orElse(0);

Map<String, Double> map = new HashMap<>();

[Link]("min", min);

[Link]("max", max);

return map;

})

))

.forEach((age, range) ->

[Link]("Age " + age + " → Min: " + [Link]("min") +

" | Max: " + [Link]("max")));

}
PROGRAM 45 — Names Starting with 'E'
import [Link].*;

public class Program45_NameStartsWithE {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.filter(e -> [Link]().startsWith("E"))

.forEach([Link]::println);

PROGRAM 46 — Unique Salaries by Age


import [Link].*;

import [Link];

public class Program46_UniqueSalariesByAge {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.collect([Link](

Employee::getAge,

[Link](Employee::getSalary, [Link]())

))

.forEach((age, salaries) ->

[Link](age + " -> " + salaries));

}
PROGRAM 47 — Employees Sharing Same Salary
import [Link].*;

import [Link];

public class Program47_SameSalaryEmployees {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.collect([Link](Employee::getSalary))

.entrySet()

.stream()

.filter(e -> [Link]().size() > 1)

.forEach(e -> {

[Link]("Salary: " + [Link]());

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

});

PROGRAM 48 — Shortest Name (Male Only)


import [Link].*;

public class Program48_ShortestMaleName {

public static void main(String[] args) {

List<Employee> list = [Link]();

Employee result = [Link]()

.filter(e -> [Link]().equals("Male"))


.min([Link](e -> [Link]().length()))

.orElse(null);

[Link](result);

PROGRAM 49 — Most Common Salary


import [Link].*;

import [Link];

public class Program49_MostCommonSalary {

public static void main(String[] args) {

List<Employee> list = [Link]();

double result =

[Link]()

.collect([Link](Employee::getSalary, [Link]()))

.entrySet()

.stream()

.max([Link]())

.get()

.getKey();

[Link]("Most Common Salary = " + result);

}
PROGRAM 50 — Oldest Employee With Lowest Salary
import [Link].*;

public class Program50_OldestEmployeeLowestSalary {

public static void main(String[] args) {

List<Employee> list = [Link]();

int maxAge = [Link]()

.mapToInt(Employee::getAge)

.max()

.orElse(0);

Employee result = [Link]()

.filter(e -> [Link]() == maxAge)

.min([Link](Employee::getSalary))

.orElse(null);

[Link](result);

PROGRAM 51 — Highest Salary in Each Gender


import [Link].*;

import [Link];

public class Program51_HighestSalaryEachGender {

public static void main(String[] args) {

List<Employee> list = [Link]();


Map<String, Employee> map =

[Link]()

.collect([Link](

Employee::getGender,

e -> e,

(a, b) -> [Link]() >= [Link]() ? a : b

));

[Link]((g, emp) -> [Link](g + " -> " + emp));

PROGRAM 52 — Unique Names


import [Link].*;

import [Link];

public class Program52_UniqueNames {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.collect([Link](Employee::getName, [Link]()))

.entrySet()

.stream()

.filter(e -> [Link]() == 1)

.forEach(e -> [Link]([Link]()));

}
PROGRAM 53 — Names in Uppercase
import [Link].*;

public class Program53_NamesUppercase {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.map(e -> [Link]().toUpperCase())

.forEach([Link]::println);

PROGRAM 54 — Salary Min-Max by Age


import [Link].*;

import [Link];

public class Program54_SalaryRangeByAge {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.collect([Link](

Employee::getAge,

[Link]([Link](), empList -> {

Map<String, Double> map = new HashMap<>();

[Link]("min", [Link]().mapToDouble(Employee::getSalary).min().orElse(0));

[Link]("max", [Link]().mapToDouble(Employee::getSalary).max().orElse(0));

return map;
})

))

.forEach((age, salRange) ->

[Link](age + " -> " + salRange));

PROGRAM 55 — Names Starting With 'E'


import [Link].*;

public class Program55_NameStartsWithE {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.filter(e -> [Link]().startsWith("E"))

.forEach([Link]::println);

PROGRAM 56 — Unique Salaries for Each Age


import [Link].*;

import [Link];

public class Program56_UniqueSalariesByAge {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()
.collect([Link](

Employee::getAge,

[Link](Employee::getSalary, [Link]())

))

.forEach((age, salSet) -> [Link](age + " -> " + salSet));

PROGRAM 57 — Employees Having Same Salary


import [Link].*;

import [Link];

public class Program57_SameSalaryEmployees {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.collect([Link](Employee::getSalary))

.entrySet()

.stream()

.filter(e -> [Link]().size() > 1)

.forEach(e -> {

[Link]("Salary: " + [Link]());

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

});

}
PROGRAM 58 — Shortest Male Employee Name
import [Link].*;

public class Program58_ShortestMaleName {

public static void main(String[] args) {

List<Employee> list = [Link]();

Employee result = [Link]()

.filter(e -> [Link]().equals("Male"))

.min([Link](e -> [Link]().length()))

.orElse(null);

[Link](result);

PROGRAM 59 — Most Common Salary


import [Link].*;

import [Link];

public class Program59_MostCommonSalary {

public static void main(String[] args) {

List<Employee> list = [Link]();

double result =

[Link]()

.collect([Link](Employee::getSalary, [Link]()))

.entrySet()

.stream()
.max([Link]())

.get()

.getKey();

[Link]("Most Common Salary = " + result);

PROGRAM 60 — Oldest Employee With Lowest Salary


import [Link].*;

public class Program60_OldestEmployeeLowestSalary {

public static void main(String[] args) {

List<Employee> list = [Link]();

int maxAge = [Link]()

.mapToInt(Employee::getAge)

.max()

.orElse(0);

Employee result = [Link]()

.filter(e -> [Link]() == maxAge)

.min([Link](Employee::getSalary))

.orElse(null);

[Link](result);

}
PROGRAM 61 — Most Common Age
import [Link].*;

import [Link];

public class Program61_MostCommonAge {

public static void main(String[] args) {

List<Employee> list = [Link]();

int result =

[Link]()

.collect([Link](Employee::getAge, [Link]()))

.entrySet()

.stream()

.max([Link]())

.get()

.getKey();

[Link]("Most Common Age = " + result);

PROGRAM 62 — Longest Name


import [Link].*;

public class Program62_LongestName {

public static void main(String[] args) {

List<Employee> list = [Link]();

Employee result = [Link]()


.max([Link](e -> [Link]().length()))

.orElse(null);

[Link](result);

PROGRAM 63 — Palindromic Names ("Anna", "Bob")


import [Link].*;

public class Program63_PalindromeNames {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.filter(e -> {

String n = [Link]().toLowerCase();

return [Link](new StringBuilder(n).reverse().toString());

})

.forEach([Link]::println);

PROGRAM 64 — Sum of Salaries of Employees With Odd Ages


import [Link].*;

public class Program64_SalarySumOddAges {

public static void main(String[] args) {

List<Employee> list = [Link]();


double sum = [Link]()

.filter(e -> [Link]() % 2 != 0)

.mapToDouble(Employee::getSalary)

.sum();

[Link]("Sum = " + sum);

PROGRAM 65 — Highest Salary Employee Whose Name Contains "Smith"


import [Link].*;

public class Program65_HighestSalarySmith {

public static void main(String[] args) {

List<Employee> list = [Link]();

Employee result = [Link]()

.filter(e -> [Link]().contains("Smith"))

.max([Link](Employee::getSalary))

.orElse(null);

[Link](result);

PROGRAM 66 — Group by First Letter of Names


import [Link].*;

import [Link];
public class Program66_GroupByFirstLetter {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.collect([Link](e -> [Link]().charAt(0)))

.forEach((ch, group) -> [Link](ch + " -> " + group));

PROGRAM 67 — Shortest Name in Entire List


import [Link].*;

public class Program67_ShortestName {

public static void main(String[] args) {

List<Employee> list = [Link]();

Employee result = [Link]()

.min([Link](e -> [Link]().length()))

.orElse(null);

[Link](result);

PROGRAM 68 — Avg Salary of Names Starting With 'E'


import [Link].*;

import [Link];
public class Program68_AvgSalaryStartWithE {

public static void main(String[] args) {

List<Employee> list = [Link]();

double avg =

[Link]()

.filter(e -> [Link]().startsWith("E"))

.mapToDouble(Employee::getSalary)

.average()

.orElse(0);

[Link]("Average Salary = " + avg);

PROGRAM 69 — Age Between 25 and 35


import [Link].*;

public class Program69_Age25To35 {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.filter(e -> [Link]() >= 25 && [Link]() <= 35)

.forEach([Link]::println);

}
PROGRAM 70 — Group by First Two Letters
import [Link].*;

import [Link];

public class Program70_GroupByFirstTwoLetters {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.collect([Link](e -> [Link]().substring(0, 2)))

.forEach((prefix, group) -> [Link](prefix + " -> " + group));

PROGRAM 71 — Longest Name Among Employees With Salary < 70,000


import [Link].*;

public class Program71_LongestNameSalaryBelow70K {

public static void main(String[] args) {

List<Employee> list = [Link]();

Employee result = [Link]()

.filter(e -> [Link]() < 70000)

.max([Link](e -> [Link]().length()))

.orElse(null);

[Link](result);

}
PROGRAM 72 — Avg Salary of Names Ending With "son"
import [Link].*;

public class Program72_AvgSalaryEndsWithSon {

public static void main(String[] args) {

List<Employee> list = [Link]();

double avg = [Link]()

.filter(e -> [Link]().toLowerCase().endsWith("son"))

.mapToDouble(Employee::getSalary)

.average()

.orElse(0);

[Link]("Average Salary = " + avg);

PROGRAM 73 — Group by Number of Words in Name


import [Link].*;

import [Link];

public class Program73_GroupByWordCount {

public static void main(String[] args) {

List<Employee> list = [Link]();

[Link]()

.collect([Link](

e -> [Link]().trim().split("\\s+").length

))
.forEach((words, group) -> [Link](words + " words -> " + group));

PROGRAM 74 — Avg Salary of Names Containing Both 'A' and 'E'


import [Link].*;

public class Program74_AvgSalaryContainsAandE {

public static void main(String[] args) {

List<Employee> list = [Link]();

double avg = [Link]()

.map(e -> [Link]().toLowerCase())

.filter(name -> [Link]("a") && [Link]("e"))

.mapToDouble(n -> {

for (Employee emp : [Link]()) {

if ([Link]().equalsIgnoreCase(n)) {

return [Link]();

return 0;

})

.average()

.orElse(0);

[Link]("Average Salary = " + avg);

You might also like