Map stores data in the form of key/value pairs. Similar to arrays, indexes in arrays are also objects.
Map Interface
Map --- implement Map
Map. Entry -- Internal class of map, which describes the key/value pairs in map.
Sortedmap --- extended map, enabling keys to be sorted in ascending order
Generally, map subclass is selected instead of map class.
The following uses hashmap as an example.
Public static void main (string ARGs [])
{
Hashmap = new hashmap ();
Hashmap. Put ("item0", "value0 ");
Hashmap. Put ("Item1", "value1 ");
Hashmap. Put ("item2", "value2 ");
Hashmap. Put ("item3", "value3 ");
Set set = hashmap. entryset ();
Iterator = set. iterator ();
While (iterator. hasnext ()
{
Map. Entry mapentry = (Map. Entry) iterator. Next ();
System. Out. println (mapentry. getkey () + "/" + mapentry. getvalue ());
}
}
Note: The map buttons must be unique here. For example, no two buttons can be null.
If you have used it, you will know its usefulness.
For example:
Map <string, order> map = new hashmap <string, order> ();
Map. Put ("order", (Order) OBJ );
MATERIALS:
The collection container contains the set and list interfaces, and the set contains hashset, which contains the sorted list and arraylist. The separate map interfaces only contain hashmap.
JavaThe Collection class in. util containsJavaSome of the most common classes in. The most common collection classes are list and map. The specific implementation of list includes arraylist and vector, which are variable-size lists and are suitable for building, storing, and operating the list of elements of any type of object. List is applicable to accessing elements by Numerical index, where the data is ordered and can be repeated. SetData is unordered and cannot be duplicated.
Map provides a more general method for storing elements. The Map Collection class is used to store element pairs (called "keys" and "values"). Each key is mapped to a value. In terms of concept, You can regard list as a map with a numeric key. In fact, apart from the definition of both list and mapJavaThere is no direct connection between. util and other Chinese and foreign companies. This article focuses on the coreJavaThe map included in the release kit also describes how to use or implement a map that is more suitable for your application.ProgramA dedicated map of specific data.
Understanding map interfaces and Methods
JavaThere are many predefined map classes in the core class. Before introducing specific implementations, let's first introduce the map interface itself to understand the commonalities of All implementations. The map interface defines four types of methods. Each map contains these methods. Next, we use two common methods (Table 1.
Table 1: overwrite method. We overwrite the two methods of the object to correctly compare the equivalence of the map object.
Equals (Object O) |
Compare the equivalence between the specified object and the map |
Hashcode () |
Returns the hash code of this map. |
Map Construction
Map defines several transformation methods for inserting and deleting elements (Table 2).
Table 2: map update method: You can change the map content.
Clear () |
Delete all mappings from map |
Remove (Object key) |
Delete keys and associated values from map |
Put (Object key, object value) |
Associate a specified value with a specified key |
Clear () |
Delete all mappings from map |
Putall (map t) |
Copy all mappings in the specified map to this map |
Although you may notice that, even if you ignore the overhead of building a map that needs to be passed to putall (), using putall () is generally not more efficient than using a large number of put () calls, however, putall () is not uncommon. This is because putall () adds each key-value pair to the map in addition to the put () iteration.AlgorithmIn addition, the elements of the map passed by iteration are also required. However, it should be noted that putall () can adjust the map size correctly before adding all elements. Therefore, if you do not adjust the map size yourself (we will briefly introduce this ), putall () may be more effective than expected.
View map
The element in iterative map does not have a straightforward method. If you want to query a map to learn which elements meet a specific query, or if you want to iterate all of its elements (whatever the reason), you first need to obtain the "View" of the map ". There are three possible views (seeTable 3)
- All key-value pairs-see entryset ()
- All keys-see keyset ()
- All values-see values ()
The first two views return the set object, and the third view returns the collection object. In both cases, the problem is not solved because you cannot directly iterate the collection object or set object. To iterate, you must obtain an iterator object. Therefore, to iterate the map elements, we must perform tedious coding.
Iterator keyvaluepairs = AMAP. entryset (). iterator (); iterator keys = AMAP. keyset (). iterator (); iterator values = AMAP. Values (). iterator ();
Note that these objects (set, collection, and iterator) are actually basic map views rather than copies of all elements. This results in high efficiency. On the other hand, the toarray () method of the collection or set object creates an array object that contains all the elements of the map. Therefore, it is not efficient except for the fact that the elements in the array need to be used.
I ran a small test (Test1In this test, hashmap is used, and the overhead of iterative map elements is compared using the following two methods:
int mapsize = AMAP. size (); iterator keyvaluepairs1 = AMAP. entryset (). iterator (); for (INT I = 0; I
object [] keyvaluepairs2 = AMAP. entryset (). toarray (); for (INT I = 0; I
Profilers in Oracle jdeveloper Oracle jdeveloper contains an embedded monitor that measures memory and execution time, allowing you to quickly identifyCode. I used jdeveloper's execution monitor to monitor hashmap'sContainskey() And containsvalue () methods, and soon foundContainskey() The speed of the method is much slower than that of the containsvalue () method (in fact, it is several orders of magnitude slower !). (SeeFigure 1AndFigure 2AndTest2Class ). |
Object value = entry. getvalue ();...}
This test uses two measurement methods: one is to measure the time of iteration elements, and the other is to use toarray to call other overhead for creating arrays. The first method (ignore the time required to create an array) indicates that the speed of using the array iteration elements that have been created from the toarray call is about 30%-60% faster than that using iterator. However, if the overhead of using the toarray method to create an array is included, the use of iterator is actually faster than 10%-20%. Therefore, if, for some reason, you want to create an array of elements in the collection instead of iterating these elements, you should use the array iteration element. If you do not need an intermediate array, do not create it. Instead, use the iterator iteration element.
Table 3: Map Method of the returned view: using these methods to return objects, You can traverse the map elements and delete the elements in the map.
Entryset () |
Returns the set view of the ing contained in the map. Each element in the set is a map. Entry object. You can use the getkey () and getvalue () methods (and the setvalue () method) to access the key elements and value elements of the latter. |
Keyset () |
Returns the set view of keys contained in the map. Deleting the elements in the set will also delete the corresponding ing (key and value) in the map) |
Values () |
Returns the collection view of values contained in the map. Deleting the elements in the collection will also delete the corresponding ing (key and value) in the map) |
Access Element
Table 4 lists the MAP access methods. MAP is usually suitable for access by key rather than by value. The map definition does not stipulate that this is true, but you can expect it to be true. For example, you can expectContainskeyThe () method is as fast as the get () method. On the other hand, the containsvalue () method may need to scan values in the map, so it may be slower.
Table 4: Map access and Test Methods: these methods retrieve information about the map content without changing the map content.
Get (Object key) |
Returns the value associated with the specified key. |
Containskey(Object key) |
Returns true if map contains the ing of the specified key. |
Containsvalue (object value) |
Returns true if the map maps one or more keys to a specified value. |
Isempty () |
Returns true if map does not contain key-value ing. |
Size () |
Returns the number of key-value mappings in the map. |
for containskey () and the time required for containsvalue () to traverse all elements in the hashmap shows that the time required for containsvalue () is much longer. It actually takes several orders of magnitude! (See Figure 1 and Figure 2 , and Test2 in the attached file ). Therefore, if containsvalue () is a performance issue in an application, it will soon become apparent and can be easily identified by monitoring your application. In this case, I believe you can come up with an effective replacement method to implement the equivalent functionality provided by containsvalue. However, if you cannot find a solution, a feasible solution is to create a map and use all the values of the first map as keys. In this way, containsvalue () on the first map will become the more effective containskey ().