In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what is the difference between ArrayList and Vector". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
21. The difference between ArrayList and Vector
Both of these classes implement the List interface (the List interface inherits the Collection interface). They are both ordered collections, that is, the positions of the elements stored in these two sets are sequential, which is equivalent to a dynamic array. We can later retrieve an element by the location index number, and the data in it is allowed to be repeated, which is the biggest difference from collections such as HashSet. Collections such as HashSet cannot retrieve elements by index number, nor do they allow duplicate elements.
The difference between ArrayList and Vector mainly includes two aspects:.
(1) synchronization:
Vector is thread-safe, that is to say, its methods are thread-synchronized, while ArrayList is not thread-safe, and its methods are not thread-synchronized. If only one thread will access the collection, it's best to use ArrayList, because it doesn't think about thread safety and is more efficient; if more than one thread accesses the collection, it's better to use Vector, because we don't have to think about and write thread-safe code ourselves.
(2) data growth:
Both ArrayList and Vector have an initial capacity. When the number of elements stored in them exceeds the capacity, it is necessary to increase the storage space of ArrayList and Vector. Each time to increase storage space, not only one storage unit is added, but multiple storage units are added. Each increase in the number of memory units in the use of memory space and program efficiency to achieve a certain balance. The default growth of Vector is twice that of the original, while the growth strategy of ArrayList is not clearly stated in the documentation (1.5 times the growth seen from the source code). Both ArrayList and Vector can set the initial space size, and Vector can also set the amount of space for growth, while ArrayList does not provide a way to set the space for growth.
Summary: that is, the growth of Vector doubled and ArrayList increased by 0.5 times.
22. The difference between HashMap and Hashtable
HashMap is a lightweight implementation of Hashtable (a non-thread-safe implementation), and they all complete the Map interface. The main difference is that HashMap allows null (null) keys (key). Because of non-thread safety, it is more efficient than Hashtable when only one thread is accessed.
HashMap allows null to be a key or value of an entry, while Hashtable does not.
HashMap removed Hashtable's contains method and changed it to containsvalue and containsKey. Because the contains method is easily misleading.
Hashtable inherits from the Dictionary class, and HashMap is an implementation of Map interface introduced by Java1.2.
The biggest difference is that Hashtable's method is Synchronize, while HashMap is not. When multiple threads access Hashtable, there is no need to synchronize its methods themselves, and HashMap must provide synchronization for it.
As far as HashMap and HashTable are concerned, there are mainly three aspects.
one。 Historical reasons: Hashtable is based on stale Dictionary classes, and HashMap is an implementation of the Map interface introduced by Java 1.2
two。 Synchronization: Hashtable is thread-safe, that is, synchronous, while HashMap is not thread-safe, not synchronous
three。 Value: only HashMap allows you to use null values as the key or value of an entry in a table
23. The difference between List and Map?
One is a collection that stores single-column data, and the other is a collection of two-column data such as keys and values. The data stored in List is sequential and allows repetition; the data stored in Map is out of order, its keys can not be repeated, and its values can be repeated.
24. Does List,Set and Map inherit from the Collection interface?
List,Set is, Map is not.
25, List, Map, Set, what are the characteristics of accessing elements?
Such questions compare the level of the examination in two aspects: one is to really understand these contents, and the other is to have a strong ability to sum up and express. )
First of all, List and Set are similar, they are a collection of single-column elements, so they have a common parent interface, called Collection. Duplicate elements are not allowed in Set, that is, there cannot be two objects that are equal (note, not just the same), that is, suppose there is an An object in the Set collection, and now I want to store another B object in the Set collection, but the B object is equal to the An object equals, then the B object cannot be stored, so the add method of the Set collection has a boolean return value when there is no element in the set. When the add method can successfully join the element, it returns true. When the collection contains an element equal to the element equals, the add method cannot join the element, and the result is false. When Set fetches elements, it cannot specify which one to take. Instead, it can only use the Iterator interface to get all the elements, and then iterate through each element one by one.
List means collections in order. Note that they are not sorted by age, size, price, and so on. When we call the add (Obje) method many times, the objects added each time are sorted in the same first-come-first-served order as if there was a queue for buying tickets at the railway station. Sometimes, you can jump the queue by calling the add (intindex,Obj e) method to specify where the current object is stored in the collection. An object can be stored repeatedly in List, and every time the add method is called, the object is inserted into the collection. In fact, instead of storing the object itself in the collection, it points to the object with an index variable in the collection. When the object is add multiple times, it means that there are multiple indexes pointing to the object in the collection, as shown in figure x. In addition to using the Iterator interface to get all the elements and iterate through each element one by one, List can also call get (index I) to specify which one to take.
Map is different from List and Set, it is a two-column collection, in which there are put methods, defined as follows: put (obj key,obj value), each storage, to store a pair of key/value, can not store duplicate key, this repeated rule is also equal by equals. If you choose, you can get the corresponding value according to key, that is, the return value of get (Object key) is the value corresponding to key. In addition, you can get all the key combinations, you can get all the value combinations, and you can get a collection of Map.Entry objects composed of key and value.
List holds elements in a specific order, but there are repeating elements. Set cannot have repeating elements, internal sorting. Map saves key-value values, and value can have multiple values.
26. Describe the storage performance and characteristics of ArrayList,Vector,LinkedList
ArrayList and Vector both use an array to store data, and the number of elements in this array is larger than the actual stored data in order to add and insert elements. They both allow elements to be indexed directly by ordinal numbers, but inserting elements involves memory operations such as array element movement, so index data is fast and data insertion is slow. Vector usually performs worse than ArrayList due to the use of the synchronized method (thread safety). While LinkedList uses two-way linked list to achieve storage, indexing data by serial number needs to be traversed forward or backward, the index becomes slow, but when inserting data, you only need to record the items before and after this item, so the insertion speed is faster.
LinkedList is also thread-unsafe, and LinkedList provides ways that LinkedList can be used as stacks and queues.
27. Remove duplicate elements from a Vector collection
Vector newVector = newVector ()
For (int iTuno Bandi)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.