Java 8 如何将流转换为数组大小? [英] Java 8 how stream to array size works?

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

问题描述

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

如何自动将大小作为流的大小?

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

推荐答案

Stream API 是围绕 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

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:

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

… using the provided generator function to allocate the returned array, as well as any additional arrays that might be required for a partitioned execution or for resizing.

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

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