Stream流
生成流
| 要转化的容器 |
生成方式 |
描述 |
| 散开的元素 |
Stream<T> Stream.of(T ...Values) |
Stream接口的静态方法 |
| 数组 |
Stream<T> stream(T[] array) |
Arrays工具类的静态方法 |
| Collection集合 |
Stream<E> stream() |
Collection接口的默认方法 |
| Collection集合 |
Stream<E> parallelStream() |
并行计算流,多核CPU支持 |
| Map集合 |
由entrySet、keySet、values方法先得到collection集合 |
转化Collection集合后间接得到 |
中间操作
| 方法名 |
描述 |
static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) |
合并a、b两个流 |
Stream<T> filter(Predicate<? super T> predicate) |
过滤 |
Stream<T> limit(long maxSize) |
截取从0到maxSize个 |
Stream<T> skip(long n) |
跳过n个到最后 |
Stream<T> distinct() |
去重(使用equals方法判断是否重复) |
Stream<T> sorted() |
自然排序 |
Stream<T> sorted(Comparator<? super T> comparator) |
比较器排序 |
转化流操作
| 方法名 |
描述 |
<R> Stream<R> map(Function<? super T, ? extends R> mapper) |
将T类型流转化为R类型的流 |
IntStream mapToInt(ToIntFunction<? super T> mapper) |
将T类型流转化为IntStream流 |
DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper) |
将T类型流转化为DoubleStream流 |
LongStream mapToLong(ToLongFunction<? super T> mapper) |
将T类型流转化为LongStream流 |
原始流
| 方法名 |
描述 |
Xxx sum() |
求和 |
XxxSummaryStatistics summaryStatistics() |
获取汇总统计对象 |
Optional
此类主要用途是在方法声明处检测空指向异常
| 方法名 |
描述 |
static <T> Optional<T> of(T value) |
获取Optional对象,不允许内容是null |
static <T> Optional<T> ofNullable(T value) |
获取Optional对象,允许内容是null,但是调用get方法会报找不到异常 |
public T get() |
获取Optional对象中存储的内容 |
boolean isEmpty() |
判断Optional对象存储内容是否为null,是返回true |
boolean isPresent() |
判断Optional对象存储内容是否为null,是返回false |
终结操作
| 方法名 |
描述 |
void forEach(Consumer<? super T> action) |
遍历 |
long count() |
统计个数 |
收集操作
| 方法名 |
描述 |
<R, A> R collect(Collector<? super T, A, R> collector) |
由收集器收集 |
收集器
由Collectors工具类生成Collector收集器
| 方法名 |
描述 |
static <T> Collector<T, ?, List<T>> toList() |
获取List收集器 |
static <T> Collector<T, ?, Set<T>> toSet() |
获取Set收集器 |
static <T, K, U> Collector<T, ?, Map<K,U>> toMap(Function keyMapper, Function valueMapper) |
获取Map收集器,前面是key的格式,后面的value的格式 |
Comments NOTHING