Introduction to hashmap in Android

Source: Internet
Author: User
In Android development, we often need to classify and operate data. For lightweight data storage, we may not need to use SQLite or XML with imperfect efficiency and class libraries, because sharedpreferences does not have the data enumeration method, if it is only a string or Int array, it can be separated by a tag, let's take a look at the introduction of arraylist sorted list set hashmap, an auxiliary class of basic data types provided by Android or Java. If you are familiar with STL or boost libraries of C ++, you can skip this article.

The collection and map interfaces are provided in Java. List and set inherit the collection interface, and use the vector, arraylist, and sorted List classes to implement the list interface, while hashset and treeset implement the set interface. Hashtable, hashmap, and treemap are directly used to implement the map interface.

Vector is an array-based list, and its performance cannot surpass that of array. vector is "sychronized", which is the only difference between vector and arraylist.

Arraylist: Like a vector, It is array-based, but the difference is that arraylist is not synchronized. Therefore, the performance is better than that of vector. Android123 prompts you to apply to sequential search

Struct list: Unlike the previous two types of list, it is not based on array and is used as the data structure of the linked list. Therefore, it is not restricted by Array Performance. You only need to modify the relevant information of nextnode when adding and deleting the nextnode list. Therefore, it is suitable for frequent insert and delete operations. This is the advantage of the sort list. Of course, the acquisition of element location is much inferior.

List:

1. All lists can only contain tables composed of a single object of different types, rather than key-value pairs. Example: [Tom, 1, C];

2. All lists can have the same elements. For example, the vector can have [Tom, koo, too, koo];

3. All lists can have null elements, such as [Tom, null, 1];

4. array-based list (vector, arraylist) is suitable for queries, while sorted list (linked list) is suitable for adding and deleting operations.

Although set and list all implement the collection interface, their implementation methods are quite different. List is basically based on array. However, set is implemented based on hashmap, which is the fundamental difference between set and list.

Hashset: the key in hashmap is used as the corresponding storage item of the set. The key of hashmap cannot be duplicated. Hashset can be used to quickly locate an element, but the hashcode () method 0 must be implemented for objects placed in hashset.

Treeset: stores the elements in the table in order, which requires that the objects in the table be sorted. Different from hashset, treeset is ordered. It is implemented through sortedmap.

Set Summary: 1. set is implemented based on map (hashmap); 2. the elements in the set cannot be repeated. If you use the add (Object OBJ) method to add an existing object, the existing object will be overwritten, it cannot contain two elements, E1 and E2 (e1.equals (E2 )).

Map is a container that associates key objects and value objects. MAP has two common implementations: hashtable, hashmap, and treemap.

Hashmap also uses the hash algorithm to quickly find a key,

Treemap stores keys in sequence, so it has some extension methods, such as firstkey () and lastkey.

The difference between hashmap and hashtable. Hashmap allows null keys or values. The efficiency may be higher than that of hashtable due to non-thread security. Hashtable does not allow null keys or values ).

We will introduce more practical Android development skills in the following articles.

In Android development, we often need to classify and operate data. For lightweight data storage, we may not need to use SQLite or XML with imperfect efficiency and class libraries, because sharedpreferences does not have the data enumeration method, if it is only a string or Int array, it can be separated by a tag, let's take a look at the introduction of arraylist sorted list set hashmap, an auxiliary class of basic data types provided by Android or Java. If you are familiar with STL or boost libraries of C ++, you can skip this article.

The collection and map interfaces are provided in Java. List and set inherit the collection interface, and use the vector, arraylist, and sorted List classes to implement the list interface, while hashset and treeset implement the set interface. Hashtable, hashmap, and treemap are directly used to implement the map interface.

Vector is an array-based list, and its performance cannot surpass that of array. vector is "sychronized", which is the only difference between vector and arraylist.

Arraylist: Like a vector, It is array-based, but the difference is that arraylist is not synchronized. Therefore, the performance is better than that of vector. Android123 prompts you to apply to sequential search

Struct list: Unlike the previous two types of list, it is not based on array and is used as the data structure of the linked list. Therefore, it is not restricted by Array Performance. You only need to modify the relevant information of nextnode when adding and deleting the nextnode list. Therefore, it is suitable for frequent insert and delete operations. This is the advantage of the sort list. Of course, the acquisition of element location is much inferior.

List:

1. All lists can only contain tables composed of a single object of different types, rather than key-value pairs. Example: [Tom, 1, C];

2. All lists can have the same elements. For example, the vector can have [Tom, koo, too, koo];

3. All lists can have null elements, such as [Tom, null, 1];

4. array-based list (vector, arraylist) is suitable for queries, while sorted list (linked list) is suitable for adding and deleting operations.

Although set and list all implement the collection interface, their implementation methods are quite different. List is basically based on array. However, set is implemented based on hashmap, which is the fundamental difference between set and list.

Hashset: the key in hashmap is used as the corresponding storage item of the set. The key of hashmap cannot be duplicated. Hashset can be used to quickly locate an element, but the hashcode () method 0 must be implemented for objects placed in hashset.

Treeset: stores the elements in the table in order, which requires that the objects in the table be sorted. Different from hashset, treeset is ordered. It is implemented through sortedmap.

Set Summary: 1. set is implemented based on map (hashmap); 2. the elements in the set cannot be repeated. If you use the add (Object OBJ) method to add an existing object, the existing object will be overwritten, it cannot contain two elements, E1 and E2 (e1.equals (E2 )).

Map is a container that associates key objects and value objects. MAP has two common implementations: hashtable, hashmap, and treemap.

Hashmap also uses the hash algorithm to quickly find a key,

Treemap stores keys in sequence, so it has some extension methods, such as firstkey () and lastkey.

The difference between hashmap and hashtable. Hashmap allows null keys or values. The efficiency may be higher than that of hashtable due to non-thread security. Hashtable does not allow null keys or values ).

We will introduce more practical Android development skills in the following articles.

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.