Example usage for java.util.stream Stream count

List of usage examples for java.util.stream Stream count

Introduction

In this page you can find the example usage for java.util.stream Stream count.

Prototype

long count();

Source Link

Document

Returns the count of elements in this stream.

Usage

From source file:Main.java

public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> numbers1 = Arrays.asList(1, 2, 3, 4, 5);

    Stream<Integer> s = Stream.concat(numbers.stream(), numbers1.stream());

    System.out.println(s.count());
}

From source file:Main.java

public static void main(String[] argv) {
    List<Person> persons = Arrays.asList(new Person("Joe", 12), new Person("Jim", 34), new Person("John", 23));

    Stream<MyWrapper> stream = persons.stream().filter(p -> p.getAge() > 18).map(p -> new MyWrapper(p));

    System.out.println(stream.count());

}

From source file:io.github.carlomicieli.footballdb.starter.pages.PageTests.java

@Test
public void shouldFindElementByPredicateByTagName() {
    String html = "" + "<div summary=\"one\">one</div>" + "<div summary=\"two\">two</div>"
            + "<div summary=\"three\"><div>three</div></div>";
    Page page = page(fromHtml(html));/*  w  ww. j a v a  2s  .  c o m*/

    Stream<Element> divs = page.selectElements(whereTagNameIs("div"));
    assertThat(divs.count()).isEqualTo(4);
}

From source file:io.github.carlomicieli.footballdb.starter.pages.PageTests.java

@Test
public void shouldFindElementByPredicateForClassNames() {
    String html = "" + "<div class=\"one\">one</div>" + "<div class=\"two\">two</div>"
            + "<div class=\"three\">three</div>";
    Page page = page(fromHtml(html));/*from  w ww.  java2s  . c  om*/

    Stream<Element> divsExact = page.selectElements(classNameIs("one"));
    assertThat(divsExact.count()).isEqualTo(1);
}

From source file:io.github.carlomicieli.footballdb.starter.pages.PageTests.java

@Test
public void shouldFindElementByPredicateForAttributes() {
    String html = "" + "<div summary=\"one\">one</div>" + "<div summary=\"two\">two</div>"
            + "<div summary=\"three\">three</div>";
    Page page = page(fromHtml(html));/*from ww  w.j  a v  a 2  s  .c  o m*/

    Stream<Element> divsExact = page.selectElements(containsAttr("summary", "two"));
    assertThat(divsExact.count()).isEqualTo(1);

    Stream<Element> divsStartsWith = page.selectElements(containsAttrLike("summary", "t%"));
    assertThat(divsStartsWith.count()).isEqualTo(2);
}

From source file:io.github.carlomicieli.footballdb.starter.pages.PageTests.java

@Test
public void shouldReturnStreamForChildrenElementsWithGivenTag() {
    String html = "<table id=\"my-table\">" + "<tr><td>1</td><td>one</td></tr>"
            + "<tr><td>2</td><td>two</td></tr>" + "<tr><td>3</td><td>three</td></tr>" + "</table>";
    Page page = page(fromHtml(html));/*from  w ww.  ja  v a2s  .  co  m*/

    Element table = page.elementWithId("my-table").orElseThrow(NoSuchElementException::new);
    Stream<Element> stream = Page.childrenWithTag(table, "tr");
    Stream<Element> columns = stream.limit(1).flatMap(r -> Page.childrenWithTag(r, "td"));
    assertThat(columns.count()).isEqualTo(2);
}

From source file:io.github.carlomicieli.footballdb.starter.pages.TableTests.java

@Test
public void shouldReturnAStreamOfRows() {
    Table table3x3 = newTable3x3();//from   w  w  w.j  a v  a2  s  . c  o  m
    Stream<Table.Row> rows = table3x3.rowsStream();
    assertThat(rows.count()).isEqualTo(3);
}

From source file:io.github.carlomicieli.footballdb.starter.pages.PageTests.java

@Test
public void shouldReturnAStreamForElementsWithTheSameClass() {
    String html = "<div class=\"one\">\n" + "    <p>one</p>\n" + "</div>\n" + "<div class=\"one\">\n"
            + "    <p>two</p>\n" + "</div>\n" + "<div class=\"one\">\n" + "    <p>tree</p>\n" + "</div>";
    Page page = page(fromHtml(html));/*from   ww  w. j  av  a  2s  .  c  o m*/
    Stream<Element> streamOne = page.elementsWithClass("one");
    Stream<Element> streamTwo = page.elementsWithClass("two");

    assertThat(streamOne).isNotNull();
    assertThat(streamOne.count()).isEqualTo(3);
    assertThat(streamTwo.count()).isEqualTo(0);
}

From source file:com.ikanow.aleph2.graph.titan.services.TestTitanGraphService.java

@Test
public void test_onPublishOrUpdate() {

    final DataBucketBean bucket = BeanTemplateUtils.build(DataBucketBean.class)
            .with(DataBucketBean::full_name, "/test/validate/schema").done().get();

    // do nothing
    {/*from  w  ww.j ava 2 s  .com*/
        CompletableFuture<Collection<BasicMessageBean>> ret_val = _mock_graph_db_service.onPublishOrUpdate(
                bucket, Optional.empty(), false, Collections.emptySet(), Collections.emptySet());

        assertEquals(Collections.emptyList(), ret_val.join());
    }
    // create all the indexes
    {
        CompletableFuture<Collection<BasicMessageBean>> ret_val = _mock_graph_db_service.onPublishOrUpdate(
                bucket, Optional.empty(), false, ImmutableSet.of(GraphSchemaBean.name), Collections.emptySet());

        assertEquals("" + ret_val.join().stream().map(b -> b.message()).collect(Collectors.joining(" // ")),
                Collections.emptyList(), ret_val.join());

        // But also now check the Titan indexes have all been created:

        final TitanManagement mgmt = _titan.openManagement();
        Stream<TitanGraphIndex> v_indexes = StreamUtils.stream(mgmt.getGraphIndexes(Vertex.class));
        Stream<TitanGraphIndex> e_indexes = StreamUtils.stream(mgmt.getGraphIndexes(Edge.class));
        assertEquals(4L, v_indexes.count());
        assertEquals(1L, e_indexes.count());
    }
    // rerun to check it all works second+ time round
    {
        CompletableFuture<Collection<BasicMessageBean>> ret_val = _mock_graph_db_service.onPublishOrUpdate(
                bucket, Optional.empty(), false, ImmutableSet.of(GraphSchemaBean.name), Collections.emptySet());

        assertEquals(
                "Should return no errors: "
                        + ret_val.join().stream().map(b -> b.message()).collect(Collectors.joining(";")),
                Collections.emptyList(), ret_val.join());

        // But also now check the Titan indexes have all been created:

        final TitanManagement mgmt = _titan.openManagement();
        Stream<TitanGraphIndex> v_indexes = StreamUtils.stream(mgmt.getGraphIndexes(Vertex.class));
        Stream<TitanGraphIndex> e_indexes = StreamUtils.stream(mgmt.getGraphIndexes(Edge.class));
        assertEquals(4L, v_indexes.count());
        assertEquals(1L, e_indexes.count());
    }
    // Error if specifying deduplication fields
    {
        final GraphSchemaBean graph_schema = BeanTemplateUtils.build(GraphSchemaBean.class)
                .with(GraphSchemaBean::custom_decomposition_configs, Arrays.asList())
                .with(GraphSchemaBean::deduplication_fields, Arrays.asList("nonempty")).done().get();

        final DataBucketBean dedup_fields_bucket = BeanTemplateUtils.build(DataBucketBean.class)
                .with(DataBucketBean::full_name, "/test/on/publish")
                .with(DataBucketBean::data_schema, BeanTemplateUtils.build(DataSchemaBean.class)
                        .with(DataSchemaBean::graph_schema, graph_schema).done().get())
                .done().get();

        CompletableFuture<Collection<BasicMessageBean>> ret_val = _mock_graph_db_service.onPublishOrUpdate(
                dedup_fields_bucket, Optional.empty(), false, ImmutableSet.of(GraphSchemaBean.name),
                Collections.emptySet());

        assertEquals(1, ret_val.join().size());
        assertEquals(1, ret_val.join().stream().filter(b -> !b.success()).count());
    }

    //(See also test_handleBucketDeletionRequest, for some coverage testing of onPublishOrUpdate)
}

From source file:io.siddhi.extension.io.file.FileSinkTestCase.java

@Test
public void fileSinkTest13() throws InterruptedException, CannotRestoreSiddhiAppStateException, IOException {
    log.info("test SiddhiIoFile Sink 13");

    String streams = "" + "@App:name('TestSiddhiApp')"
            + "define stream FooStream (symbol string, price float, volume long); "
            + "@sink(type='file', add.line.separator='false', @map(type='csv'), append='true', " + "file.uri='"
            + sinkUri + "/test3.xml') " + "define stream BarStream (symbol string, price float, volume long); ";

    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";

    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("FooStream");

    siddhiAppRuntime.start();//from   w  ww .  jav  a  2  s  . c  o  m

    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 57.678f, 100L });
    stockStream.send(new Object[] { "GOOGLE", 50f, 100L });
    stockStream.send(new Object[] { "REDHAT", 50f, 100L });
    Thread.sleep(100);

    File sink = new File(sinkUri);
    if (sink.isDirectory()) {
        for (File file : sink.listFiles()) {
            Stream<String> lines = Files.lines(file.toPath(), StandardCharsets.UTF_8);
            AssertJUnit.assertEquals(4, lines.count());
        }
    } else {
        AssertJUnit.fail(sinkUri + " is not a directory.");
    }

    siddhiAppRuntime.shutdown();
}