Java Instance-Collection traversal

Source: Internet
Author: User

Traversal of the list and set type collection
1 Importjava.util.ArrayList;2 ImportJava.util.HashSet;3 ImportJava.util.Iterator;4 Importjava.util.List;5 ImportJava.util.Set;6  7  Public classMain {8  9     Public Static voidMain (string[] args) {Ten       //traversal of the list collection One listtest (); A       //traversal of Set set - settest (); -    } the   -    Private Static voidsettest () { -Set<string> set =NewHashset<string>(); -Set.add ("JAVA"); +Set.add ("C"); -Set.add ("C + +"); +       //Duplicate data addition failed ASet.add ("JAVA"); atSet.add ("JAVASCRIPT"); -   -       //To traverse a set collection using iterator -Iterator<string> it =set.iterator (); -        while(It.hasnext ()) { -String value =It.next (); in System.out.println (value); -       } to        +       //traversing a set collection using an enhanced for loop -        for(String s:set) { the System.out.println (s); *       } $    }Panax Notoginseng   -    //traversing the list collection the    Private Static voidlisttest () { +list<string> list =NewArraylist<string>(); AList.add ("W"); theList.add ("M"); +List.add ("L"); -List.add ("D"); $List.add ("www.pekst.com"); $   -       //using iterator traversal -Iterator<string> it =list.iterator (); the        while(It.hasnext ()) { -String value =It.next ();Wuyi System.out.println (value); the       } -   Wu       //traversing with a traditional for loop -        for(inti = 0, size = list.size (); i < size; i++) { AboutString value =List.get (i); $ System.out.println (value); -       } -   -       //traverse with enhanced for loop A        for(String value:list) { + System.out.println (value); the       } -    } $}
About traversal of a collection of map types

In the following example we use HashMap's KeySet () and EntrySet () methods to iterate through the collection:

1 ImportJava.util.Map;2 ImportJava.util.HashMap;3 ImportJava.util.HashSet;4 ImportJava.util.Iterator;5 Importjava.util.List;6 ImportJava.util.Set;7 ImportJava.util.Map.Entry;8 9 //Enhanced for LoopTen  Public classMain { One  A     Public Static voidMain (string[] args) { -       //creates a HashMap object and joins some key-value pairs.  -Map<string, string> maps =NewHashmap<string, string>(); theMaps.put ("1", "PHP"); -Maps.put ("2", "Java"); -Maps.put ("3", "C"); -Maps.put ("4", "C + +"); +Maps.put ("5", "HTML"); -        +       //traditional method of traversing the map set 1; KeySet () A       //traditionalMethod1 (maps); at       //traditional method of traversing the map set 2; EntrySet () -       //TRADITIONALMETHOD2 (maps); -       //use the enhanced for loop to traverse the Map collection Method 1; KeySet () -       //strongForMethod1 (maps); -       //use the enhanced for loop to traverse the Map collection Method 2; EntrySet () - STRONGFORMETHOD2 (maps); in    } -  to    Private Static voidSTRONGFORMETHOD2 (map<string, string>maps) { +Set<entry<string, string>> set =Maps.entryset (); -        for(Entry<string, string>Entry:set) { theString key =Entry.getkey (); *String value =Entry.getvalue (); $SYSTEM.OUT.PRINTLN (key + ":" +value);Panax Notoginseng       } -    } the  +    Private Static voidStrongForMethod1 (map<string, string>maps) { ASet<string> set =Maps.keyset (); the        for(String s:set) { +String key =s; -String value =Maps.get (s); $SYSTEM.OUT.PRINTLN (key + ":" +value); $       } -    } -  the    //Use the EntrySet () method to get each key-value pair in the Maps collection, -    Private Static voidTRADITIONALMETHOD2 (map<string, string>maps) {Wuyiset<map.entry<string, string>> sets =Maps.entryset (); the       //gets the iterator that iterates out the corresponding value.  -Iterator<entry<string, string>> it =sets.iterator (); Wu        while(It.hasnext ()) { -map.entry<string, string> Entry = (entry<string, string>) It.next (); AboutString key =Entry.getkey (); $String value =Entry.getvalue (); -SYSTEM.OUT.PRINTLN (key + ":" +value); -       } -    } A  +    //Use the Keyset () method to get all the keys in the Maps collection and traverse the keys to get the corresponding values.  the    Private Static voidTraditionalMethod1 (map<string, string>maps) { -Set<string> sets =Maps.keyset (); $       //gets the iterator to traverse the corresponding value theIterator<string> it =sets.iterator (); the        while(It.hasnext ()) { theString key =It.next (); theString value =Maps.get (key); -SYSTEM.OUT.PRINTLN (key + ":" +value); in       } the    } the}

Java Instance-Collection traversal

Related Article

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.