gpt4 book ai didi

java - RandomAccess 在现代 Java 中有什么值(value)吗?

转载 作者:搜寻专家 更新时间:2023-11-01 03:21:37 24 4
gpt4 key购买 nike

有一个特殊的界面,它是documented as such (仅摘录):

Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists.

列出的实现是(从 JDK 8 开始):

ArrayList, AttributeList, CopyOnWriteArrayList, RoleList, RoleUnresolvedList, Stack, Vector

其中两个(实际上是后两个)在您今天生成的任何代码中都已过时。

现在,作为警告,我的研究还不够广泛,以至于我一时兴起问这个问题。目前是否有任何用例表明需要使用或什至实现此接口(interface)?

最佳答案

此标记接口(interface)可能对算法实现者有用。实现此接口(interface)的列表可以与没有随机访问的列表进行不同的处理。

例如,“如果列表是随机访问的,则使用二进制搜索,否则使用线性搜索。”

一个例子是 Guava's Lists.transform :

public static <F, T> List<T> transform(
List<F> fromList, Function<? super F, ? extends T> function) {
return (fromList instanceof RandomAccess)
? new TransformingRandomAccessList<F, T>(fromList, function)
: new TransformingSequentialList<F, T>(fromList, function);
}

关于java - RandomAccess 在现代 Java 中有什么值(value)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29155764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com