java后台数据校验框架
As a developer we use various data structures to deal with different kind of data to meet certain requirements. We need to decide which data structure is best suited for our requirements.
作为开发人员,我们使用各种数据结构来处理不同种类的数据以满足特定要求。 我们需要确定哪种数据结构最适合我们的需求。
Talking particularly for Java, we are provided with Collection of classes & interfaces which is popularly known as Collection framework. This has several useful classes & these classes have tons of methods which makes basic tasks super easy for developers. Collection Framework is a vast topic in itself. Here this article is just to give an inside view of arraylist, vectors, linkedlist, hashsets etc. which we often use from collection framework.
特别针对Java而言,我们提供了类和接口的集合,通常称为集合框架。 它有几个有用的类,这些类具有大量的方法,这些方法使基本任务对于开发人员而言非常容易。 收集框架本身就是一个巨大的话题。 在这里,本文仅是提供我们经常在集合框架中使用的arraylist,vector,链表,哈希集等的内部视图。
The internal implementation knowledge can sometimes let us decide the optimal solution for handling the data. Moving further with this article we will cover inside implementation of commonly used Collections. Here by inside implementation, what I mean is that we will be looking like what an arraylist is using in background to give us functionalities which it supports now. Lets get started.
内部实现知识有时可以使我们决定用于处理数据的最佳解决方案。 随着本文的深入,我们将介绍常用Collections的内部实现。 在这里,通过内部实现,我的意思是,我们将看起来就像一个数组列表在后台使用的那样,以提供我们现在支持的功能。 让我们开始吧。
1. ArrayList (1. ArrayList)
One of the widely used collection for handling data is Arraylist. Whenever we have requirement to store data in list, first choice most of us make is an arraylist. An arraylist internally uses dynamic array to store the data. As we know, an arraylist can have any no. of data elements in it irrespective of its size as the size of arraylist can be changed dynamically.
用于处理数据的广泛使用的集合之一是Arraylist。 每当我们需要将数据存储在列表中时,我们大多数人的首选就是数组列表。 arraylist在内部使用动态数组存储数据。 众所周知,arraylist可以有任何no。 数据元素的大小无关,因为arraylist的大小可以动态更改。
A dynamic array resizes itself (according to some strategy) as soon as it becomes out of index to accommodate new element inside it. Here in arraylist, the size increase by half of the original size whenever such out of index scenario occurs.
一旦动态数组超出索引以在其中容纳新元素,动态数组就会自动调整大小(根据某种策略)。 在arraylist中,每当发生这种超出索引的情况时, 大小就会增加原始大小的一半 。
2.链表 (2. LinkedList)
LinkedList is a linear data structure where data is not stored in contagious memory locations, rather links are made to point different locations of node consisting of our data element. This is what we traditionally know about linked list as data structure.
LinkedList是一种线性数据结构,其中的数据未存储在有感染力的内存位置,而是建立了链接以指向由我们的数据元素组成的节点的不同位置。 这就是我们传统上将链表作为数据结构所了解的知识。
Linked list in Java, uses Doubly Linked List in background to store the data. A doubly linked list is having address of next node as well as previous node.
Java中的链表,在后台使用双链表来存储数据。 双链表具有下一个节点以及上一个节点的地址。
3.向量 (3. Vector)
Like Arraylist, vector is also a linear data structure which can accommodate & resize its size dynamically. Vector also uses dynamic array in background. Now a question arises that what is the difference between an arraylist & vector if both fulfilling the same purpose ?
像Arraylist一样,vector也是一个线性数据结构,可以动态适应和调整其大小。 向量还在背景中使用动态数组 。 现在出现一个问题,如果数组列表和向量都实现相同的目的,它们之间有什么区别?
One of the major difference is, Vector is thread safe while arraylist is not. Though we can make it thread safe but thats an another topic to discuss. Talking in terms of dynamic array they use in background, the difference is in strategy array uses to resize itself. In vector array resizes itself by doubling the size of itself as soons as the out of index scenario occurs whereas in arraylist array size increases by size half of the original one as discussed above.
主要区别之一是, Vector是线程安全的,而arraylist不是。 尽管我们可以使其线程安全,但这是另一个需要讨论的话题。 说到它们在后台使用的动态数组,区别在于策略数组用于调整自身大小。 在向量数组中 ,一旦发生索引不足的情况,它就会通过将自身大小加倍来调整自身大小,而在数组列表中,数组大小会像上面讨论的那样增加原始大小的一半。
4. HashSet (4. HashSet)
We often need Collection to have unique data in it & Hashset comes to our mind. Here a question arises, how does a hashset know that if the element is already present inside it, because iterating each time to know if the element is present in it will surely degrade the performance.
我们经常需要Collection来包含唯一数据,并且我们想到了Hashset。 这里出现一个问题,即哈希集如何知道元素是否已存在于其中,因为每次都重复一次以了解元素中是否存在元素,这肯定会降低性能。
Interestingly, HashSet uses a HashMap behind the scene. Whenever we create any instance for hashset, a hashmap instance is also created. Now whenever we insert any value, it is stored as key in HashMap. Thus while inserting any new element in Hashset it becomes quite optimized to know that whether the element is already present or not. LinkedHashSet uses a doubly linked list for maintaining insertion order in set, which is not the case in Hashset as insertion order is not retain there.
有趣的是,HashSet在后台使用了HashMap 。 每当我们为哈希集创建任何实例时,也会创建一个hashmap实例。 现在,无论何时插入任何值,它都将作为键存储在HashMap中。 因此,当在Hashset中插入任何新元素时,它将变得非常优化,可以知道该元素是否已经存在。 LinkedHashSet使用双链表来维护set中的插入顺序,而Hashset中不是这种情况,因为插入顺序未保留在其中。
5. HashMap (5. HashMap)
Hashmap as we all know stores data in key-value pair. But how do it do it ? It internally either uses array or linked list generally to store data. The keys provided by us is used to get a hashcode by Hashing. Now this hashcode can be the index of bucket which is containing base address for the list containing the data (here we are assuming that linked list data structure is used). Again Hashing is another topic that can be discuss in detail. An image is given below for having an idea for this.
众所周知,Hashmap将数据存储在键值对中。 但是如何做到的呢? 它内部通常使用数组或链接列表来存储数据。 我们提供的密钥用于通过Hashing获取哈希码。 现在,此哈希码可以是存储桶的索引,该存储桶包含包含数据的列表的基地址(这里我们假设使用链表数据结构)。 同样,散列是另一个可以详细讨论的主题。 下面给出了一个图像,以供您参考。
Hashmap working 哈希图工作Here we can see that the key is for eg. “Lisa Smith”. Now this key is converted to a hash value. In our case the value obtained is “001”. Now this 001 is an index of bucket which is having header address of linked list which is containing our data.
在这里我们可以看到密钥是例如。 “丽莎·史密斯”。 现在,此键转换为哈希值。 在我们的情况下,获得的值为“ 001”。 现在,该001是存储区的索引,该存储区的链表头地址包含我们的数据。
Thus these are some data structures which are used internally in Collections of Java. There are more Collections like TreeSet, TreeMap, Hashtable etc. Some of these collections either uses same concepts or can be discussed in detail in another article, for eg. in case of HashTable which can take an entire article to discuss.
因此,这些是Java集合内部使用的一些数据结构。 还有更多的集合,例如TreeSet,TreeMap,Hashtable等。其中一些集合使用相同的概念,或者可以在另一篇文章中进行详细讨论,例如。 如果是HashTable,则可能需要整篇文章进行讨论。
Thanks for reading :-)
谢谢阅读 :-)
references:
参考资料:
翻译自: https://medium.com/swlh/data-structures-in-background-java-collection-framework-2555463226d0
java后台数据校验框架