Static initialization of 1.map, and several methods of map traversal:
Packagecom.cy.test;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Map.Entry; Public classTest { Public Static voidMain (string[] args) {Map<string, integer> map =NewHashmap<string, integer>() {{put ("ZH", 1); Put ("En", 2); Put ("Zh_cn", 1); Put ("en_US", 2); } }; /*** Method One uses iterator traversal * This approach looks redundant but has its advantages. * First, this is the only way to traverse the map in the old version of Java. * Another advantage is that you can delete entries by calling Iterator.remove () over the duration, and the other two methods cannot, according to Javadoc's instructions, if * Try this method in For-each traversal, the result is unpredictable. * From a performance perspective, this method is similar to the performance of for-each traversal (i.e., method two). */Iterator<entry<string, integer>> it =Map.entryset (). iterator (); while(It.hasnext ()) {Entry<string, integer> entry =It.next (); System.out.println ("Key:" + entry.getkey () + "----" +Entry.getvalue ()); } /*** Method Two uses entries in the For-each loop to traverse * This is the most common and in most cases the most desirable way to traverse. Used when the key value is required. * If you are traversing an empty map object, the For-each loop will throw NullPointerException*/ for(Map.entry<string, integer>Entry:map.entrySet ()) {System.out.println ("Key =" + entry.getkey () + ", Value =" +Entry.getvalue ()); } /*** Method Three traverses the keys or values in the For-each loop. * This method is slightly better than entryset traversal (10% faster), and the code is cleaner. */ for(String key:map.keySet ()) {System.out.println ("Key =" +key); } for(Integer value:map.values ()) {System.out.println ("Value =" +value); } /*** Method Four by key to find value traversal (inefficient) * In fact, it is quite slow and inefficient. Because it is time-consuming to take a value from a key * try to avoid it. */ for(String key:map.keySet ()) {Integer value=Map.get (key); System.out.println ("key =" + key + ", Value =" +value); } }}
Static initialization of 2.List:
Public Static void Main (string[] args) { // mode 1 new arraylist<string>() { { Add ("string1"); Add ("string2"); Add ("String3"); } ; // Mode 2 New Arraylist<string> (Arrays.aslist ("Ryan", "Julie", "Bob"));
3.
Static initialization of the Java Foundation-map and the traversal of the map, etc.......