简体   繁体   English

Java / Android比ArrayList快什么?

[英]Java /Android what is faster than ArrayList?

What is faster than ArrayList<String> in Java ? 什么比Java中的ArrayList<String>快? I have a list which is of an undefined length. 我有一个未定义长度的列表。 (sometimes 4 items, sometimes 100). (有时4个项目,有时100个)。

What is the FASTEST way to add and get it from any list ? 从任何列表添加和获取它的最快方法是什么? arrayList.add(string) and get() are very slow. arrayList.add(string)get()非常慢。

Is there a better way for this? 有更好的方法吗? ( string s[] and then copyArray are the slowest?) string s[]然后是copyArray最慢?)

Faster for what? 为了什么更快?

"basically arraylist.add(string) and get() is very slow." “基本上arraylist.add(string)和get()非常慢。” - based on what evidence? -根据什么证据? And compared to what? 和什么相比? (No need for the word 'basically' here - it's a high tech "um".) I doubt that ArrayList is the issue with your app. (这里不需要“基本”一词-它是高科技的“ um”。)我怀疑ArrayList是应用程序的问题。 Profiling your code is the only way to tell whether or not you're just guessing and grasping at straws. 对代码进行概要分析是告诉您是否只是在猜测和掌握的唯一方法。

Even an algorithm that's O(n^2) is likely to be adequate if the data set is small. 如果数据集很小,那么即使是O(n ^ 2)的算法也足够了。

You have to understand the Big-Oh behavior of different data structures to answer this question. 您必须了解不同数据结构的Big-Oh行为才能回答此问题。 Adding to the end of an ArrayList is pretty fast, unless you have to resize it. 除非必须调整其大小,否则添加到ArrayList的末尾非常快。 Adding in the middle may take longer. 在中间添加可能需要更长的时间。

LinkedList will be faster to add in the middle, but you'll have to iterate to get to a particular element. 在中间添加LinkedList会更快,但是您必须迭代才能到达特定元素。

100 items is not very many. 100件不是很多。 Your bottleneck is elsewhere. 您的瓶颈在别处。

Both add() to end of list and get() should run in O(1). add()到列表末尾和get()都应在O(1)中运行。 And since length is undefined, you can't use a fixed length array. 而且由于长度是不确定的,因此不能使用固定长度的数组。 You can't do any better I'm afraid. 恐怕您无法做得更好。

add(int index, E element) takes linear time for worst case though if that's why you think it's slow. add(int index, E element)在最坏的情况下需要线性时间,但是如果这就是为什么您认为它很慢。 If that is the case, either use Hashtable (insertion takes constant time) or TreeMap (insertion takes logarithmic time). 如果是这种情况,请使用Hashtable(插入需要固定时间)或TreeMap(插入需要对数时间)。

Take a look the Jodd Utilities . 看看Jodd实用程序 They have some collections that implement ArrayList but on primatives (jodd/util/collection/), such as IntArrayList. 他们有一些实现ArrayList的集合,但这些集合在基元(jodd / util / collection /)上实现,例如IntArrayList。 So if you're creating a ArrayList of int, float, double, etc.. it will be faster and consume less memory. 因此,如果您要创建一个由int,float,double等组成的ArrayList,它将更快并且消耗更少的内存。

Even faster than that is what they call a FastBuffer, which excels at add() and can provide a get() at O(1). 他们称之为FastBuffer的速度甚至更快,它擅长add()并可以在O(1)提供get()。

The classes have little interdependency, so it's easy to just drop in the class you need into your code. 这些类之间几乎没有相互依赖性,因此很容易将所需的类放入代码中。

You can use javolution library. 您可以使用javolution库。 http://javolution.org http://javolution.org

http://javolution.org/target/site/apidocs/javolution/util/FastList.html http://javolution.org/target/site/apidocs/javolution/util/FastList.html

ist much faster than arraylist ;) ist比arraylist快得多;)

尝试使用哈希表会更快

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM