Пример #1
0
 private void validateFileSystemLoopException(Path start, Path... causes) {
   try (Stream<Path> s = Files.walk(start, FileVisitOption.FOLLOW_LINKS)) {
     try {
       int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
       fail("Should got FileSystemLoopException, but got " + count + "elements.");
     } catch (UncheckedIOException uioe) {
       IOException ioe = uioe.getCause();
       if (ioe instanceof FileSystemLoopException) {
         FileSystemLoopException fsle = (FileSystemLoopException) ioe;
         boolean match = false;
         for (Path cause : causes) {
           if (fsle.getFile().equals(cause.toString())) {
             match = true;
             break;
           }
         }
         assertTrue(match);
       } else {
         fail("Unexpected UncheckedIOException cause " + ioe.toString());
       }
     }
   } catch (IOException ex) {
     fail("Unexpected IOException " + ex);
   }
 }
Пример #2
0
  public static void main(String... args) {

    /*
    -------------------------------------------------------------------------
    From obj to ...
    -------------------------------------------------------------------------
    */
    Stream<String> streamOfStrings = Stream.of("un", "deux", "trois");
    Function<String, StringBuilder> function = StringBuilder::new;
    streamOfStrings.map(function) /*.forEach(System.out::println)*/;
    streamOfStrings = Stream.of("un", "deux", "trois");
    ToIntFunction<String> toIntFunction = String::length;
    IntStream streamOfInts = streamOfStrings.mapToInt(toIntFunction);
    streamOfStrings = Stream.of("un", "deux", "trois");
    ToDoubleFunction<String> toDoubleFunction = String::length;
    DoubleStream streamOfDoubles = streamOfStrings.mapToDouble(toDoubleFunction);
    streamOfStrings = Stream.of("un", "deux", "trois");
    ToLongFunction<String> toLongFunction = String::length;
    LongStream streamOfLongs = streamOfStrings.mapToLong(toLongFunction);

    /*
    -------------------------------------------------------------------------
    From int to ...
    -------------------------------------------------------------------------
    */
    IntUnaryOperator plusplus = i -> i++;
    IntStream.of(1, 2, 3).map(plusplus)
    /*.forEach(x->System.out.println(x))*/ ;
    IntFunction<String> intFunction = i -> "" + i;
    IntStream.of(1, 2, 3).mapToObj(intFunction)
    /*.forEach(System.out::println)*/ ;
    IntToDoubleFunction itdf = i -> i;
    IntStream.of(1, 2, 3).mapToDouble(itdf)
    /*.forEach(System.out::println)*/ ;
    IntToLongFunction itlf = i -> i;
    IntStream.of(1, 2, 3).mapToLong(itlf)
    /*.forEach(System.out::println)*/ ;

    /*
    -------------------------------------------------------------------------
    From long to ...
    -------------------------------------------------------------------------
    */
    LongUnaryOperator times = l -> l * l;
    LongStream.of(1L, 2L, 3L).map(times)
    /*.forEach(System.out::println)*/ ;
    LongFunction<String> lf = l -> "toto";
    LongStream.of(1L, 2L, 3L).mapToObj(lf);

    /*
    -------------------------------------------------------------------------
    From double to ...
    -------------------------------------------------------------------------
    */
    DoubleToIntFunction dtif = d -> (int) d;
    DoubleStream.of(1.3, 1.5, 1.6).mapToInt(dtif).forEach(System.out::println);
  }
Пример #3
0
  public void testBasic() {
    try (Stream<Path> s = Files.list(testFolder)) {
      Object[] actual = s.sorted().toArray();
      assertEquals(actual, level1);
    } catch (IOException ioe) {
      fail("Unexpected IOException");
    }

    try (Stream<Path> s = Files.list(testFolder.resolve("empty"))) {
      int count = s.mapToInt(p -> 1).reduce(0, Integer::sum);
      assertEquals(count, 0, "Expect empty stream.");
    } catch (IOException ioe) {
      fail("Unexpected IOException");
    }
  }
Пример #4
0
  public static void main(String[] args) {

    // Page 45
    // ========

    IntStream.iterate(1, i -> i * 2).limit(10).forEachOrdered(System.out::println);

    IntStream.range(1, 6).forEach(System.out::print); // prints 123456

    // Page 46
    // ========

    List<Integer> intList = Arrays.asList(1, 2, 3);

    OptionalDouble maxDistance =
        intList.stream().map(i -> new Point(i % 3, i / 3)).mapToDouble(p -> p.distance(0, 0)).max();

    DoubleStream ds =
        intList.stream().map(i -> new Point(i % 3, i / 3)).mapToDouble(p -> p.distance(0, 0));

    OptionalDouble maxDistance1 = ds.max();

    // Page 48
    // ========

    Optional<Integer> max =
        Arrays.asList(1, 2, 3, 4, 5).stream().map(i -> i + 1).max(Integer::compareTo);

    // Page 49
    // ========

    OptionalInt max1 = IntStream.rangeClosed(1, 5).map(i -> i + 1).max();

    DoubleStream ds1 = IntStream.rangeClosed(1, 10).asDoubleStream();

    Stream<Integer> is = IntStream.rangeClosed(1, 10).boxed();

    Stream<Integer> integerStream = Stream.of(1, 2);
    IntStream is1 = integerStream.mapToInt(Integer::intValue);
  }