是一个原始整数值序列,该流提供了许多方法可以对该流中的元素顺序执行或并行执行一些聚合操作
所属包:java.util.stream
注意事项
Stream API只能被消费一次,后续重复使用已建立的流会报异常!所以stream流是线程安全的
案例
public static void main(String[] args) {
IntStream intStream = IntStream.range(1, 5);
intStream.forEach(System.out::println);
IntStream intStream1 = IntStream.rangeClosed(1, 5);
intStream1.forEach(System.out::println);//1234
IntStream intStream2 = IntStream.rangeClosed(1, 3);
int a = intStream2.sum();
System.out.println(a);//12345
IntStream intStream3 = IntStream.rangeClosed(5, 10);
OptionalInt b = intStream3.min();//[5]
IntStream intStream4 = IntStream.rangeClosed(1, 3);
int[] c = intStream4.toArray();
}
Modifier and Type | Method and Description |
---|---|
boolean | allMatch(IntPredicate predicate) 返回此流的所有元素是否与提供的谓词匹配。 |
boolean | anyMatch(IntPredicate predicate) 返回此流的任何元素是否与提供的谓词匹配。 |
DoubleStream | asDoubleStream() 返回一个 |
LongStream | asLongStream() 返回一个 |
OptionalDouble | average() 返回 |
Stream<Integer> | boxed() 返回一个 |
static IntStream.Builder | builder() 返回一个 |
<R> R | collect(Supplier<R> supplier, ObjIntConsumer<R> accumulator, BiConsumer<R,R> combiner) 对此流的元素执行 mutable reduction操作。 |
static IntStream | concat(IntStream a, IntStream b) 创建一个懒惰连接的流,其元素是第一个流的所有元素,后跟第二个流的所有元素。 |
long | count() 返回此流中的元素数。 |
IntStream | distinct() 返回由该流的不同元素组成的流。 |
static IntStream | empty() 返回一个空的顺序 |
IntStream | filter(IntPredicate predicate) 返回由与此给定谓词匹配的此流的元素组成的流。 |
OptionalInt | findAny() 返回一个描述流的一些元素的 |
OptionalInt | findFirst() 返回描述此流的第一个元素的 |
IntStream | flatMap(IntFunction<? extends IntStream> mapper) 返回由通过将提供的映射函数应用于每个元素而产生的映射流的内容来替换该流的每个元素的结果的流。 |
void | forEach(IntConsumer action) 对此流的每个元素执行操作。 |
void | forEachOrdered(IntConsumer action) 对此流的每个元素执行一个操作,保证每个元素按遇到顺序处理,以便具有定义的遇到顺序的流。 |
static IntStream | generate(IntSupplier s) 返回无限顺序无序流,其中每个元素由提供的 |
static IntStream | iterate(int seed, IntUnaryOperator f) 返回有序无限连续 |
PrimitiveIterator.OfInt | iterator() 返回此流的元素的迭代器。 |
IntStream | limit(long maxSize) 返回由此流的元素组成的流,截断长度不能超过 |
IntStream | map(IntUnaryOperator mapper) 返回由给定函数应用于此流的元素的结果组成的流。 |
DoubleStream | mapToDouble(IntToDoubleFunction mapper) 返回一个 |
LongStream | mapToLong(IntToLongFunction mapper) 返回一个 |
<U> Stream<U> | mapToObj(IntFunction<? extends U> mapper) 返回一个对象值 |
OptionalInt | max() 返回 |
OptionalInt | min() 返回 |
boolean | noneMatch(IntPredicate predicate) 返回此流的元素是否与提供的谓词匹配。 |
static IntStream | of(int... values) 返回其元素是指定值的顺序排序流。 |
static IntStream | of(int t) 返回一个包含单个元素的顺序 |
IntStream | parallel() 返回平行的等效流。 |
IntStream | peek(IntConsumer action) 返回由该流的元素组成的流,另外在从生成的流中消耗元素时对每个元素执行提供的操作。 |
static IntStream | range(int startInclusive, int endExclusive) 返回有序的顺序 |
static IntStream | rangeClosed(int startInclusive, int endInclusive) 从 |
OptionalInt | reduce(IntBinaryOperator op) 使用 associative累积函数对此流的元素执行 reduction ,并返回描述减小值的 |
int | reduce(int identity, IntBinaryOperator op) 使用提供的身份值和 associative累积功能对此流的元素执行 reduction ,并返回减小的值。 |
IntStream | sequential() 返回顺序的等效流。 |
IntStream | skip(long n) 在丢弃流的第一个 |
IntStream | sorted() 以排序顺序返回由该流的元素组成的流。 |
Spliterator.OfInt | spliterator() 返回此流的元素的拼接器。 |
int | sum() 返回此流中元素的总和。 |
IntSummaryStatistics | summaryStatistics() 返回一个 |
int[] | toArray() 返回一个包含此流的元素的数组。 |