Beispiel #1
0
  public static void main(String[] args) throws IOException {
    Path path = Paths.get("../alice.txt");
    String contents = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);

    Stream<String> words = Stream.of(contents.split("[\\P{L}]+"));
    show("words", words);
    Stream<String> song = Stream.of("gently", "down", "the", "stream");
    show("song", song);
    Stream<String> silence = Stream.empty();
    silence = Stream.<String>empty(); // Explicit type specification
    show("silence", silence);

    Stream<String> echos = Stream.generate(() -> "Echo");
    show("echos", echos);

    Stream<Double> randoms = Stream.generate(Math::random);
    show("randoms", randoms);

    Stream<BigInteger> integers = Stream.iterate(BigInteger.ONE, n -> n.add(BigInteger.ONE));
    show("integers", integers);

    Stream<String> wordsAnotherWay = Pattern.compile("[\\P{L}]+").splitAsStream(contents);
    show("wordsAnotherWay", wordsAnotherWay);

    try (Stream<String> lines = Files.lines(path, StandardCharsets.UTF_8)) {
      show("lines", lines);
    }
  }
Beispiel #2
0
 public static void main(String[] args) {
   Personne[] tab = {
     new Personne("thibault", "Rougier", 2001),
     new Personne("thomas", "Niesseron", 1987),
     new Personne("thifaine", "Mitenne", 1959),
     new Personne("maxime", "Forest", 1995),
     new Personne("jules", "Forest", 1995)
   };
   System.out.println("--- Nes apres 1985 : ");
   Stream.of(tab)
       .filter(pp -> pp.getAnnee() > 1985)
       .forEach(pp -> System.out.print(pp.getPrenom() + ", "));
   System.out.println("\n--- Nes avant 2000 :");
   long nombre =
       Stream.of(tab)
           .filter(pp -> pp.getAnnee() < 2000)
           .sorted(Comparator.comparing(Personne::getNom))
           .peek(pp -> System.out.print(pp.getNom() + " "))
           .count();
   System.out.println("\n       Ils sont " + nombre);
   System.out.println("--- Tous tries sur nom + prenom : ");
   Stream.of(tab)
       .sorted(Comparator.comparing(pp -> pp.getNom() + pp.getPrenom()))
       .forEach(pp -> System.out.print("(" + pp.getNom() + ", " + pp.getPrenom() + ") "));
 }
  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);
  }
 @Test
 public void shouldConvertToStream() {
   final Value<Integer> value = of(1, 2, 3);
   final Stream<Integer> stream = value.toStream();
   if (value.isSingleValued()) {
     assertThat(stream).isEqualTo(Stream.of(1));
   } else {
     assertThat(stream).isEqualTo(Stream.of(1, 2, 3));
   }
 }
Beispiel #5
0
  public static void main(String[] args) {
    // check args
    Stream.of(args).forEach(System.out::println);

    // check .
    Stream.of(new File(".").list()).forEach(System.out::println);

    // check external libs
    Client client = ClientBuilder.newClient();
    String response = client.target("https://api.github.com/zen").request().get(String.class);
    System.out.println(response);
  }
Beispiel #6
0
  public static void main(String[] args) {

    Map<Integer, HashSet<String>> withSet =
        Stream.of("jon", "andrew", "harvey", "bil")
            .collect(Collectors.groupingBy(String::length, HashSet::new));
    out.println(withSet);
  }
Beispiel #7
0
 static <T> Stream<T> inOrder(Tree<T> tree) {
   if (tree.isLeaf()) {
     return Stream.of(tree.getValue());
   } else {
     final List<Node<T>> children = tree.getChildren();
     return children
         .tail()
         .foldLeft(Stream.<T>empty(), (acc, child) -> acc.appendAll(inOrder(child)))
         .prepend(tree.getValue())
         .prependAll(inOrder(children.head()));
   }
 }
Beispiel #8
0
 static <T> Stream<T> preOrder(Tree<T> tree) {
   return tree.getChildren()
       .foldLeft(Stream.of(tree.getValue()), (acc, child) -> acc.appendAll(preOrder(child)));
 }