Map stores data in the form of key/numeric pairs, and arrays are similar to indexes that exist in arrays, which are themselves objects.
The interface of Map
map---Implementation Map
The inner class of Map.entry--map, which describes the key/value pairs in the map.
SortedMap---Extend the map to keep the keys in ascending order
In terms of how to use it, it is usually the subclass of the map that is chosen, rather than using the map class directly.
The following is an example of HashMap.
public static void Main (String args[])
{
HashMap HashMap = new HashMap ();
Hashmap.put ("Item0", "Value0");
Hashmap.put ("Item1", "Value1");
Hashmap.put ("Item2", "Value2");
Hashmap.put ("Item3", "Value3");
Set set = Hashmap.entryset ();
Iterator Iterator = Set.iterator ();
while (Iterator.hasnext ()
{
Map.entry mapentry = (map.entry) iterator.next ();
System.out.println (Mapentry.getkey () + "/" + Mapentry.getvalue ());
}
}
Note that the button for the map must be unique, for example, two keys cannot be null.
If you use it, you will know its usefulness.
Another example:
map<string, order> map = new hashmap<string, order> ();
Map.put ("Order", (order) obj);
Information:
The collection container includes the Set and list interfaces, and the set includes LinkedList and ArrayList in the Hashset,list, and only hashmap in a separate map interface.
The collection classes in Java. Util include some of the most frequently used classes in Java . The most frequently used collection classes are List and Map. The detailed implementation of the list includes ArrayList and vectors, which are variable-sized lists that are appropriate for building, storing, and manipulating elements of whatever type of object. The List is useful for the case of an element that is indexed by a numeric index, in which the data is sequential and can be repeated. The data in set is not sequential and cannot be repeated.
Map provides a more general method of storing elements. The Map collection class is used to store element pairs (called "Keys" and "values"), where each key is mapped to a value. Conceptually, you can treat a List as a Map with numeric keys. In fact, in addition to the List and Map are in the definition of Java. Util there is no direct link between the two countries. This article will focus on the map included with the core Java distribution Suite, and at the same time will show you how to use or implement a dedicated map that is more appropriate for your application-specific data.
understanding MAP Interfaces and methods
There are very many pre-defined MAP classes in the Java core class. Before we introduce the detailed implementation, let's introduce the Map interface itself to understand the common denominator of all implementations. The map interface defines four types of methods, each of which is included in each map. Below, we start by introducing these methods from two common methods ( table 1).
Table 1: overridden methods. We override these two methods of this object to correctly compare the equivalence of the Map object.
Equals (Object o) |
Compare the equivalence of the specified object to this Map |
Hashcode () |
Returns the hash code for this MAP |
Map Build
Map defines several transformation methods for inserting and deleting elements ( table 2).
Table 2:map Update method: Ability to change Map content.
Clear () |
Remove all mappings from map |
Remove (Object key) |
Remove keys and associated values from a Map |
Put (object key, Object value) |
Associate a specified value with a specified key |
Clear () |
Remove all mappings from map |
Putall (Map t) |
Copies 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 not usually more efficient than using a large number of put () calls, but the existence of Putall () is not uncommon. This is because Putall () has to iterate over the elements of the map being passed in addition to the algorithm that the iteration put () runs to add each key-value pair to the map. It should be noted, however, that Putall () correctly adjusts the size of the map before it joins all elements, so if you do not adjust the size of the map yourself (which we will introduce), Putall () may be more efficient than expected.
View Map
The elements in the iteration Map do not have a straightforward method. Suppose you want to query a map to see which elements satisfy a particular query, or if you want to iterate over all of its elements (whatever the reason), you first need to get the view of the map. There are three possible views (see Table 3)
- all key-value pairs-see EntrySet ()
- all keys-see KeySet ()
- all values-see values ()
the first two views return a Set object, and the third view returns the Collection object. In both cases, the problem does not end here because you cannot iterate directly over the Collection object or the Set object. To iterate, you must obtain a Iterator object. Therefore, in order to iterate over the elements of the MAP, it is necessary to do a cumbersome coding
Iterator keyvaluepairs = Amap.entryset (). Iterator (); Iterator keys = Amap.keyset (). Iterator (); Iterator values = Amap.values (). iterator ();
It is worth noting that these objects (Set, Collection, and Iterator) are actually views of the underlying Map, rather than a copy of all the elements. This makes them highly efficient to use. On the other hand, the ToArray () method of the Collection or Set object creates an array object that includes all the elements of the MAP, so it is not efficient except in cases where the elements in the array are really needed.
I performed a small test ( Test1in the accompanying file), which used the HASHMAP and compared the cost of the iteration Map element using the following two methods:
int mapsize = Amap.size ();Iterator keyValuePairs1 = Amap.entryset (). Iterator ();for (int i = 0; i < mapsize; i++) {Map.entry Entry = (map.entry) keyvaluepairs1.next (); Object key = Entry.getkey (); Object value = Entry.getvalue (); ...}object[] keyValuePairs2 = Amap.entryset (). ToArray ();for (int i = 0; i < REM; i++) {{Map.entry Entry = (map.entry) keyvaluepairs2[i]; Object key = Entry.getkey ();
profilers in Oracle JDeveloper Oracle JDeveloper includes an embedded monitor that measure memory and uptime, allowing you to identify bottlenecks in your code at high speed. I used Jdeveloper's run-time monitor to monitor the HASHMAP's containskey() and Containsvalue () methods, not often discovering the containskey() method faster than Cont The Ainsvalue () method is much slower (actually a few orders of magnitude slower!). )。 (see Figure 1 and Figure 2, and the Test2 class in the accompanying file). |
Object value = Entry.getvalue (); ...}
This test uses two measurement methods: One is the time to measure the iteration element, and the other overhead of using the ToArray call to create the array. The first method (ignoring the time it takes to create an array) indicates that an array iteration element that has been created from the ToArray call is about 30%-60% faster than the Iterator. But assuming that the cost of creating an array using the ToArray method is included, the use of Iterator is actually 10%-20% faster. Therefore, suppose that for some reason you want to create an array of collection elements rather than iterate over them, you should use that array to iterate over the elements. But suppose you do not need this intermediate array, do not create it, but instead use the Iterator iteration element.
Table 3: Return to the Map method of the view: Using the objects returned by these methods, you can traverse the elements of the map and delete the elements in the map.
EntrySet () |
Returns the Set view of the map included in the map. Each element in Set is a Map.entry object that can use the GetKey () and GetValue () methods (another SetValue () method) to access the key elements and value elements of the latter |
KeySet () |
Returns a Set view of the keys included in the MAP. Deleting the elements in the set also deletes the corresponding mappings (keys and values) in the map |
VALUES () |
Returns a Collection view of the values included in the map. Deleting the elements in the Collection also deletes the corresponding mappings (keys and values) in the map |
Interview Elements
the Map access method is listed in table 4. Maps are often used for keystrokes (rather than by value). The MAP definition does not stipulate that this is certainly true, but usually you can expect it to be true. For example, you can expect the ContainsKey() method to be as fast as the Get () method. On the other hand, the Containsvalue () method is likely to need to scan the values in the Map, so it may be slower.
Table 4:map Interview and test methods: These methods retrieve information about the map content but do not change the map content.
Get (Object key) |
Returns the value associated with the specified key |
ContainsKey (Object key) |
Returns TRUE if the map includes a mapping of the specified key |
Containsvalue (Object value) |
If this map maps one or more keys to a specified value, it returns true |
IsEmpty () |
If map does not include a key-value mapping, it returns true |
Size () |
Returns the number of key-value mappings in a map |
A test of the time required to traverse all the elements in the HASHMAP using containskey() and Containsvalue () shows that the time required for Containsvalue () is much longer. It actually takes a few orders of magnitude! (see Figure 1 and Figure 2, along with the Test2in the accompanying file). So, assuming that containsvalue () is a performance problem in your application, it will appear very quickly 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 (). But assuming there is no way out, a workable solution is to create a map and use the entire value of the first map as the key. Thus, the Containsvalue () on the first map will be the more efficient containskey() on the second map.
Use of Map in Java