简体   繁体   English

Java 和集合:何时使用数组链表?

[英]Java and collections:When to use a linked list of arrays?

What would be the advantage of using a linked list of arrays?使用数组链表有什么好处?

I have read that if the data is large and insertions are allowed (for example, a text buffer), then a common halfway measure is to use a linked list of arrays .我读过,如果数据很大并且允许插入(例如,文本缓冲区),那么一个常见的中途措施是使用数组的链表。
But there was no explanation on what is the benefit of using such a structure.但是没有解释使用这种结构有什么好处。

Or at least I did not get it.或者至少我没有得到它。

So what would be the gain in using a linked list of arrays and when?那么使用数组链表的好处是什么?何时使用?

With Linked list of arrays you can easily combine both the advantages of linked lists and static arrays.使用数组链表,您可以轻松地结合链表和静态数组的优点。

Check this question.检查这个问题。

Think about this problem:想想这个问题:

Imagine a (literal) stack of plates If the stack gets too high, it might topple There- fore, in real life, we would likely start a new stack when the previous stack exceeds some threshold Implement a data structure SetOfStacks that mimics this SetOf- Stacks should be composed of several stacks, and should create a new stack once the previous one exceeds capacity (Cracking Tech Code Interview)想象一下(字面意义的)一叠盘子 如果堆栈太高,它可能会倒塌 因此,在现实生活中,当前一叠超过某个阈值时,我们可能会开始一个新的叠堆栈应该由多个堆栈组成,并且在前一个堆栈超出容量时应创建一个新堆栈(Cracking Tech Code Interview)

No need to create custom data structure if unnecessarily.如果没有必要,无需创建自定义数据结构。 Beacause,as Linked List of Array, there's no difference between HashMap.因为作为数组的链表,HashMap 没有区别。

I can think of opposite situation: array of linked lists (actually hashmap) where every array element is a bucket that points to the list that can dynamically grow.Think of Dictionary that is being built and new words are added but the number of letters is constant.我可以想到相反的情况:链表数组(实际上是 hashmap),其中每个数组元素都是一个桶,指向可以动态增长的列表。想想正在构建的字典并添加新单词,但字母数是持续的。

Some example of what you describe can be a skip list based solution I can also think of some efficient hashing or implementing a dynamic queue of constant sets of batch jobs.您所描述的一些示例可以是基于跳过列表的解决方案,我也可以想到一些有效的散列或实现批处理作业的常量集的动态队列。

I think it gives an ability to insert a new portion of data (quite big, might be) simply as a new array into a list, without touching existing arrays in the list which go after an inserting position.我认为它能够将数据的新部分(相当大,可能是)简单地作为一个新数组插入到列表中,而不会触及列表中插入位置之后的现有数组。

Generally, inserting into a linked list is faster than inserting into an array, because it doesn't require shifting elements after new one.通常,插入链表比插入数组更快,因为它不需要在新元素之后移动元素。 In case you described, the benefit should even more significant because you don't need to shift all subsequent data which is large (in average) by definition.在您描述的情况下,好处应该更显着,因为您不需要移动根据定义而大(平均)的所有后续数据。

At the same time, we should take into account that access to such data structure is more complex and slower.同时,我们应该考虑到访问这种数据结构更复杂,速度也更慢。

PS Also, if speed is really critical for you, firstly I would do a small investigation with comparison of existing real implementation of data structures (ArrayList, LinkedList, arrays, etc.). PS 另外,如果速度对你来说真的很重要,首先我会做一个小调查,比较现有的数据结构(ArrayList、LinkedList、数组等)的实际实现。

When you'll need fast object obtaining from collection and you'll not be sure, that you have enough memory to allocate "whole" array (ArrayList implementation), you might write your own implementation which uses list of arrays to rapidly fast find the object you're looking for.当您需要从集合中快速获取对象并且不确定是否有足够的内存来分配“整个”数组(ArrayList 实现)时,您可能会编写自己的实现,该实现使用数组列表来快速找到您正在寻找的对象。

But there are definitely better data structures for this approach.但是这种方法肯定有更好的数据结构。

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

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