2. Посчитайте для каждой валюты разницу между самым большим и самым маленьким заказом типа DELIVERY. Верните результат вычисления для каждой валюты. Валюты в результате должны находиться в порядке возрастания этой разницы. /** * Возвращает map вида {валюта (в порядке возрастания разницы) – разница между самым большим и маленьким заказом типа DELIVERY для валюты}. * Если по какой-то валюте только один заказ, то он является и самым большим и самым маленьким и разница равна 0. * Пример входных данных: * [ * Order(DELIVERY, "EUR", 2000), * Order(DELIVERY, "USD", 15), * Order(DELIVERY, "RUB", 200), * Order (PICKUP, "RUB", 1250), * Order (DELIVERY, "USD", 35), * Order (PICKUP, "USD", 55), * Order (DELIVERY, "RUB", 100) * ] * * Ожидаемый результат: * ["EUR" -> 0.0, "USD" -> 20.0, "RUB" -> 100.0] * */ Map<String, Double> getMaxMinusMinDeliveryMapByCurrency(List<OrderData> orderDataList) { } class OrderService { enum Type {DELIVERY, PICKUP} static class OrderData { final Type type; final String currency; final Long amount; OrderData(Type type, String currency, Long amount) { this.type = type; this.currency = currency; this.amount = amount; } String getCurrency() { return currency; } Long getAmount() { return amount; } Type getType() { return type; } } static class MinMax { long min; long max; MinMax(long amount) { this.min = amount; this.max = amount; } void update(long amount) { min = Math.min(min, amount); max = Math.max(max, amount); } double diff() { return (double) (max - min); } } Map<String, Double> getMaxMinusMinDeliveryMapByCurrency(List<OrderData> orderDataList) { Map<String, MinMax> statsByCurrency = new HashMap<>(); for (OrderData order : orderDataList) { if (order == null) { continue; } if (order.getType() != Type.DELIVERY) { continue; } String currency = order.getCurrency(); long amount = order.getAmount(); MinMax current = statsByCurrency.get(currency); if (current == null) { statsByCurrency.put(currency, new MinMax(amount)); } else { current.update(amount); } } List<Map.Entry<String, MinMax>> resultList = new ArrayList<>(); for (Map.Entry<String, MinMax> entry : statsByCurrency.entrySet()) { String currency = entry.getKey(); double diff = entry.getValue().diff(); resultList.add(Map.entry(currency, diff)); } resultList.sort(Comparator.comparingDouble(Map.Entry::getValue)); Map<String, Double> result = new LinkedHashMap<>(); for (Map.Entry<String, Double> entry : resultList) { result.put(entry.getKey(), entry.getValue()); } return result; } }