Collection MAP Java Data structure

Source: Internet
Author: User

Collection Map Frame Map

A collection of interface objects for the Collection interface
├listSub-InterfaceSequentially save repeatable by entering order
│├linkedlist interface Implementation Class List insert Delete no synchronization thread unsafe
│├arraylist interface Implementation class array random access no synchronization thread insecure
│└vector interface implements class array synchronization thread safety
│└stack
└setSub-InterfaceReceive only once, and do an internal sort

├hashset

│└linkedhashset
└treeset

For List, the concern is the order, which ensures that elements are maintained in a specific order (allowing the same elements), and that this interface provides precise control over where each element is inserted. The user is able to access the elements in the list using an index (where the element is positioned in the list, similar to an array subscript).

For set, it is only concerned about whether an element belongs to set (it is not allowed to have the same element), and does not care about its order.

A collection of MAP interface key-value pairs
├hashtable interface Implementation class synchronization thread safety
├hashmap interface implementation class is not synchronized thread insecure

│├linkedhashmap

│└weakhashmap

├treemap
└identifyhashmap


For map, the most important feature is the key-value mapping, and the one by one mapping, the key can not be repeated, the value is OK, so is the key to index the value. Method put (Object key, object value) adds a "value" (What you Want) and the "Key" (key) associated with the value (using it to find). Method get (Object key) returns the value associated with the given "key".

Map also saves one copy of each element, but this is based on the "key", and the map has a built-in sort, so it doesn't care about the order in which the elements are added. If the order of adding elements is important to you, you should use Linkedhashset or Linkedhashmap.

For efficiency, Map finds elements that are significantly faster than ArrayList because of hashed hashes.

Excerpt: Several specific differences

The difference between HashMap and Hashtable
    • HashMap is not thread-safe
    • HashTable in HashMap method, so it is thread-safe, but slower.
    • Another difference is that the HashMap iterator (Iterator) is a fail-fast iterator, and Hashtable's enumerator iterator is not fail-fast. So when there are other threads that change the structure of the HashMap (add or remove elements), the concurrentmodificationexception will be thrown, but the remove () of the iterator itself The Concurrentmodificationexception method removes the element without throwing an exception. But this is not a certain behavior, depends on the JVM. This one is also the difference between enumeration and iterator.
HashMap and Concurrenthashmap

As can be seen from the Concurrenthashmap code, it introduces a concept of "segmented lock", which can be understood as dividing a large map into n small Hashtable, according to Key.hashcode () to decide which Hashtable to put the key.

In Concurrenthashmap, it is the time to divide the map into n segment,put and get, which is now calculated based on Key.hashcode () to which segment:

Arraylist,vector,linkedlist

LinkedList primarily guarantees the order of elements that are thread insecure

Arraylist,vector are stored in a contiguous array, but the ArrayList thread is unsafe and the vector thread is safe.

Article reference: http://blog.csdn.net/zccst/article/details/5056920

Collection MAP Java Data structure

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.