Android List,set,map Collection Security Set difference Concurrency Collection class performance analysis

Source: Internet
Author: User
Tags concurrentmodificationexception

For Android developers, it is necessary to learn more about Java's collection classes, which are derived primarily from the collection and map interfaces, and currently provide the list, set, and  Map of the three categories of the collection collection interface mainly have two seed classes are list and set, the main difference is that the list of objects can be repeated and set can not be repeated and map is generally key-value such a correspondence, such as our common hashmap. Array

The array storage interval is continuous and occupies a serious memory, so the space is very complex. But the binary finding time of the array is small and the complexity is O (1); The array is characterized by: easy addressing, insertion and deletion difficulties;

Linked list

The storage interval of the list is discrete, the memory is relatively loose, so the space complexity is very small, but the time complexity is very large, up to O (N). The list is characterized by difficult addressing, easy insertion and deletion.

Hash table

Can we combine the characteristics of both to make a data structure that is easy to address, insert and delete? The answer is yes, and that's the hash table we're going to mention. Hash table (hash table) not only satisfies the data search convenience, but also does not occupy too much content space, the use is very convenient.


Diagrams of various collections in Java

A collection of interface objects for the Collection interface
├list sub-interface in order to enter a sequential save repeatable
│├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
The └set sub-interface is only received once, and does an internal sort

├hashset

│└linkedhashset
└treeset list mainly has ArrayList, LinkedList, Vector and stack One, list mainly has ArrayList, LinkedList, vector and stack
With regard to the performance of these subclasses, the Android Development network analyzes the execution efficiency of the elements from insert, delete, and move, and concludes by analyzing Sun's Java source code and actual element operations:

ArrayList-is the thread insecure underlying is implemented by the array his structure mainly from the abstractlist implementation, mainly to determine the initial element of the capacity, ArrayList the biggest feature is to provide the add, get operations, of course, can be iterated through the iterator, The existence of elements can be judged by the contains method.
LinkedList-thread insecure as a doubly linked list structure, for the insertion and deletion of elements is more efficient, only need to adjust the node point, but for random lookups, the main performance depends on the list length and luck. LinkedList also provides a ArrayList get method, but it is much more complex, mainly through the next or previous method traversal, LinkedList to move the pointer.
Vector-Thread-safe, these two classes are implemented by the bottom of the array, low efficiency is relatively simple and ArrayList, mainly the implementation of the Synchronized keyword internally, the implementation of thread-safe access but the performance is somewhat reduced, At the same time, the expansion of the elements in the algorithm and ArrayList slightly different, through the structure of the capacity increment coefficient to determine.
Stack-as the operation of the stack, this time inherits from the vector, provides the push,pop and Peek methods, peek is not ejected according to the data size to get the last element object. Two, Set mainly have hashset and TreeSet

HashSet-The class is inherited from the set interface, which means that internally added elements cannot be duplicated relative to list, Hashtable inherits the map interface and implements a Key-value mapped hash table. Any object that is not empty (non-null) can be either a key or a value. Of course, from the name of the hash is the hash algorithm to achieve the prevention of conflict to prevent duplication, the overall implementation from the HASHMAP, the storage element method is similar to the corresponding key-value, through the iterator traversal, but hashset is not thread-safe.
TreeSet-This is primarily a sort of support relative to HashSet, TreeSet is implemented from the TreeMap class and is non-thread safe.

You can see that the set of the two classes are related to the map, the following is a look at the mapping (map) related to the use.

Three, Map mainly has HashMap and TreeMap HashMap and Hashtable difference

HashMap and Hashtable both implement the map interface, but decide which one to use before you start to figure out the difference between them. The main differences are: thread safety, Synchronization (synchronization), and speed.

    1. HashMap can be almost equivalent to Hashtable, except that HashMap is non-synchronized and can accept null (HashMap can accept null key value (key) and values (value), and Hashtable does not).
    2. HashMap is non-synchronized, and Hashtable is synchronized, which means that Hashtable is thread-safe, multiple threads can share a hashtable, and if there is no proper synchronization, Multiple threads are not able to share hashmap. Java 5 provides Concurrenthashmap, which is an alternative to Hashtable that is better than Hashtable extensibility.
    3. 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.
    4. Because Hashtable is thread-safe and synchronized, it is slower than HashMap in a single-threaded environment. If you don't need to sync and just need a single thread, it's better to use HashMap performance than Hashtable.
    5. HashMap cannot guarantee that the order of elements in the map will be constant over time.


HashMap-, can be empty, provides a relatively powerful implementation, such as Loadfactor can control the memory allocation when the element grows, HashMap is also non-thread-safe. Principle:

Android List,set,map Collection Security Set difference Concurrency Collection class performance analysis

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.