Skip to content

Instantly share code, notes, and snippets.

View theboreddev's full-sized avatar
💭
💻

The Bored Dev theboreddev

💭
💻
View GitHub Profile
final Item item = new Item(1L, new BigDecimal("12.99"));
System.out.println("Delivery price is " + Plan.BASIC.deliveryPrice.apply(item));
System.out.println("Delivery price is " + Plan.PREMIUM.deliveryPrice.apply(item));
System.out.println("Delivery price is " + Plan.BUSINESS.deliveryPrice.apply(item));
final Car car = new Car();
final CarWashStep chain = new InitialWashStep();
chain.andThen(new SoapStep())
.andThen(new RinseStep())
.andThen(new PolishStep())
.andThen(new DryStep());
final Car finalCar = chain.applyTo(car);
package com.theboreddev.examples.forkjoin;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
public class ForkJoinExample {
public static void main(String[] args) {
final Collection<Employee> employees = List.of(
new Employee("Karen Smith", 51200.0, 29, Employee.Sex.FEMALE),
new Employee("John Smith", 24000.0, 32, Employee.Sex.MALE),
new Employee("Anthony Jackson", 44000.0, 33, Employee.Sex.MALE),
new Employee("Alyson Palmer", 34320.0, 36, Employee.Sex.FEMALE),
new Employee("Jessica Sanders", 64320.0, 34, Employee.Sex.FEMALE)
);
final Map<Employee.Sex, List<Employee>> employeesBySex = employees.stream()
.collect(groupingBy(Employee::getSex));
final Map<Employee.Sex, Set<Employee>> employeesBySex = employees.stream()
.collect(groupingBy(Employee::getSex, toSet()));
final Map<Employee.Sex, Double> averageAgeBySex = employees.stream()
.collect(groupingBy(Employee::getSex, averagingInt(Employee::getAge)));
@theboreddev
theboreddev / GroupByClassifier.java
Created July 18, 2020 20:31
groupBySexAndAge groupBySexAndAge
final Map<Employee.Sex, Map<Integer, List<Employee>>> groupBySexAndAge = employees.stream()
.collect(groupingBy(Employee::getSex, groupingBy(Employee::getAge)));
@theboreddev
theboreddev / GroupByClassifier.java
Created July 18, 2020 20:35
youngestEmployeeBySex
final Map<Employee.Sex, Optional<Employee>> youngestEmployeeBySex = employees.stream()
.collect(groupingBy(Employee::getSex, minBy(comparing(Employee::getAge))));
@theboreddev
theboreddev / GroupByClassifier.java
Created July 18, 2020 20:49
employeesOverThirtyBySex
final Map<Employee.Sex, List<Employee>> employeesOverThirtyBySex = employees.stream()
.collect(groupingBy(Employee::getSex, filtering(employee -> employee.getAge() > 30, toList())));
final Collection<Employee> employees = List.of(
new Employee("Karen Smith", 51200.0, 29, Employee.Sex.FEMALE),
new Employee("John Smith", 24000.0, 32, Employee.Sex.MALE),
new Employee("Anthony Jackson", 44000.0, 33, Employee.Sex.MALE),
new Employee("Alyson Palmer", 34320.0, 36, Employee.Sex.FEMALE),
new Employee("Jessica Sanders", 64320.0, 34, Employee.Sex.FEMALE)
);
final Map<Employee.Sex, List<Employee>> existingMap = new HashMap<>() {
{