Java collections: 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 finds----linkedlist linked list, no matter where the insertion is deleted fast----Vector efficiency is lower than ArrayList. But can be used for multi-threaded synchronization

Set: (unordered, cannot be repeated)set has a maximum of one null element. Since it is not possible to repeatedly----HashSet No sort, not repeatedly(sequential random)----Linkedhashset by insert sort. Do not repeat(by insert order)----TreeSet implements the comparable interface, the object can compare(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 map subscript is random type, list subscript is only int type)----HASHMAP location, insert delete a map funny(sequential random) out of sync, agree null value--null key----Hashtablesynchronization. Do not agree with null key value----Linkedhashmap sorted by Insertion Order(by insert order)----TREEMAP is efficient in traversing the ordered key values and implements the comparable interface(Ascending according to key value)(according to binary tree sort)
 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 isSyncOf    The methods in HashMap are non-synchronous by default. ----Hashtable key and value disagree.Null value, while HashMap only agrees with a null key.    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. And HashMap again calculates the hash value, and uses and replaces the modulo  The default size of the hash array----Hashtable is 11. The way to add is old*2+1.

The default size of the hash array in HashMap is 16, and must be the exponent of 2. The----Hashtable and hashmap all used Iterator. For historical reasons, Hashtable also uses the enumeration approach in detail:

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 Java classes need to be 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 in the former set are ordered. elements can be repeated; the latter elements are unordered, but the elements cannot be repeated. 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 infer? This is the Object.Equals method.

However, assuming that each element is added to be checked once, when the element is very long, then the number of elements added to the collection is much more.

That is, suppose that there are now 1000 elements in a collection. Then the 1001th element joins the collection, it calls the Equals method 1000 times. This obviously will significantly reduce efficiency.

So. 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. About the hashing algorithm. This is not specifically described here. To be able to understand this simply, the Hashcode method actually returns an image of where the object is stored.

So. When a collection is added to a new element, the Hashcode method of the element is called to locate the storage location where it should be placed.

Assuming there are no elements in this position, it can be stored directly in this position, no matter what the comparison. Assuming that there is already an element in this position, the Equals method that calls it is compared to the new element, and the same is not saved, and does not mean that there is a conflict. The hash table has a detailed workaround for the conflict, but finally the new element is saved 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 , equal objects must have equal hash codes (or hash codes).


2, assuming that two objects are hashcode same, they are not necessarily the same. equals () equal hashcode () must be equal)

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



Java collection: List, Set, map summary + hashmap/hashtable difference

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.