public void testClosedStream() throws IOException { try (Stream<Path> s = Files.list(testFolder)) { s.close(); Object[] actual = s.sorted().toArray(); fail("Operate on closed stream should throw IllegalStateException"); } catch (IllegalStateException ex) { // expected } try (Stream<Path> s = Files.walk(testFolder)) { s.close(); Object[] actual = s.sorted().toArray(); fail("Operate on closed stream should throw IllegalStateException"); } catch (IllegalStateException ex) { // expected } try (Stream<Path> s = Files.find(testFolder, Integer.MAX_VALUE, (p, attr) -> true)) { s.close(); Object[] actual = s.sorted().toArray(); fail("Operate on closed stream should throw IllegalStateException"); } catch (IllegalStateException ex) { // expected } }
@Override protected BackEndDataProvider<StrBean, SerializablePredicate<StrBean>> createDataProvider() { return dataProvider = new BackEndDataProvider<>( query -> { Stream<StrBean> stream = data.stream().filter(t -> query.getFilter().orElse(s -> true).test(t)); if (!query.getSortOrders().isEmpty()) { Comparator<StrBean> sorting = query .getSortOrders() .stream() .map(this::getComparator) .reduce((c1, c2) -> c1.thenComparing(c2)) .get(); stream = stream.sorted(sorting); } List<StrBean> list = stream .skip(query.getOffset()) .limit(query.getLimit()) .collect(Collectors.toList()); list.forEach(s -> System.err.println(s.toString())); return list.stream(); }, query -> (int) data.stream().filter(t -> query.getFilter().orElse(s -> true).test(t)).count()); }
public void testWalk() { try (Stream<Path> s = Files.walk(testFolder)) { Object[] actual = s.sorted().toArray(); assertEquals(actual, all); } catch (IOException ioe) { fail("Unexpected IOException"); } }
public static void main(String[] args) throws IOException { try (Stream<Path> stream = Files.list(Paths.get(""))) { String joined = stream .map(String::valueOf) .filter(path -> !path.startsWith(".")) .sorted() .collect(Collectors.joining("; ")); System.out.println("List: " + joined); } Path start = Paths.get(""); int maxDepth = 5; try (Stream<Path> stream = Files.find(start, maxDepth, (path, attr) -> String.valueOf(path).endsWith(".java"))) { String joined = stream.sorted().map(String::valueOf).collect(Collectors.joining("; ")); System.out.println("Found: " + joined); } try (Stream<Path> stream = Files.walk(start, maxDepth)) { String joined = stream .map(String::valueOf) .filter(path -> path.endsWith(".java")) .sorted() .collect(Collectors.joining("; ")); System.out.println("walk(): " + joined); } List<String> lines = Files.readAllLines(Paths.get("src/golf.sh")); lines.add("puts 'foobar'"); Path path = Paths.get("src/golf-modified.sh"); Files.write(path, lines); try (Stream<String> stream = Files.lines(path)) { stream.filter(line -> line.contains("puts")).map(String::trim).forEach(System.out::println); } System.out.println("a" == "a"); System.out.println("a" != new String("a")); System.out.println(null != "a"); System.out.println("a".equals("a")); try (BufferedReader reader = Files.newBufferedReader(path)) { while (reader.ready()) System.out.println(reader.readLine()); } try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("hello-world.sh"))) { writer.write("puts 'Hello world'"); } try (BufferedReader reader = Files.newBufferedReader(path)) { long countPuts = reader.lines().filter(line -> line.contains("put")).count(); System.out.println(countPuts); } }
public void testWalkFollowLink() { // If link is not supported, the directory structure won't have link. // We still want to test the behavior with FOLLOW_LINKS option. try (Stream<Path> s = Files.walk(testFolder, FileVisitOption.FOLLOW_LINKS)) { Object[] actual = s.sorted().toArray(); assertEquals(actual, all_folowLinks); } catch (IOException ioe) { fail("Unexpected IOException"); } }
@Test public void shouldExecuteValiableOrderDesc() { Stream<MonetaryAmount> stream = Stream.of(Money.of(7, EURO), Money.of(9, BRAZILIAN_REAL), Money.of(8, DOLLAR)); List<MonetaryAmount> list = stream.sorted(MonetaryFunctions.sortValuableDesc(provider)).collect(Collectors.toList()); Assert.assertEquals(Money.of(7, EURO), list.get(0)); Assert.assertEquals(Money.of(8, DOLLAR), list.get(1)); Assert.assertEquals(Money.of(9, BRAZILIAN_REAL), list.get(2)); }
public static void main(String[] args) { // 스트림 생성 Stream<String> song = Stream.of("gently", "down", "the", "stream"); // 난수 100개 Stream<Double> randoms = Stream.generate(Math::random).limit(100); // Stream<String> uniqueWords = Stream.of("merrily", "merrily", "merrily", "gentyl").distinct(); Stream<Double> greatFirst = randoms.sorted(Comparator.comparing(Double::doubleValue).reversed()); greatFirst.forEach(System.out::println); }
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"); } }
protected Date getFirstDate(Stream<Date> allDates) { return allDates.sorted().findFirst().get(); }
static void filesTest() { // Files.list try { try (Stream<Path> stream = Files.list(Paths.get("/opt"))) { String joined = stream .map(String::valueOf) .filter(path -> !path.startsWith(".")) .sorted() .collect(Collectors.joining("; ")); System.out.println("List path /opt : " + joined); } } catch (IOException e) { e.printStackTrace(); } // Files.find Path start = Paths.get("/Users/alibaba/Downloads/2016113"); int maxDepth = 5; try (Stream<Path> stream = Files.find(start, maxDepth, (path, attr) -> String.valueOf(path).endsWith(".js"))) { String joined = stream.sorted().map(String::valueOf).collect(Collectors.joining("; ")); System.out.println("Files find : " + joined); } catch (IOException e) { e.printStackTrace(); } // Files.walk try (Stream<Path> stream = Files.walk(start, maxDepth)) { String joined = stream .map(String::valueOf) .filter(path -> path.endsWith(".js")) .sorted() .collect(Collectors.joining("; ")); System.out.println("Files walk : " + joined); } catch (IOException e) { e.printStackTrace(); } // Files.readAllLines try { String p = "/Users/alibaba/linuxsir.txt"; List<String> lines = Files.readAllLines(Paths.get(p)); lines.add("print('foobar');"); Files.write(Paths.get(p), lines); lines.remove(lines.size() - 1); System.out.println("readAllLines " + lines); Files.write(Paths.get(p), lines); } catch (IOException e) { e.printStackTrace(); } // Files.lines try (Stream<String> stream = Files.lines(Paths.get("/Users/alibaba/linuxsir.txt"))) { stream.filter(line -> line.contains("w")).map(String::valueOf).forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } // Files.newBufferedReader&Files.newBufferedWriter Path path = Paths.get("/Users/alibaba/linuxsir.txt"); try (BufferedReader reader = Files.newBufferedReader(path)) { System.out.println(reader.readLine()); } catch (IOException e) { e.printStackTrace(); } path = Paths.get("/Users/alibaba/output.txt"); try (BufferedWriter writer = Files.newBufferedWriter(path)) { writer.write("print('Hello World')"); } catch (IOException e) { e.printStackTrace(); } }
@Override public Stream<T> apply(Stream<T> input) { return comparator != null ? input.sorted(comparator) : input.sorted(); }