Java 8如何流到数组大小的工作原理? [英] Java 8 how stream to array size works?

查看:119
本文介绍了Java 8如何流到数组大小的工作原理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String[] stringArray = streamString.toArray(size -> new String[size]);

如何自动调整流的大小?

How it takes the size as stream's size automatically?

推荐答案

围绕 Stream API 8 / docs / api / java / util / Spliterator.htmlrel =noreferrer> Spliterator 这是一种高级形式的迭代器。这些可以报告某些特征 ,允许优化操作 Stream 将适用。他们也可以报告预期的元素数量,估计确切 Spliterator 将报告 SIZED 如果它事先知道元素的数量就具有特征。

The Stream API is formed around Spliterator which is an advanced form of iterators. These can report certain characteristics, allowing optimizations of the operations a Stream will apply. They may also report the expected number of elements, either estimated or exact. A Spliterator will report a SIZED characteristic if it knows the number of elements in advance.

根据封装操作,您可以使用以下方法测试 Stream 中有关其元素的知识:

You can test which knowledge about its elements a Stream has, given the encapsulated operations, using the following method:

public static <T> Stream<T> printProperties(String op, Stream<T> s) {
    System.out.print("characteristics after "+op+": ");
    Spliterator<T> sp=s.spliterator();
    int characteristics=sp.characteristics();
    if(characteristics==0) System.out.println("0");
    else {
        String str;
        for(;;) {
            int flag=Integer.highestOneBit(characteristics);
            switch(flag) {
                case ORDERED: str="ORDERED"; break;
                case DISTINCT: str="DISTINCT"; break;
                case SORTED: str="SORTED"; break;
                case SIZED: str="SIZED"; break;
                case NONNULL: str="NONNULL"; break;
                case IMMUTABLE: str="IMMUTABLE"; break;
                case CONCURRENT: str="CONCURRENT"; break;
                case SUBSIZED: str="SUBSIZED"; break;
                default: str=String.format("0x%X", flag);
            }
            characteristics-=flag;
            if(characteristics==0) break;
            System.out.append(str).append('|');
        }
        System.out.println(str);
    }
    return StreamSupport.stream(sp, s.isParallel());
}

您可以使用它来了解某些操作如何影响元素的知识。例如,当您将此方法与以下测试程序一起使用时:

You can use it to learn how certain operations influence the knowledge about the elements. E.g., when you use this method with the following test program:

Stream<Object> stream;
stream=printProperties("received from TreeSet", new TreeSet<>().stream() );
stream=printProperties("applying map", stream.map(x->x) );
stream=printProperties("applying distinct", stream.distinct() );
stream=printProperties("filtering", stream.filter(x->true) );
stream=printProperties("applying sort", stream.sorted() );
stream=printProperties("requesting unordered", stream.unordered() );

System.out.println();

stream=printProperties("received from varargs array", Stream.of("foo", "bar") );
stream=printProperties("applying sort", stream.sorted() );
stream=printProperties("applying map", stream.map(x->x) );
stream=printProperties("applying distinct", stream.distinct() );
stream=printProperties("requesting unordered", stream.unordered() );

System.out.println();

printProperties("ConcurrentHashMap.keySet().stream()",
    new ConcurrentHashMap<>().keySet().stream() );

它将打印:

characteristics after received from TreeSet: SIZED|ORDERED|SORTED|DISTINCT
characteristics after applying map: SIZED|ORDERED
characteristics after applying distinct: ORDERED|DISTINCT
characteristics after filtering: ORDERED|DISTINCT
characteristics after applying sort: ORDERED|SORTED|DISTINCT
characteristics after requesting unordered: SORTED|DISTINCT

characteristics after received from varargs array: SUBSIZED|IMMUTABLE|SIZED|ORDERED
characteristics after applying sort: SUBSIZED|SIZED|ORDERED|SORTED
characteristics after applying map: SUBSIZED|SIZED|ORDERED
characteristics after applying distinct: ORDERED|DISTINCT
characteristics after requesting unordered: DISTINCT

characteristics after ConcurrentHashMap.keySet().stream(): CONCURRENT|NONNULL|DISTINCT

as JB Nizet解释说,如果一个流事先不知道大小,它必须使用策略来收集可能包含的元素重新分配数组。正如文档说明

As JB Nizet explained, if a stream does not know the size in advance, it has to use a strategy for collecting the elements which might include reallocating arrays. As the documentation says:


...使用提供的生成器函数来分配返回的数组,以及任何分区执行或调整大小可能需要的其他数组

这篇关于Java 8如何流到数组大小的工作原理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆