Java tool Classes Learn to organize--collection

Source: Internet
Author: User

For a long time did not summarize some things, at the same time the collection part of the knowledge point of learning is relatively early, but never take time to study and study, today just have a moment to summarize the map commonly used traversal method:

Package Runningwhile;import Java.util.hashmap;import Java.util.iterator;import Java.util.map;public class hashmaptest {public static void main (string[] args) {map<string,string> TestMap = new Hashmap<string,string > ("Testmap.put", "4120000002"), Testmap.put ("name", "Lin Yu Ink"), Testmap.put ("Age", "" "), Testmap.put (" Class "," 04 "); Testmap.put ("Professional", "HVAC"); System.out.println ("-------------------The first Way---------------------");//The first method, the most primitive, the simplest, the lowest efficiency System.out.println ( Testmap.tostring ()); System.out.println (Testmap.keyset () +testmap.values (). toString ()); System.out.println ("-------------------The second Way---------------------"); for (String String:testmap.keySet ()) { System.out.println (string+ ":" +testmap.get (String));} System.out.println ("-------------------The Third Way---------------------");//output iterator<map.entry< via the Map iterator String, String>> iterator =testmap.entryset (). iterator (); while (Iterator.hasnext ()) {map.entry<string, string> infoentry = Iterator.next (); System.out.println (INFOENTRY.GETKEY () + ":" +infoentry.getvalue ());} System.out.println ("-------------------The Fourth Way---------------------");//Similar to the third method, but higher efficiency for (map.entry<string, String> Infoentry:testmap.entrySet ()) {System.out.println (Infoentry.getkey () + ":" +infoentry.getvalue ());}}}

  

The list also has its own iterator, with the following examples:

Iterator it = List.iterator ();

If you want to traverse the list, use Iterator.hasnext (), i.e.

while (It.hasnext ()) {

}

Java tool Classes Learn to organize--collection

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.