The similarities and differences of vectors, ArrayList and Hashtable HashMap [ZZ]

Source: Internet
Author: User
Tags constant

Linear table, linked list, hash table is a commonly used data structure, in the Java development, JDK has provided us with a series of corresponding classes to achieve the basic data structure. These classes are in the Java.util package. This paper attempts to explain the roles of the classes and how to use them correctly by a simple description.

Collection
├list
│├linkedlist
│├arraylist
│└vector
│└stack
└set
Map
├hashtable
├hashmap
└weakhashmap

Collection interface
Collection is the most basic set interface, and a collection represents a set of object, the collection element (Elements). Some collection allow the same elements while others do not. Some can be sorted and others not. The Java SDK does not provide classes that directly inherit from collection, and the Java SDK provides classes that inherit from collection such as list and set.
All classes that implement the collection interface must provide two standard constructors: parameterless constructors are used to create an empty collection, and a constructor for a collection parameter is used to create a new collection. This new collection has the same element as the incoming collection. The latter constructor allows the user to replicate a collection.
How to traverse every element in the collection. Regardless of the actual type of collection, it supports a iterator () method that returns an iteration that can be used to access each element of the collection one at a time. Typical usage is as follows:
Iterator it = Collection.iterator (); To get an iterative child
while (It.hasnext ()) {
Object obj = It.next (); Get the next element
}
The two interfaces that are derived from the collection interface are list and set.

List interface
The list is an ordered collection, using this interface to precisely control where each element is inserted. The user can access the elements in the list, similar to the Java array, by using the index (the position of the element in the list, similar to the array subscript).
Unlike the set mentioned below, the list allows the same elements.
In addition to the iterator () method with the collection interface prerequisites, the list also provides a listiterator () method that returns a Listiterator interface, compared to the standard iterator interface, There are a number of add () listiterator that allow adding, deleting, setting elements, and traversing forward or backwards.
Common classes that implement the list interface are Linkedlist,arraylist,vector and stack.

LinkedList class
LinkedList implements the list interface, allowing null elements. Additionally LinkedList provides additional Get,remove,insert methods at LinkedList's header or tail. These operations enable LinkedList to be used as stacks (stack), queues (queue), or bidirectional queues (deque).
Note that LinkedList does not have a synchronization method. If multiple threads access a list at the same time, you must implement access synchronization yourself. One workaround is to construct a synchronized list when the list is created:
List List = Collections.synchronizedlist (new LinkedList (...));

ArrayList class
ArrayList implements a variable sized array. It allows all elements, including null. ArrayList is not synchronized.
The Size,isempty,get,set method runs at a constant time. However, the Add method cost is the allocated constant, and the time of O (n) is required to add n elements. The other methods run at a linear time.
Each ArrayList instance has a capacity (Capacity), which is the size of the array used to store the elements. This capacity can automatically increase as new elements are added, but the growth algorithm is not defined. When you need to insert a large number of elements, you can call the Ensurecapacity method before inserting to increase the capacity of the ArrayList to increase the insertion efficiency.
Like LinkedList, ArrayList is also unsynchronized (unsynchronized).

Vector class
Vectors are very similar to ArrayList, but vectors are synchronized. Iterator created by vector, although the same interface was created with ArrayList, but because vectors are synchronized, and when one iterator is created and is being used, another thread changes the state of the vector (for example, Add or remove some elements), the concurrentmodificationexception is thrown when the iterator method is invoked, so the exception must be caught.

Stack class
Stack inherits from the vector, implementing a LIFO stack. Stack provides 5 additional methods that allow vectors to be used as stacks. Basic push and Pop methods, and Peek method to get the top of the stack elements, empty method test stack is empty, the search method detects an element in the stack position. Stack is just created after stack is empty.

Set interface
A set is a collection that contains no duplicate elements, that is, any two elements E1 and E2 have E1.equals (E2) =false,set have a maximum of one null element.
Obviously, the constructor of the set has a constraint, and the incoming collection parameter cannot contain duplicate elements.
Note: You must be careful to manipulate variable objects (mutable object). If a variable element in a set changes its state, causing the Object.Equals (Object) =true to cause some problems.

Map interface
Note that the map does not inherit the collection interface, and the map provides a mapping of key to value. A map cannot contain the same key, and each key can map only one value. The map interface provides a view of 3 collections, which can be treated as a set of key sets, a set of value sets, or a set of key-value mappings.

Hashtable class
Hashtable inherits the map interface and implements a hash table of key-value mappings. Any object that is Non-null (non-null) can be a key or value.
Adding data using put (key, value), fetching data using get (key), the time cost of these two basic operations is constant.
Hashtable adjusts performance by initial capacity and load factor two parameters. Typically, the default load factor 0.75 achieves a better balance of time and space. Increasing the load factor saves space but the corresponding lookup time increases, which can affect operations like get and put.
Using Hashtable for example, put 1,2,3 into Hashtable, their key is "one", "two", "three":
Hashtable numbers = new Hashtable ();
Numbers.put ("One", New Integer (1));
Numbers.put ("Two", New Integer (2));
Numbers.put ("Three", New Integer (3));
To remove a number, such as 2, use the corresponding key:
Integer n = (integer) numbers.get ("two");
System.out.println ("two =" + N);
Because an object that is a key will determine the location of its corresponding value by calculating its hash function, any object that is a key must implement the Hashcode and Equals methods. The hashcode and Equals methods inherit from the root class object, and if you use a custom class as a key, be very careful, as defined by the hash function, if two objects are the same, that is, Obj1.equals (OBJ2) =true, Their hashcode must be the same, but if two objects are different, their hashcode is not necessarily different, and if the hashcode of two different objects is the same, this phenomenon is called conflict, and the conflict causes the time overhead of manipulating the hash table, so as to define the hashcode () method to speed up the operation of the hash table.
If the same object has a different hashcode, the operation of the hash table will have unexpected results (the expected GET method returns null), to avoid this problem, you need to keep in mind one: you have to copy both the Equals method and the Hashcode method, not just one.
The Hashtable is synchronized.

HashMap class
HashMap is similar to Hashtable, except that HashMap is unsynchronized and allows NULL, that is, null value and null key. , but when HashMap is treated as collection (the values () method returns collection), its iterative child operation time cost is proportional to the capacity of the HashMap. Therefore, if the performance of an iterative operation is significant, do not set the HASHMAP initialization capacity too high, or the load factor too low.

Weakhashmap class
Weakhashmap is an improved hashmap, which implements a "weak reference" to key, and if a key is no longer referenced externally, the key can be reclaimed by GC.

Summarize
If you are involved in stacks, queues, and other operations, you should consider using the list, for the need to quickly insert, delete elements, should use LinkedList, if you need to quickly randomly access elements, you should use ArrayList.
If the program is in a single-threaded environment, or if access is only done in one thread, it is more efficient to consider a class that is not synchronized, and should use a synchronized class if multiple threads might be working on a class at the same time.
Special attention should be paid to the operation of the hash table, where the object of the key is correctly hashcode the Equals and the method.
Try to return the interface instead of the actual type, such as returning a list instead of ArrayList, so that if you need to replace ArrayList with LinkedList later, the client code does not have to change. This is for abstract programming.

Sync Sex
Vectors are synchronized. Some of the methods in this class ensure that the objects in the vector are thread safe. ArrayList is asynchronous, so the objects in ArrayList are not thread safe. Because the requirements of synchronization can affect the efficiency of execution, it is a good choice to use ArrayList if you do not need a thread-safe collection, which avoids the unnecessary performance overhead of synchronization.
Data growth
From an internal implementation mechanism, both ArrayList and vectors use Arrays (array) to control objects in the collection. When you add elements to both types, if the number of elements exceeds the current length of the internal array they all need to extend the length of the internal array, the vector automatically increases the original one times the length of the array by default, ArrayList is the original 50%, So at the end of the collection you get a lot more space than you really need. So if you want to save a lot of data in a collection, there are some advantages to using vectors because you can avoid unnecessary resource overhead by setting the initialization size of the collection.
Usage patterns
In ArrayList and vectors, it takes the same amount of time to look up data from a specified location (through the index) or to add and remove an element at the end of the collection, which we use O (1). However, if an element is added or removed elsewhere in the collection, the time it takes to grow linearly increases: O (n-i), where n represents the number of elements in the collection, and I represents the index position of the element's increase or removal. Why is that so? It is assumed that all elements after the first and first elements of the collection will perform the action of displacement when the above actions are performed. What does all this mean?
This means that you are simply looking for elements in a particular location or adding and removing elements only at the end of the collection, so you can use vectors or ArrayList. If it's a different operation, you'd better choose a different collection action class. For example, does the Linklist collection class spend the same amount of time adding or removing elements from any location in the collection? O (1), but it is in the index the use of an element is relatively slow-o (i), where I is the location of the index. It's also easy to use ArrayList because you can simply use an index instead of creating an iterator object. Linklist also creates objects for each inserted element, and all you have to understand is that it will incur additional overhead.
Finally, in the book "Practical Java", Peter Haggar suggests using a simple array to replace vectors or ArrayList. This is particularly true for procedures that require high efficiency in implementation. The use of arrays (array) avoids synchronization, additional method calls, and unnecessary reallocation of space.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.