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); } }
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) { Map<Integer, HashSet<String>> withSet = Stream.of("jon", "andrew", "harvey", "bil") .collect(Collectors.groupingBy(String::length, HashSet::new)); out.println(withSet); }
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())); } }
static <T> Stream<T> preOrder(Tree<T> tree) { return tree.getChildren() .foldLeft(Stream.of(tree.getValue()), (acc, child) -> acc.appendAll(preOrder(child))); }