, and if we can get all the keys in the collection, we can use the Get () method to get the value in the collection.2. Use the keyset method to remove elements from the map collectionIdea: The keyset method can get all the keys in the collection and put those keys into the set collection. We can then use the iterator to read the keys in the set and get () the value corresponding to each key.The code is implemented as follows:3. Use the EntrySet method
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 MapThe elements in the iteration Map do not have a straightforward method. Suppose you want to query a map to see
/*
The program begins to create a hash map and then adds the mapping of the name to the balance table. Next, the mapped content is displayed by using the collection "view" obtained by invoking the function EntrySet (). The keywords and values are displayed by calling the Getkey () and GetValue () methods defined by Map.entry. Notice how the deposit was made into Evan's account. The put () method automatically replaces the original value associated wit
in JDK1.4
Map map = new HashMap ();
Iterator it = Map.entryset (). iterator ();
while (It.hasnext ()) {
Map.entry Entry = (map.entry) it.next ();
Object key = Entry.getkey ();
Object value = Entry.getvalue ();
}
JDK1.5, the application of new characteristics For-each cycle
Map m = new HashMap ();
For (Object O:map.keyset ()) {
map.get (o);
}
Each element in the returned set is a map.entry type.
Private hashtable
In addition, we can change the HashM
} PublicSetEntrySet () {Setes; return(es = entryset) = =NULL? (EntrySet =NewLinkedentryset ()): es;//returns a set containing all key-value pairs}
Checking the Putval method of HashMap, we can see that afternodeaccess is called when the same key value is found and the value is modified, and the node order is changed for Access-order
if NULL // The same key is found, the value is modified
Map is a key-value pair and is a common data structure. The map interface defines the basic behavior of the map, including the most central get and put operations, and the method of defining this interface is shown in the following:There are different map implementations in the JDK that apply to different scenarios, such as thread-safe hashtable and non-thread-safe hashmap.As a subclass of the map interface in the JDK UML class diagram, there is a special case dictionary is not recommended:The m
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, to iterate over th
data is to use the EntrySet () method to return a set set and then use an iterator to take out or to enhance a For loop (a collection that implements the Iterable interface can be looped using foreach). The mapping relationship returned by EntrySet is a map.entry type because entry is actually defined in the map, why do you do it? Because only the map collection has a key-value pair that has a mapping of k
Two ways to HashMap traversal in JavaOriginal address: http://www.javaweb.cc/language/java/032291.shtmlThe first type:Map map = new HashMap ();Iterator iter = Map.entryset (). Iterator ();while (Iter.hasnext ()) {Map.entry Entry = (map.entry) iter.next ();Object key = Entry.getkey ();Object val = Entry.getvalue ();}The efficiency is high, must use this kind of way later!The second type:Map map = new HashMap ();Iterator iter = Map.keyset (). Iterator ();while (Iter.hasnext ()) {Object key = Iter.
parameter you pass is null, a null pointer will be thrown. At this point, the most important part has been completed. The following describes some common methods:
Public synchronized v remove (Object key) {// Delete the entry tab [] = table; int hash = hash (key ); // calculate the hash value int Index = (hash 0x7fffffff) % tab. length; // calculate the index for (Entry
Public synchronized void clear () {// clear the bucket array entry tab [] = table; modcount ++; For (INT Index = tab. lengt
Interface Map
I. Map Function
1. Add
Put (K key, V value)
Putall (Map
2. Delete
Clear ()
Remove (Object key); returns the corresponding value
3. Judgment
Containskey (Object key)
Containsvalue (object value)
Isempty ()
4. Get
Get (Object key): NULL is not returned
Size ()
Values ();
-------------------------------------
Key points: entryset () and keyset ();
Two methods to retrieve a map set
1. keyset: Set
Because set
/*** Change the map structure to list and change the list to map structure
* An object is an arbitrary object.* @ Author liutianfang* @ Version 2011-8-16 01:16:11 pm*/Public class mapobjectutil {/*** Change the data structure of the object type to the data structure of the map type.* @ Param object* @ Return Map*/Public static Map Map Class A = O. getclass ();Field [] FS = A. getdeclaredfields ();For (INT I = 0; I Map. Put (FS [I]. getname (), reflectutil. getfieldvalue (O, FS [I]. getname (), o
to obtain the key, you can do this: Call the hashmap method entryset to obtain the set getKey()Method to obtain the key of the hanshmap. (Map. Entry String> entry: map. entryset ())
7. put the built-in resources of the application in the assets folder and create a directory in it. Note that the resources and directories cannot contain Chinese characters. The method for obtaining resources is as follows: Us
It is found that hashmap uses the following format to traverse the entire hashmap hashtable in a loop.
Each element in the returned set is of the map. Entry type.Java code private hashtable
// Method 1: entryset ()
// Iterator it = emails. entryset (). iterator ();
// While (it. hasnext ()){
// Map. Entry M = (Map. Entry) it. Next ();
// Logger.info ("email-" + M. getkey () + ":" + M. getvalue ());
changes", which also reflects that when we write the program, it must be "data"-oriented.Although not in the previous article Map Collection , it provides three "set View" (Collection views), corresponding to the following three method one by one:
Set, providing a set view of key
Collection, providing a collection view of value
Set, which provides a set view of the key-value sequence pairs, where the internal class is used Map.Entry to represent the order
Abstractmap Abstr
/*
The program starts to create a hash ing and then adds the name ing to the balance table. Next, the ing content is displayed by using the set "View" obtained by calling the function entryset. Keywords and values are displayed by calling the getkey () and getvalue () methods defined by map. Entry. Note how deposits are made into evan's account. The put () method automatically replaces the original value associated with the specified keyword with the
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.