package net.nie.test;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
public class Hashmaptest {private static Map<integer, string> map=new hashmap<integer,string> (); /** 1.HashMap class mappings are not guaranteed in order; Some mappings can explicitly guarantee their order: TREEMAP class * 2. In traversing the map, you cannot modify and delete elements with Map.put (Key,newval), Map.Remove (key).
A concurrent modification exception can be raised by removing the current iteration element * from the collection pointed to by the iterator by removing (): * To remove the element from the access.
* */public static void main (string[] args) {map.put (1, "one");
Map.put (2, "two");
Map.put (3, "three");
Map.put (4, "four");
Map.put (5, "five");
Map.put (6, "six");
Map.put (7, "seven");
Map.put (8, "eight");
Map.put (5, "five");
Map.put (9, "nine");
Map.put ("ten");
Iterator<map.entry<integer, string>> it = Map.entryset (). iterator ();
while (It.hasnext ()) {Map.entry<integer, string> entry=it.next (); int Key=entry.getkey ();
if (key%2==1) {System.out.println ("delete this:" +key+ "=" +key); Map.put (Key, "odd"); Concurrentmodificationexception//map.remove (key); Concurrentmodificationexception It.remove ();
OK}//traverse the current map; This new for loop cannot modify the content of the map because it does not pass through iterators.
System.out.println ("-------NT final Map element traversal:");
For (Map.entry<integer, string> entry:map.entrySet ()) {int K=entry.getkey ();
String V=entry.getvalue ();
System.out.println (k + "=" +v); }
}
}