コード例 #1
0
ファイル: TodaysSales.java プロジェクト: satanson8517/Lambdas
  public static void main(String[] args) {
    // how many sales?
    long saleCount = saleStream().count();
    System.out.println("Count of sales: " + saleCount);

    // any sales over $100?
    Supplier<DoubleStream> totalStream = () -> saleStream().mapToDouble(Sale::total);
    boolean bigSaleDay = totalStream.get().anyMatch(total -> total > 100.00);
    System.out.println("Big sale day? " + bigSaleDay);

    // maximum sale amount?
    DoubleSummaryStatistics stats = totalStream.get().summaryStatistics();
    System.out.println("Max sale amount: " + stats.getMax());
    System.out.println("Stats on total: " + stats);

    // how many items were sold today?
    Supplier<Stream<Item>> itemStream = () -> saleStream().flatMap(sale -> sale.items.stream());
    long itemCount = itemStream.get().count();
    System.out.println("Count of items: " + itemCount);

    // which different items were sold today?
    String uniqueItems =
        itemStream.get().map(item -> item.identity).distinct().collect(Collectors.joining(" & "));
    System.out.println("Distinct items: " + uniqueItems);

    // summarize sales by store
    ConcurrentMap<String, DoubleSummaryStatistics> summary =
        saleStream()
            .parallel()
            .collect(
                Collectors.groupingByConcurrent(
                    sale -> Thread.currentThread().getName(),
                    Collectors.summarizingDouble(Sale::total)));
    System.out.println("Summary by thread: " + summary);
    summary
        .keySet()
        .stream()
        .sorted()
        .forEach(store -> System.out.println(store + " stats: " + summary.get(store)));
  }
コード例 #2
0
  /**
   * Get the top n external Services of the given services.
   *
   * @param statistics
   * @param externalServices
   * @param timeRange
   * @param limit
   * @return
   */
  public static List<ExternalServiceVo> topByAvgResponseTime(
      Iterable<ExternalServiceStatistic> statistics,
      Iterable<ExternalService> externalServices,
      TimeRange timeRange,
      Integer limit) {
    Map<Long, ExternalService> rpcTransactionMap = new HashMap<>();

    StreamSupport.stream(externalServices.spliterator(), false)
        .forEach(transaction -> rpcTransactionMap.put(transaction.getId(), transaction));

    Map<TransactionGroup, List<ExternalServiceStatistic>> groups =
        StreamSupport.stream(statistics.spliterator(), false)
            .collect(
                Collectors.groupingBy(
                    new Function<ExternalServiceStatistic, TransactionGroup>() {
                      @Override
                      public TransactionGroup apply(ExternalServiceStatistic statistic) {
                        Long transactionId = statistic.getExternalServiceId();
                        ExternalService webTransaction = rpcTransactionMap.get(transactionId);
                        TransactionGroup group = new TransactionGroup();
                        group.setAppId(webTransaction.getAppId());
                        group.setDisplayName(webTransaction.getUrl());
                        return group;
                      }
                    }));
    List<ExternalServiceVo> result = new ArrayList<>();
    groups.forEach(
        (group, webTransactionStatistics) -> {
          DoubleSummaryStatistics responseSummaryStatistics =
              webTransactionStatistics
                  .stream()
                  .filter(statistic -> statistic.getSumResponseTime() != null)
                  .mapToDouble(ExternalServiceStatistic::getSumResponseTime)
                  .summaryStatistics();
          DoubleSummaryStatistics maxSummaryStatistics =
              webTransactionStatistics
                  .stream()
                  .filter(statistic -> statistic.getMaxResponseTime() != null)
                  .mapToDouble(ExternalServiceStatistic::getMaxResponseTime)
                  .summaryStatistics();
          DoubleSummaryStatistics minSummaryStatistics =
              webTransactionStatistics
                  .stream()
                  .filter(statistic -> statistic.getMinResponseTime() != null)
                  .mapToDouble(ExternalServiceStatistic::getMinResponseTime)
                  .summaryStatistics();
          LongSummaryStatistics pvSummaryStatistics =
              webTransactionStatistics
                  .stream()
                  .filter(statistic -> statistic.getPv() != null)
                  .mapToLong(ExternalServiceStatistic::getPv)
                  .summaryStatistics();

          ExternalServiceVo transaction = new ExternalServiceVo();
          transaction.setAppId(group.getAppId());
          transaction.setDestination(group.getDisplayName());

          transaction.setPv(pvSummaryStatistics.getSum());
          transaction.setCpm(
              format(
                  calculateRate(
                      pvSummaryStatistics.getSum(), timeRange.getDuration(ChronoUnit.MINUTES))));
          transaction.setMaxResponseTime(format(maxSummaryStatistics.getMax()));
          transaction.setMinResponseTime(format(minSummaryStatistics.getMin()));
          transaction.setResponseTime(
              format(
                  calculateRate(responseSummaryStatistics.getSum(), pvSummaryStatistics.getSum())));

          result.add(transaction);
        });

    return result
        .stream()
        .sorted(Comparator.comparing(ExternalServiceVo::getResponseTime))
        .limit(limit)
        .collect(Collectors.toList());
  }