|
| 1 | +package com.java8; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.util.stream.Collectors; |
| 5 | +import java.util.stream.Stream; |
| 6 | + |
| 7 | +public class Java8MethodCheatSheet { |
| 8 | + |
| 9 | + public static void main(String[] args) { |
| 10 | + |
| 11 | + |
| 12 | + List<Employee> employees = EmployeeDataBase.getAllEmployees(); |
| 13 | + |
| 14 | + // forEach |
| 15 | + //employees.forEach(e-> System.out.println(e.getName()+" : "+e.getSalary())); |
| 16 | + |
| 17 | + //employees.stream().forEach(System.out::println); |
| 18 | + |
| 19 | + //filter |
| 20 | + //.collect |
| 21 | + |
| 22 | + Map<Integer, String> developmentEmployees = employees.stream() |
| 23 | + .filter(e -> e.getDept().equals("Development") && e.getSalary() > 80000) |
| 24 | + .collect(Collectors.toMap(Employee::getId, Employee::getName)); |
| 25 | + |
| 26 | + // System.out.println(developmentEmployees); |
| 27 | + |
| 28 | + //map |
| 29 | + //distinct |
| 30 | + List<String> depts = employees.stream() |
| 31 | + .map(Employee::getDept) |
| 32 | + .distinct() |
| 33 | + .collect(Collectors.toList()); |
| 34 | + //System.out.println(depts); |
| 35 | + |
| 36 | + List<Stream<String>> projectNames = employees.stream() |
| 37 | + .map(e -> e.getProjects() |
| 38 | + .stream().map(p -> p.getName())).collect(Collectors.toList()); |
| 39 | + |
| 40 | + //flatMap |
| 41 | + |
| 42 | + List<String> projects = employees.stream() |
| 43 | + .flatMap(e -> e.getProjects().stream()) |
| 44 | + .map(p -> p.getName()).distinct() |
| 45 | + .collect(Collectors.toList()); |
| 46 | + |
| 47 | + //System.out.println(projects); |
| 48 | + |
| 49 | + |
| 50 | + //sorted |
| 51 | + //asc |
| 52 | + List<Employee> ascSortedEmployees = employees.stream() |
| 53 | + .sorted(Comparator.comparing(Employee::getSalary)) |
| 54 | + .collect(Collectors.toList()); |
| 55 | + |
| 56 | +// ascSortedEmployees.get(0); |
| 57 | + |
| 58 | + //ascSortedEmployees.forEach(System.out::println); |
| 59 | + |
| 60 | + //desc |
| 61 | + List<Employee> descSortedEmployees = employees.stream() |
| 62 | + .sorted(Collections.reverseOrder(Comparator.comparing(Employee::getSalary))) |
| 63 | + .collect(Collectors.toList()); |
| 64 | + |
| 65 | +// descSortedEmployees.get(0); |
| 66 | + |
| 67 | + //descSortedEmployees.forEach(System.out::println); |
| 68 | + |
| 69 | + //min & max |
| 70 | + Optional<Employee> highestPaidEmployees = employees.stream() |
| 71 | + .max(Comparator.comparingDouble(Employee::getSalary)); |
| 72 | + |
| 73 | + // System.out.println("Highest paid employee : "+highestPaidEmployees); |
| 74 | + |
| 75 | + Optional<Employee> lowestPaidEmployees = employees.stream() |
| 76 | + .min(Comparator.comparingDouble(Employee::getSalary)); |
| 77 | + |
| 78 | + //System.out.println("Lowest paid employee : "+lowestPaidEmployees); |
| 79 | + |
| 80 | + //groupingBy |
| 81 | + |
| 82 | + Map<String, List<Employee>> employeeGroup = employees.stream() |
| 83 | + .collect(Collectors.groupingBy(Employee::getGender)); |
| 84 | + |
| 85 | + //System.out.println(employeeGroup); |
| 86 | + |
| 87 | + //Gender -> [names] |
| 88 | + Map<String, List<String>> employeeGroupNames = employees.stream() |
| 89 | + .collect(Collectors.groupingBy(Employee::getGender, |
| 90 | + Collectors.mapping(Employee::getName, Collectors.toList()) |
| 91 | + )); |
| 92 | + |
| 93 | + //System.out.println(employeeGroupNames); |
| 94 | + |
| 95 | + //Gender -> [count] |
| 96 | + Map<String, Long> employeeGroupCountMap = employees.stream() |
| 97 | + .collect(Collectors.groupingBy(Employee::getGender, Collectors.counting())); |
| 98 | + System.out.println(employeeGroupCountMap); |
| 99 | + |
| 100 | + //findFirst |
| 101 | + |
| 102 | + Employee findFirstElement = employees.stream() |
| 103 | + .filter(e -> e.getDept().equals("Development")) |
| 104 | + .findFirst() |
| 105 | + .orElseThrow(()->new IllegalArgumentException("Employee not found ")); |
| 106 | + |
| 107 | +// System.out.println(findFirstElement.get());//NPE |
| 108 | +// |
| 109 | +// if(findFirstElement.isPresent()){ |
| 110 | +// System.out.println(findFirstElement.get()); |
| 111 | +// } |
| 112 | +// |
| 113 | +// findFirstElement.ifPresent(e-> System.out.println(e.getName())); |
| 114 | + |
| 115 | + //System.out.println(findFirstElement); |
| 116 | + |
| 117 | + //findAny |
| 118 | + |
| 119 | + Employee findAnyElement = employees.stream() |
| 120 | + .filter(e -> e.getDept().equals("Development")) |
| 121 | + .findAny() |
| 122 | + .orElseThrow(()->new IllegalArgumentException("Employee not found ")); |
| 123 | + |
| 124 | + // System.out.println(findAnyElement); |
| 125 | + |
| 126 | + //anyMatch(Predicate) , allMatch(Predicate) , noneMatch(Predicate) |
| 127 | + |
| 128 | + boolean developmentEmpAnyMatch = employees.stream() |
| 129 | + .anyMatch(e -> e.getDept().equals("Development")); |
| 130 | + //System.out.println("is there any employee match from development dept "+developmentEmpAnyMatch); |
| 131 | + |
| 132 | + |
| 133 | + boolean developmentEmpAllMatch = employees.stream() |
| 134 | + .allMatch(e -> e.getSalary()>50000);//55000 |
| 135 | + //System.out.println(developmentEmpAllMatch); //false |
| 136 | + |
| 137 | + |
| 138 | + boolean isNoneMatch = employees.stream() |
| 139 | + .noneMatch(e -> e.getDept().equals("abc")); |
| 140 | + //System.out.println(isNoneMatch); |
| 141 | + |
| 142 | + //limit(long) |
| 143 | + |
| 144 | + List<Employee> topPaidEmployees = employees.stream() |
| 145 | + .sorted(Comparator.comparing(Employee::getSalary).reversed()) |
| 146 | + .limit(4) |
| 147 | + .collect(Collectors.toList()); |
| 148 | + |
| 149 | + topPaidEmployees.forEach(e-> System.out.println(e.getName())); |
| 150 | + |
| 151 | + //skip(long) |
| 152 | + List<Employee> skipEmployees = employees.stream().skip(10) |
| 153 | + .collect(Collectors.toList()); |
| 154 | + |
| 155 | + |
| 156 | +// |
| 157 | +// forEach(Consumer) |
| 158 | +// filter(Predicate) |
| 159 | +// collect(Collector) |
| 160 | +// map(Function) |
| 161 | +// distinct() |
| 162 | +// flatMap(Function) |
| 163 | +// sorted(Comparator both ASC and DESC) |
| 164 | +// min() & max() |
| 165 | +// GroupBy |
| 166 | +// findFirst() |
| 167 | +// findAny() |
| 168 | +// anyMatch(Predicate) |
| 169 | +// allMatch(Predicate) |
| 170 | +// noneMatch(Predicate) |
| 171 | +// limit(long maxSize) |
| 172 | +// skip(long n) |
| 173 | + |
| 174 | + |
| 175 | + } |
| 176 | +} |
0 commit comments