Sparse array sparsearray<e> for Android performance optimization

Source: Internet
Author: User

Sparsearray<e> is the compression of an array, which is represented by a compression method.

When we define an array, there are times in the array where most of the content is not used (or 0), resulting in a waste of memory controls. To save memory controls without affecting the data in the array, we used a compression method to represent the array.

The yellow in the figure shows the elements we used, and the other elements here are wasted. Take a look at the methods represented by sparse arrays

The first part of the sparse array represents the number of rows, columns, and elements used by the array. The second part records the position and content of the elements used in the original array.

Use of Sparsearray:

Increase

 Public void put (int  key, E value) {}publicvoid append (int key, E value) { }

By deleting

 Public void Delete (int  key) {}publicvoid Remove (int//  Directly called delete (int key)publicvoid removeAt (int  index) {}public  void Clear () {}

Change

/** the processing in the source code is: if the put data already exists, is modified, otherwise it is added * */  Public void put (int  key, E value)publicvoid setvalueat (int index, E Value

Check

 PublicE Get (intkey) PublicE Get (intkey, E valueifkeynotfound)/**to view the keys in the first few places:*/ Public intKeyat (intindex)/**view values for the first few positions*/ PublicE Valueat (intindex)/**A value less than 0 is returned instead of 1 when the view key is located in a location that is not found. The negative value returned is the location where it was not found. */ Public intIndexofkey (intkey)/**see where the value is, no, return-1*/ Public intIndexofvalue (E value)

Sparsebooleanarray <---> Hashmap<integer, boolean>

Sparseintarray <---> Hashmap<integer, integer>

Sparsearray is specifically written for hashmap<interger,object> such as Android, with the aim of improving efficiency, with the core of binary lookup function (binarysearch)

New New Sparsearray<e> ();

Sparse array sparsearray<e> for Android performance optimization

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.