예제 #1
0
  public static void main(String[] args) {
    /** 例①: 打印数列的前10项 */
    Stream<Long> fibonacci = Stream.generate(new FibonacciSupplier());
    fibonacci.limit(10).forEach(System.out::println);

    /** 例②: 打印数列的20~30项 */
    Stream<Long> fibonacci2 = Stream.generate(new FibonacciSupplier());
    List<Long> list = fibonacci2.skip(20).limit(10).collect(Collectors.toList());
    list.forEach(System.out::println);
  }
예제 #2
0
 @Nullable
 public static <T> T getIfSingle(@Nullable Stream<T> items) {
   return items == null
       ? null
       : items
           .limit(2)
           .map(Optional::ofNullable)
           .reduce(
               Optional.empty(), (a, b) -> a.isPresent() ^ b.isPresent() ? b : Optional.empty())
           .orElse(null);
 }
예제 #3
0
 public static <T> void show(String title, Stream<T> stream) {
   final int SIZE = 10;
   List<T> firstElements = stream.limit(SIZE + 1).collect(Collectors.toList());
   System.out.print(title + ": ");
   if (firstElements.size() <= SIZE) System.out.println(firstElements);
   else {
     firstElements.remove(SIZE);
     String out = firstElements.toString();
     System.out.println(out.substring(0, out.length() - 1) + ", ...]");
   }
 }
  public static void main(String[] args) {
    S3BillingRecordFileScanner fileScanner = createBillingRecordFileScanner(args);

    // The scan() method returns the full list of all billing files. This list could get rather
    // large over time.
    // For this example we'll just print a few files to demonstrate the method.
    System.out.println("Printing a few billing files...");
    try (Stream<S3BillingRecordFile> billingRecordFileStream = fileScanner.scan()) {
      // For the purpose of this example we'll limit the output to 3 files. Java's streams make this
      // trivial.
      billingRecordFileStream
          .limit(3)
          .forEach(
              s3BillingRecordFile -> {
                printBillingRecoreFile(System.out, s3BillingRecordFile);
              });
    }
    System.out.println();

    // Scanning for all of the billing files has margin value as the contents of the various file
    // types differs
    // dramatically. It is likely that you are only interested in files of a particular type. The
    // scan() method
    // takes an argument that is the file type you are interested in.
    System.out.println("Printing a few detailed line item files...");
    try (Stream<S3BillingRecordFile> billingRecordFileStream =
        fileScanner.scan(FileType.DETAILED_LINE_ITEMS)) {
      // For the purpose of this example we'll limit the output to 3 files. Java's streams make this
      // trivial.
      billingRecordFileStream
          .limit(3)
          .forEach(
              s3BillingRecordFile -> {
                printBillingRecoreFile(System.out, s3BillingRecordFile);
              });
    }
    System.out.println();

    // If you are interested in a billing file for a specific month (ex. this month or last month)
    // there
    // is an additional overload of the scan() method that accepts a month and year.
    System.out.println("Printing the detailed line item for Jan. 2016...");
    try (Stream<S3BillingRecordFile> billingRecordFileStream =
        fileScanner.scan(FileType.DETAILED_LINE_ITEMS, 2016, 1)) {
      // Since we have specified the file type, year and month the scan() method above _should_
      // return a stream
      // with only zero or one items. We'll limit the results to 1 for sanity sake.
      billingRecordFileStream
          .limit(1)
          .forEach(
              s3BillingRecordFile -> {
                printBillingRecoreFile(System.out, s3BillingRecordFile);
              });
    }
    System.out.println();

    // Finally, if you want to search the billing files using some other criteria you can pass in a
    // predicate. Note
    // that this predicate version of scan() isn't magic: you can just pass the same predicate to
    // the filter()
    // method of the resultant stream.
    System.out.println("Printing a few detailed line item files where the month is Jan or Feb...");
    Predicate<S3BillingRecordFile> searchPredicate =
        billingRecordFile -> billingRecordFile.getMonth() < 3;
    try (Stream<S3BillingRecordFile> billingRecordFileStream = fileScanner.scan(searchPredicate)) {
      billingRecordFileStream
          .limit(5)
          .forEach(
              s3BillingRecordFile -> {
                printBillingRecoreFile(System.out, s3BillingRecordFile);
              });
    }
    System.out.println();
  }