Java Collection list, set, map summary + hashmap/hashtable differences

Source: Internet
Author: User
Tags comparable

List: (ordered, can be repeated) by subscript index----ArrayList variable array, randomly find----linkedlist linked list, any position insertion delete fast----Vector efficiency is lower than ArrayList, but can be used for multi-threaded synchronization

Set: (unordered, cannot be duplicated)set has a maximum of one null element because it cannot be duplicated----HashSet no sorting, no repetition(sequential random)----Linkedhashset by Insert Sort, no Duplicates(by insert order)----TreeSet implements the comparable interface, objects can be compared(Ascending according to the size of the Insert value) (sorted by binary tree)

Map:(The key value is similar to the subscript in the list, but the map subscript is any type, the list subscript is just the int type)----HASHMAP location, insert delete a map funny(sequential random) out of sync, allow null value--null key----Hashtablesynchronous, NULL key value not allowed----Linkedhashmap sorted by Insertion Order(by insert order)----TREEMAP is efficient in traversing the ordered key values and implements the comparable interface(Ascending by key value)(sorted by binary tree)

Hashmap, Hashtable are both an array and a linked list (in the data structure called "linked list hash")the difference between Hashtable and HashMap: The method in----Hashtable isSync, and the methods in HashMap are not synchronized by default. Neither key nor value is allowed in the----hashtableNull value, and only one null key is allowed in HashMap, and the value can be null. ----HASHMAPEfficiency----inheritance is different than Hashtable high.
public  class  Hashtable extends  Dictionary implements  Map 
public  Span style= "BACKGROUND-COLOR:INHERIT; Color:rgb (0,0,255) ">class  HashMap extends
 abstractmap implements  Map 
----Hash ValueUsing different hashtable, the hashcode of the object is used directly, while the HashMap recalculates the hash value, and uses and replaces the modulo  The default size of the hash array----Hashtable is 11, and the increment is old*2+1.    The default size of the hash array in HashMap is 16, and must be a 2 index. The----Hashtable and hashmap all used Iterator. And for historical reasons, Hashtable also used the enumeration waySpecific use:
Map map = new HashMap (), Map.put ("A", "N"), Map.put ("B", "n"), Map.put ("C", "N"); 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 do the Java classes need Hashcode? (The role of Hashcode)

In general, there are two types of collections (Collection) in Java, one is list, and the other is set. Do you know the difference between them? The elements within the set are ordered, the elements can be repeated, the latter elements are unordered, but the elements are not repeatable. So here is a more serious problem: to ensure that the elements do not repeat, can two elements should be repeated based on what to judge? This is the Object.Equals method. However, if each additional element is checked once, the number of elements that are added to the collection is much more numerous when the element is many. That is, if there are now 1000 elements in the collection, then the 1001th element joins the collection, it calls the Equals method 1000 times. This obviously will significantly reduce efficiency.

Thus, Java uses the principle of a hash table. A hashing algorithm, also known as a hashing algorithm, is a direct assignment of data to an address based on a particular algorithm. The hashing algorithm is not described in detail here. It is easy to understand that the Hashcode method actually returns an image of where the object is stored.

This way, when the collection is adding a new element, the Hashcode method of the element is called to locate the storage location where it should be placed. If there are no elements in this position, it can be stored directly in this position, no further comparisons are made, and if there is already an element in this position, then the Equals method of calling it is compared with the new element, the same is not saved, the difference indicates that there is a conflict, and the hash table has a concrete solution to the conflict. , but will eventually save the new element in the appropriate location. In this way, the number of actual calls to the Equals method is greatly reduced, almost only one or two times.



1. An equal object must have an equal hash code (or hash code).
2. If the hashcode of two objects are the same, they are not necessarily the same. (hashcode () equal equals () are not necessarily equal;equals () equal hashcode () are certainly equal)

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



Related Article

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.