Performance Analysis of Java Collection classes in Android Development

Source: Internet
Author: User

For Android Developers, it is necessary to have a deep understanding of Java's collection classes mainly derived from the collection and map interfaces. Currently, the collection of list, set, and map classes is mainly provided, today, android123 performs a simple analysis on the performance of their sub-classes under standard conditions and multithreading.

The collection interface mainly has two subclasses: List and set. The difference is that the objects saved by list can be repeated, but set cannot be repeated, while MAP generally has a correspondence relationship such as key-value, for example, we commonly use hashmap.

1. List mainly includes arraylist, vector, and stack.

Concerning the performance of these sub-classes, the android Development Network analyzes the execution efficiency of elements in terms of insertion, deletion, and movement. By analyzing Sun's Java source code and actual element operations, the following conclusions are drawn:

Arraylist-its construction is mainly implemented from abstractlist, mainly to determine the capacity of the initial element. The biggest feature of arraylist is that it provides the ADD and get operations. Of course, it can be traversed through the iterator, you can use the contains method to determine the existence of elements.

Sort list-as a two-way linked list structure, the insert and delete efficiency of elements is relatively high. You only need to adjust the node pointing to it, however, for random search, the performance mainly depends on the length and luck of the linked list. The arraylist get method is also provided, but it is much more complicated. It is obtained through the next or previous method.

Vector-is relatively simple and similar to arraylist. It mainly implements the synchronized keyword internally to implement thread-safe access, but its performance is somewhat lower. Meanwhile, the element expansion algorithm is slightly different from that of arraylist, determined by the capacity increment coefficient.

Stack-as a stack operation, this operation inherits from the vector and provides the push, pop, and peek methods. Peek does not pop up to obtain the last element object based on the data size.

Ii. Set mainly includes hashset and treeset

Hashset-This class is inherited from the set interface. Compared with the list, it means that the elements added internally cannot be repeated, of course, from the perspective of name hash, the hash algorithm is used to prevent conflicts and prevent duplication. The hashmap is implemented as a whole, and the element storage method is similar to the key-value method, traversal through the iterator, but hashset is NOT thread-safe.

Treeset-compared with hashset, this mainly provides sorting support. treeset is implemented from the treemap class and is not thread-safe.

We can see that the two classes of set are related to map. The following describes the usage of map.

3. MAP mainly includes hashmap and treemap

Hashmap-provides powerful functions. For example, loadfactor can control the memory allocation when elements increase, and hashmap is non-thread-safe.

Treemap-compared with hashmap, its sorting can be controlled by passing in attributes containing comparator.

4. Performance Testing in single-thread mode. Test elements: 100 ~ Average score in 1000:

The efficiency of adding a hashmap is the highest, the arraylist is the lowest, and the other high efficiency include stack, hashset, and vector. The lower efficiency is the sorted list and treeset and treemap.

The efficiency of deleting a hashmap is the highest, and the list of partitions is the lowest. Other hashsets, treemap, and treeset have higher efficiency, and the lower ones include vector, arraylist, and stack.

The efficiency of searching for hashmap is the highest, the efficiency of listing is the lowest, the efficiency of hashxxx and treexxx is relatively high, and the efficiency of list-based classes is about 10 times that of map or set.

5. Performance Testing in multi-threaded mode. The testing elements are 100 ~ 1000, the average score of 10 threads:

The efficiency of adding a hashset is the highest, the efficiency of a hashset is the lowest, and the efficiency of hashxxx and treexxx is relatively high. Here, arraylist is less efficient and the overall difference is not big.

The efficiency of deleting a hashset is the highest. The overall performance is similar to that of adding a hashset, but the performance of hashxxx or treexxx is three times higher than that of the List series.

Search is still the best in hashset performance, the lowest in sorted list, and the arraylist with poor performance, all others are doing well.

In summary, the preceding single-thread and multi-thread tests are described as follows:

Android123 recommends that you use a two-way linked list such as javaslist as much as possible during the development process. The overall efficiency is not very good, and the performance of hashmap is better integrated. For arraylist, because it contains index tables, however, the random search performance is good, and the performance is relatively moderate. For the collection class-specific concurrent package Concurrent Version performance, refer to the Java concurrent package collection class performance analysis developed by Android.

Last time, we mainly analyzed the performance of Java Collection classes in single-thread and multi-thread Android development and analyzed the performance of Java Collection classes. Today, android123 makes a simple Evaluation and Analysis on the Performance of collection classes in the concurrent library.

And package Java. util. the concurrent library is added in JDK 1.5, which can be well supported by Android. It performs better than thread control and synchronization management, compare the simplest performance of concurrenthashmap, copyonwritearraylist, copyonwritearrayset, and arrayblockingqueue with hashmap, listarray, set, and queue in multiple threads.

1. concurrenthashmap is a hashmap of the thread security version. Its construction also has initialcapacity and loadfactor coefficient attributes, but it also has a concurrentcylevel. It is empty by default, these three values are 16, 0.75, and 16. In concurrenthashmap, synchronization is not implemented through synchronized. If the input object value is null, a null pointer exception is thrown to prevent conflicts still implemented using the hashcode method of the Java object class.

1) 100 elements in a single thread

The efficiency of adding concurrenthashmap is much lower than that of hashmap, but the general application is almost invisible.

2) 100 elements in multiple threads

In 10 threads, the performance of both of them is similar. However, at this time, the performance of concurrenthashmap has exceeded that of hashmap, regardless of the number of elements. As the number of threads increases, the efficiency increases significantly, however, for apps such as Android phones, there may not be many applications with more than 10 threads. However, for Java VMS, concurrent packaging has obvious performance advantages in multithreading and is strongly recommended.

2. copyonwritearraylist is a thread-safe arraylist, but copyonwritearraylist is unlocked when reading elements. From this point, we can see that it is not a simple and practical synchronized keyword to implement locking, for concurrent packages, reentrantlock is used internally to implement secure thread access. For the addition of elements, the new memory allocation of the copyonwritearraylist is to create a buffer that is 1 larger than the original one, copy the old data to the new buffer, and add the new elements to the end of the array to add the elements. The deletion is also implemented through reentrantlock, which is more complex than the deletion operation of arraylist. First, create a buffer with 1 fewer than the current array element, and then copy the existing array to the buffer, but the copy process is 1 less than the existing array, which is the opposite of adding elements. Android Development Network prompts you to add and delete copyonwritearraylist, which is not as simple as system. arraycopy as arraylist. Therefore, the performance is definitely different from that of arraylist.

1) 100 elements in a single thread

Copyonwritearraylist is nearly twice slower than that of arraylist when an element is added or deleted, but the search performance does not change much. However, as the element increases, copyonwritearraylist is less efficient than that of arraylist in a single thread, when the number of elements reaches 100.

2) 100 elements in multiple threads

Here, android123 mainly tests the possible 10 threads on the mobile phone. In this way, the efficiency of adding and deleting copyonwritearraylist in the environment is far less than that of arraylist, but the search efficiency is faster than that of arraylist, when the number of elements increases to 10000, the efficiency of adding and deleting elements is the same, but the efficiency of searching copyonwritearraylist is much higher than that of arraylist.

3. copyonwritearrayset is based on copyonwritearraylist. Because the set mechanism cannot be repeated, the add method of copyonwritearrayset calls the addifabsent method in the copyonwritearraylist when processing element addition. If so, the system returns the result, however, the efficiency is not ideal. Because additional operations are performed during addition, It is slower than copyonwritearraylist to add a record. The others are the same as copyonwritearraylist.

4. arrayblockingqueue is a queue from the perspective of its name, but it is a FIFO thread-safe queue with a stack-like structure.

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.