849 * return mapToLong(e -> 1L).sum(); |
849 * return mapToLong(e -> 1L).sum(); |
850 * }</pre> |
850 * }</pre> |
851 * |
851 * |
852 * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>. |
852 * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>. |
853 * |
853 * |
|
854 * @apiNote |
|
855 * An implementation may choose to not execute the stream pipeline (either |
|
856 * sequentially or in parallel) if it is capable of computing the count |
|
857 * directly from the stream source. In such cases no source elements will |
|
858 * be traversed and no intermediate operations will be evaluated. |
|
859 * Behavioral parameters with side-effects, which are strongly discouraged |
|
860 * except for harmless cases such as debugging, may be affected. For |
|
861 * example, consider the following stream: |
|
862 * <pre>{@code |
|
863 * List<String> l = Arrays.asList("A", "B", "C", "D"); |
|
864 * long count = l.stream().peek(System.out::println).count(); |
|
865 * }</pre> |
|
866 * The number of elements covered by the stream source, a {@code List}, is |
|
867 * known and the intermediate operation, {@code peek}, does not inject into |
|
868 * or remove elements from the stream (as may be the case for |
|
869 * {@code flatMap} or {@code filter} operations). Thus the count is the |
|
870 * size of the {@code List} and there is no need to execute the pipeline |
|
871 * and, as a side-effect, print out the list elements. |
|
872 * |
854 * @return the count of elements in this stream |
873 * @return the count of elements in this stream |
855 */ |
874 */ |
856 long count(); |
875 long count(); |
857 |
876 |
858 /** |
877 /** |