Java Collection Class performance analysis for Android development

Source: Internet
Author: User

   for Android developers, it is necessary to learn more about the collection classes of Java, mainly from the collection and map interfaces, which currently provide a collection of the three major categories of list, set and map. Today a simple analysis of their subclasses ' performance in standard and multi-threaded situations.

Collection interface mainly has two seed classes are list and set, the main difference is that the list of objects can be duplicated, and set can not be repeated, and map is generally key-value such a correspondence, such as our common hashmap.

First, the List mainly has ArrayList, LinkedList, vector and stack about the performance of these subclasses, the Android development network from the insertion, deletion, mobile and other aspects according to the execution efficiency of the elements to do one-by-one analysis, through the analysis of sun Java source code and the actual elements of the operation of the following conclusions: ArrayList-his structure is 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-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 GET method for ArrayList, but it is much more complex and is mainly traversed by next or previous methods.   Vector-relatively simple and ArrayList, mainly the implementation of the internal synchronized keyword, the implementation of thread-safe access but some performance degradation, while 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.

Second, set mainly has HashSet and TreeSet HashSet-the class is inherited from the set interface, relative to the list is to say that internal added elements can not be duplicated, of course, from the hash of the name is the hash algorithm to achieve the prevention of conflict to prevent duplication,   As a whole, from the HashMap implementation, the element method is also 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.

Third, the MAP mainly has HashMap and TreeMap HashMap-provides a relatively powerful implementation, such as loadfactor can control the growth of the element memory allocation, HASHMAP is also non-thread-safe. TreeMap-the sort that is relative to HashMap can be controlled by passing in an attribute that contains comparator.

Four, single-threaded mode performance test, test element 100~1000 average score: Add HashMap efficiency is highest, ArrayList is the lowest, other high efficiency also have stack, hashset and vector, The lower have LinkedList and TreeSet and treemap delete hashmap efficiency is highest, linkedlist lowest, other hashset, treemap and treeset efficiency is higher, lower has vector, ArrayList and Stack find HashMap are the most efficient, LinkedList lowest, hashxxx and treexxx are more efficient, while the list-based efficiency time is about 10 times times that of map or set.

Five, multi-threaded mode performance test, test element 100~1000, the number of threads 10 average score: Add HashSet efficiency is highest, linkedlist lowest, hashxxx and treexxx efficiency are relatively high, here ArrayList efficiency is low, the overall difference is small.   Removing hashset is the most efficient, linkedlist lowest, with overall performance similar to adding, but hashxxx or treexxx performance is 3 times times higher than the list series. Find still hashset performance is the best, linkedlist lowest, poor performance is ArrayList, others are very good performance.

Java Collection Class performance analysis for Android development

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.