Last active
May 31, 2025 21:01
-
-
Save fResult/136dbe716cc673d23e68d56c3f14425a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.math.BigDecimal; | |
| import java.time.Duration; | |
| import java.time.LocalDateTime; | |
| import java.util.*; | |
| import java.util.stream.Collector; | |
| import java.util.stream.Collectors; | |
| class TripAnalyzerApp1 { | |
| private static final List<Trip> TRIPS = List.of( | |
| new Trip(TripType.BUSINESS, "Tokyo", Duration.ofHours(5),LocalDateTime.of(2025, 5, 25, 8, 0), "Thomas Anderson", new BigDecimal("1200.00")), | |
| new Trip(TripType.BUSINESS, "New York", Duration.ofHours(8), LocalDateTime.of(2025, 5, 26, 9, 30), "John Wick", new BigDecimal("2500.00")), | |
| new Trip(TripType.VACATION, "Phuket", Duration.ofHours(3), LocalDateTime.of(2025, 6, 1, 10, 15), "Johnny Silverhand", new BigDecimal("850.00")), | |
| new Trip(TripType.VACATION, "Paris", Duration.ofHours(7), LocalDateTime.of(2025, 6, 5, 12, 0), "Ted Logan", new BigDecimal("1800.00")), | |
| new Trip(TripType.EDUCATION, "London", Duration.ofHours(6), LocalDateTime.of(2025, 5, 29, 7, 45), "Jack Traven", new BigDecimal("1350.00")), | |
| new Trip(TripType.EDUCATION, "Boston", Duration.ofHours(9), LocalDateTime.of(2025, 6, 10, 8, 30), "John Constantine", new BigDecimal("1950.00")), | |
| new Trip(TripType.MEDICAL, "Singapore", Duration.ofHours(4), LocalDateTime.of(2025, 5, 30, 14, 0), "Kevin Lomax", new BigDecimal("1100.00")), | |
| new Trip(TripType.RELOCATION, "Berlin", Duration.ofHours(10), LocalDateTime.of(2025, 6, 15, 11, 30), "Jonathan Harker", new BigDecimal("3200.00"))); | |
| public static void main(String[] args) { | |
| final var analyzer = new TripDurationAnalyzer(); | |
| final var result = analyzer.averageOfTypeDestinationDurations(TRIPS); | |
| System.out.println("Average trip duration: " + (result.isPresent() ? result.getAsDouble() + " minutes" : "No data")); | |
| } | |
| } | |
| class TripDurationAnalyzer { | |
| public OptionalDouble averageOfTypeDestinationDurations(List<Trip> trips) { | |
| final var averageDurationsByType = trips.stream().collect(groupByTypeWithDestinationAverages()).values(); | |
| return averageDurationsByType.stream() | |
| .filter(OptionalDouble::isPresent) | |
| .mapToDouble(OptionalDouble::getAsDouble) | |
| .average(); | |
| } | |
| private static Collector<Trip, ?, Map<TripType, OptionalDouble>> groupByTypeWithDestinationAverages() { | |
| return Collectors.groupingBy(Trip::type, averageMinDurationCollector()); | |
| } | |
| private static Collector<Trip, ?, OptionalDouble> averageMinDurationCollector() { | |
| return Collectors.collectingAndThen( | |
| groupDestinationsByMinDuration(), DurationStatistics::calculateAverageDuration); | |
| } | |
| private static final Comparator<Trip> BY_DURATION = Comparator.comparing(Trip::duration); | |
| private static Collector<Trip, ?, Map<String, Duration>> groupDestinationsByMinDuration() { | |
| return Collectors.groupingBy( | |
| Trip::destination, | |
| Collectors.collectingAndThen( | |
| Collectors.minBy(BY_DURATION), | |
| opt -> opt.map(Trip::duration).orElse(null))); | |
| } | |
| } | |
| final class DurationStatistics { | |
| public static OptionalDouble calculateAverageDuration(Map<String, Duration> map) { | |
| final var nonNullDurations = map.values().stream().filter(Objects::nonNull); | |
| return nonNullDurations.mapToLong(Duration::toMinutes).average(); | |
| } | |
| } | |
| enum TripType { | |
| BUSINESS, | |
| VACATION, | |
| EDUCATION, | |
| MEDICAL, | |
| RELOCATION; | |
| } | |
| record Trip( | |
| TripType type, | |
| String destination, | |
| Duration duration, | |
| LocalDateTime departureTime, | |
| String travelerName, | |
| BigDecimal cost) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.math.BigDecimal; | |
| import java.time.Duration; | |
| import java.time.LocalDateTime; | |
| import java.util.*; | |
| import java.util.function.Function; | |
| import java.util.stream.Collectors; | |
| class TripAnalyzerApp2 { | |
| private static final List<Trip> TRIPS = List.of( | |
| new Trip(TripType.BUSINESS, "Tokyo", Duration.ofHours(5),LocalDateTime.of(2025, 5, 25, 8, 0), "Thomas Anderson", new BigDecimal("1200.00")), | |
| new Trip(TripType.BUSINESS, "New York", Duration.ofHours(8), LocalDateTime.of(2025, 5, 26, 9, 30), "John Wick", new BigDecimal("2500.00")), | |
| new Trip(TripType.VACATION, "Phuket", Duration.ofHours(3), LocalDateTime.of(2025, 6, 1, 10, 15), "Johnny Silverhand", new BigDecimal("850.00")), | |
| new Trip(TripType.VACATION, "Paris", Duration.ofHours(7), LocalDateTime.of(2025, 6, 5, 12, 0), "Ted Logan", new BigDecimal("1800.00")), | |
| new Trip(TripType.EDUCATION, "London", Duration.ofHours(6), LocalDateTime.of(2025, 5, 29, 7, 45), "Jack Traven", new BigDecimal("1350.00")), | |
| new Trip(TripType.EDUCATION, "Boston", Duration.ofHours(9), LocalDateTime.of(2025, 6, 10, 8, 30), "John Constantine", new BigDecimal("1950.00")), | |
| new Trip(TripType.MEDICAL, "Singapore", Duration.ofHours(4), LocalDateTime.of(2025, 5, 30, 14, 0), "Kevin Lomax", new BigDecimal("1100.00")), | |
| new Trip(TripType.RELOCATION, "Berlin", Duration.ofHours(10), LocalDateTime.of(2025, 6, 15, 11, 30), "Jonathan Harker", new BigDecimal("3200.00"))); | |
| public static void main(String[] args) { | |
| final var analyzer = new TripDurationAnalyzer(); | |
| final var result = analyzer.averageOfTypeDestinationDurations(TRIPS); | |
| System.out.println("Average trip duration: " + (result.isPresent() ? result.getAsDouble() + " minutes" : "No data")); | |
| } | |
| } | |
| class TripDurationAnalyzer { | |
| public OptionalDouble averageOfTypeDestinationDurations(List<Trip> trips) { | |
| final Map<TripType, Map<String, Trip>> tripsByTypeAndDestination = groupByTypeAndDestination(trips); | |
| final Map<TripType, Map<String, Duration>> minDurationsByTypeAndDestination = findMinDurations(tripsByTypeAndDestination); | |
| final Map<TripType, OptionalDouble> averagesByType = calculateAveragesByType(minDurationsByTypeAndDestination); | |
| return averagesByType.values().stream() | |
| .filter(OptionalDouble::isPresent) | |
| .mapToDouble(OptionalDouble::getAsDouble) | |
| .average(); | |
| } | |
| private Map<TripType, Map<String, Trip>> groupByTypeAndDestination(List<Trip> trips) { | |
| return trips.stream() | |
| .collect(Collectors.groupingBy(Trip::type, | |
| Collectors.toMap(Trip::destination, Function.identity(), | |
| (trip1, trip2) -> trip1.duration().compareTo(trip2.duration()) <= 0 ? trip1 : trip2))); | |
| } | |
| private Map<TripType, Map<String, Duration>> findMinDurations(Map<TripType, Map<String, Trip>> byTypeAndDestination) { | |
| return byTypeAndDestination.entrySet().stream() | |
| .collect(Collectors.toMap( | |
| Map.Entry::getKey, | |
| entry -> extractDurations(entry.getValue()) | |
| )); | |
| } | |
| private Map<String, Duration> extractDurations(Map<String, Trip> tripByDestination) { | |
| return tripByDestination.entrySet().stream() | |
| .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().duration())); | |
| } | |
| private Map<TripType, OptionalDouble> calculateAveragesByType(Map<TripType, Map<String, Duration>> durationsByType) { | |
| return durationsByType.entrySet().stream() | |
| .collect(Collectors.toMap( | |
| Map.Entry::getKey, | |
| entry -> DurationStatistics.calculateAverageDuration(entry.getValue()) | |
| )); | |
| } | |
| } | |
| final class DurationStatistics { | |
| public static OptionalDouble calculateAverageDuration(Map<String, Duration> durations) { | |
| return durations.values().stream() | |
| .mapToLong(Duration::toMinutes) | |
| .average(); | |
| } | |
| } | |
| enum TripType { | |
| BUSINESS, | |
| VACATION, | |
| EDUCATION, | |
| MEDICAL, | |
| RELOCATION; | |
| } | |
| record Trip( | |
| TripType type, | |
| String destination, | |
| Duration duration, | |
| LocalDateTime departureTime, | |
| String travelerName, | |
| BigDecimal cost) {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment