Map is a data storage method. In my personal opinion, it is an array in php or asp.net and can be directly used for Traversing arrays. However, there are many inherent methods for Traversing map in java, let's take a look at some instances.
The traversal Map method can be divided into two types:
One type is map-based Entry; map. entrySet ();
One is map-based key; map. keySet ()
Each category has two traversal methods:
A. Use the iterator;
B. Use the f
(mutex) {return M. containskey (key );}}Public Boolean containsvalue (object Value ){Synchronized (mutex) {return M. containsvalue (value );}}Public v get (Object key ){Synchronized (mutex) {return M. Get (key );}}
Public v put (K key, V value ){Synchronized (mutex) {return M. Put (Key, value );}}Public v remove (Object key ){Synchronized (mutex) {return M. Remove (key );}}Public void putall (Map Synchronized (mutex) {M. putall (MAP );}}Public void clear (){Synchronized (mutex) {M. Clear ();}}
Interfaces in Java* Object get (Object key) * object put (object key, Object value) * Set KeySet (): Returns the keys set set KeySet () * Set EntrySet (): Returns mappings Set set entryset () * ContainsKey () * Containsvalue () Map is a collection of key-value pairs. Each element in the map is a key-value pair, which is a key (key) and its corresponding object value (value). For the map contain
parameters are used when using the tree to store data for the time being.Then look at several member variables:Such as:Table: An array of stored data;EntrySet: Is the return value of EntrySet (), which is used as the cache, so that calling the EntrySet () method does not recreate a set object each time;The number of size:key-value data;Modcount: To prevent concu
in the Set collection11.values (): Returns a collection of all values12.entrySet (): Returns a set set of key-value pairs13.getOrDefault (): Returns the value if the key exists (even if the corresponding value is null), otherwise the given default value is returned14.putIfAbsent (): If the given key does not exist or if there is a corresponding value of NULL, reset it, or return its value15.remove (Key,value): If the given value is not equal to the v
in the map , using more examples such as : EntrySet () method, The return value of EntrySet () is a set collection of type Map.entry. Map.entry is an internal interface to the map declaration, which is generic and defined as entrypublic static void Main (string[] args) {mapThe results of the implementation are as follows:EntrySet = [Null=null, A=1, b=2, c=3]Key.getkey=null Key.getvalue () =nullKey.getkey=a
value corresponding to each key. KeySet (): The key can only be taken with get () after the iteration.The result is random, because the Hashmap.keyset () method is used when the primary key of the data row is obtained, and the result of the set returned by this method is that the data is emitted in a disorderly order.Typical usage is as follows:Map map = new HashMap ();Map.put ("Key1", "LISI1");Map.put ("Key2", "Lisi2");Map.put ("Key3", "Lisi3");Map.put ("Key4", "Lisi4");Gets the set set of all
integer value = null; Iterator iter = Map.entryset (). Iterator (); while (Iter.hasnext ()) {Map.entry Entry = (map.entry) iter.next (); Get key key = (String) entry.getkey (); Gets the value value = (Integer) entry.getvalue (); } NULL ; Integer valuenull; = Map.keyset (). iterator (); while (Iter.hasnext ()) { // get key key = (String) Iter.next (); // based on key, get value Value= (Integer) map.get (key); } C Trav
the interface mapCovered:Keyset in the class abstractmapReturn:
The set view of the keys contained in this map is described in relation to set use. 】
Here is an example to illustrate the resolution:
[Java] View plain copy print? publicclasshashmaptest{ publicstaticvoid main (String[]args) { hashmapBy running the test multiple times, the EntrySet traversal time is much shorter than the keyset traversal time, and the performance of the
specific node. This line of code is used to get the most recent node that is most infrequently used.
The first thing to be explained is that Android Linkedhashmap and Java Linkedhashmap, as well, use headers to save two-way lists. When you put and get, you update the corresponding node, save the Header.after point to the longest unused node, and Header.before to point to the node you just used. So map.entry toevict = (map.entry) this.map.entrySet (). iterator (). Next (); This line is definite
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
Method One: Use EntrySet ()Iterator it = Emails
For more information about four ways to traverse a map in Java, see below.Way one This is the most common and in most cases also the most desirable way to traverse. Used when the key value is required.1 New Hashmap2 for (Map.entry3 System. out. println ( "" "" "+4 }Method Two traverses the keys or values in the For-each loop.If you only need the keys or values in the map, you can implement the traversal through keyset or values instead of using EntrySet
of the first occurrence of the specified element if the element does not exist return-1;So, by 1, you can tell if an element exists.int lastIndexOf (Object o): Reverse index Specifies the position of the element.List sublist (start,end): Gets the child list.4, modify:Object Set (index,element): Modifies elements of the specified index bit.5, get all the elements:Listiterator Listiterator (): A list collection-specific iterator.The list collection supports adding, deleting, changing, and checkin
: Extract the key out of the set, then use the iterator to traverse the keyset, and use Map.get (key) to get the value corresponding to key. SetStu. KeySet (); Iterator it=Keyset.iterator (); while(It.hasnext ()) {String Next=(String) it.next (); System.out.println (Next+","+Stu.get (next)); }}}//the comparator that defines the key, the comparison algorithm according to the first parameter O1, less than, equal to or greater than O2 respectively return a negative integer, 0 or a positive intege
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
over the elements of the map being passed, in addition to the algorithm that the iteration put () is running to add each key-value pair to the map. It should be noted, however, that Putall () can correctly adjust the size of the map before joining 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 ma
New features after jdk1.5:
1. Generic type:
Commonly used on collections. You can avoid type conversion exceptions
How generics are used on collections.
Common collections: List set Map
Generic Syntax: Collections
Traverse List:1) General for Loop 2) enhanced for 3) iterator iterator
Traversal set:1) Enhanced for 2) iterator iterator
Unordered, cannot have duplicate elements
Traversal map:1) keyset () () () 2) EntrySet ()
Keyset () Way:
First gets t
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.