The static initialization of the Java Foundation-map and the traversal of the map, etc......

Source: Internet
Author: User

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.......

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.