Java Collection List, Set, Map summary + HashMap/Hashtable difference, hashmaphashtable

Source: Internet
Author: User

Java Collection List, Set, Map summary + HashMap/Hashtable difference, hashmaphashtable
List: (ordered, can be repeated) through the subscript index ---- ArrayList variable array, random search ---- random List linked List, insert and delete at any location faster ---- Vector efficiency is lower than arraylist, but can be used for multi-thread synchronization

Set :( unordered, not repeated) a set can have a maximum of null elements, because it cannot be repeated ---- HashSet is not sorted, not repeated (random order) ---- sorted HashSet is sorted by insert, no duplicates (in insert order) ---- the Comparable interface is implemented by TreeSet. objects can be compared (in ascending order of insert values) (in binary tree order)

Map: (the key value is similar to the subscript in the list, but the map subscript is of any type, and the list subscript is of the int type.) ---- HashMap locating, inserting and deleting a ing is funny (random order) and not synchronized, allow null value -- null key ---- Hashtable synchronization, do not allow null key value ---- sort hashmap by insertion Order (by insertion order) ---- TreeMap is efficient in traversing and sorting key values in order, implement the Comparable interface (sort by key value in ascending order) (sort by binary tree)

Hashmap and Hashtable are a combination of arrays and linked lists (called "linked list hash" in the data structure). The difference between hashTable and hashMap: ---- the methods in Hashtable are synchronous, the methods in HashMap are not synchronized by default. ---- Keys and values in Hashtable do not allow null values, while HashMap only allows one null key. The value can be null. ---- HashMap is more efficient than Hashtable ---- inheritance is different.

public class Hashtable extends Dictionary implements Map
public class HashMap  extends AbstractMap implements Map
---- Different hash values, Hashtable directly uses the hashCode of the object; while HashMap re-calculates the hash value, and replaces the modulo ---- the default size of the hash array in Hashtable is 11, the added method is old * 2 + 1. The default size of the hash array in HashMap is 16, and it must be an index of 2. ---- Both Hashtable and HashMap use Iterator. For historical reasons, Hashtable also uses the Enumeration method:
Map map = new HashMap (); map. put ("a", "100"); map. put ("B", "200"); map. put ("c", "200"); Iterator iter = map. entrySet (). iterator (); while (iter. hasNext () {Map. entry entry = (Map. entry) iter. next (); Object key = entry. getKey (); Object val = entry. getValue ();} // or for (Map. entry entry: map. entrySet () {Object key = entry. getKey (); Object value = entry. getValue ();}


Why does a Java class need hashCode? (Role of hashCode) 

In general, collections in Java have two types: List and Set. Do you know the differences between them? The elements in the former set are ordered, and the elements can be repeated. The latter elements are unordered, but the elements cannot be repeated. So here is a serious problem: to ensure that the elements are not repeated, what is the basis for determining whether the two elements are repeated? This is the Object. equals method. However, if each added element is checked once, when there are many elements, the number of times that the elements added to the set are compared is very large. That is to say, if there are already 1000 elements in the Set, it will call the 1,001st equals method when 1000 elements are added to the set. This will obviously greatly reduce the efficiency.

Therefore, Java uses the principle of hash tables. A hash algorithm is also called a hash algorithm. It directly specifies an address based on a specific data algorithm. We will not detail the hash algorithm here. It can be simply understood that the hashCode method actually returns the image of the object storage location.

In this way, when a set needs to add a new element, the hashCode method of this element is called first to locate the storage location where it should be placed. If there are no elements in this position, it can be directly stored in this position without any comparison. If there are already elements in this position, the equals method is called to compare with the new element. If it is the same, it will not be saved. If it is different, it indicates a conflict. The hash list provides a specific solution to the conflict, however, the new elements will be stored in the appropriate location. In this way, the actual number of calls to the equals method is greatly reduced, almost only one or two.



1. An Equal object must have an equal hash code (or hash code ).
2. If two objects have the same hashCode, they are not necessarily the same. Equals () with the same hashCode () are not necessarily equal; hashCode () with the same equals () must be equal)

Please indicate the source: http://blog.csdn.net/df1012890048/article/details/38672155




In java, the set List and Map set hashcode and hashMap are used when everything is used.

There are several core classes you mentioned above. You should want to use things like a collection linked list.
I will give you an example of life to help you understand.
List: a sequence where arbitrary data is stored. It is not ordered and can be repeated.
Its subclass is ArrayList.
Roster: Zhang San, Li Si, Wang Wu, Zhang San, Saturday ....

Map: Key-value pairs. When used, they are accessed in key-value mode. They are not sorted and cannot be repeated.
Subclass: HashMap and TreeMap
Note: Zhang San: 100, Li Si: 80, Wang Wu: 90 ......

Set: sequence, same as List, but not repeated
Subclass: HashSet
All courses: mathematics, Chinese, English ....

As for the hashCode you mentioned, it is a method of these classes to obtain the hash values of these sets.

How to Use HashMap, hashtable, list, set, map, do not be different,

Hashmap and hashtable are map-based implementation. map is stored in key-value pairs, that is, a key corresponds to a value list. stored in an ordered and repeatable Data set stores unordered and non-duplicated data. data
 

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.