Java traversal of collections and the use of enhanced for loop (foreach)

Source: Internet
Author: User
Tags set set

The use of Java collection classes can be said to be ubiquitous, the total we can be divided into three chunks, respectively, from the collection interface extension of the list, set and the form of key-value pairs to store the map type collection.     Many situations require that we iterate through the elements in the collection and do the appropriate processing. The following is a summary of the traversal of various types of collections, with regard to enhancing the For loop, it is important to note that the array subscript value cannot be accessed using an enhanced for loop, and that the traversal of the collection is also a iterator related method. If you only do simple traversal reading, the enhanced for loop does reduce a lot of code. About the list and set type collection traversal:
 1 Import java.util.ArrayList; 2 Import Java.util.HashSet; 3 Import Java.util.Iterator; 4 Import java.util.List; 5 Import Java.util.Set; 6 7 public class Listandsettest {8 9 public static void Main (string[] args) {ten//List of collections Test (); The traversal of the set set of Settest ();}15 + private static void Settest () {Set<strin         g> set = new Hashset<string> (), Set.add ("JAVA"), Set.add ("C"), Set.add ("C + +"); 21 The repetition of the addition does not go in. Set.add ("JAVA"), Set.add ("JAVASCRIPT"),//set collection Traversal Method 1, using Iterator26 iterator<st Ring> it = Set.iterator (); It.hasnext ()) {String value = It.next (); syste M.out.println (value),}31//set Collection Traversal Method 2, using the enhanced for loop.  S:set (String) {System.out.println (s); 35}36}37 38//Traverse List Collection         static void Listtest () {40list<string> list = new arraylist<string> (), List.add ("java111"), List.add ("java222"); 43 List.add ("java333"), List.add ("java444"), List.add ("java555"), 46 47//Traversal mode 1, using Iterato R48 iterator<string> it = List.iterator (); (It.hasnext ()) {. String value = it. Next (); System.out.println (value), 52}53 54//Traversal Method 2, traversing with a traditional for loop.             for (int i = 0, size = list.size (); i < size; i++) {String value = List.get (i); 57 System.out.println (value), 58}59 60//Traverse Method 3, traverse with enhanced for loop. Value:list (String) {System.out.println (value); 63}64}65}

For a traversal of the collection of map types, the KeySet () and EntrySet () methods:

 1//Enhanced for Loop 2 public class Maptest {3 4 public static void main (string[] args) {5//Create a HashMap object and add some keys value pairs. 6 map<string, string> maps = new hashmap<string, string> (); 7 Maps.put ("111", "java111"); 8 Maps.put ("222", "java222");         9 Maps.put ("333", "java333"), Maps.put ("444", "java444"), Maps.put ("555", "java555"); 12 13//Traditional method of traversing the map set 1; KeySet ()//traditionalmethod1 (maps); 15//traditional method of traversing map collection 2; EntrySet ()//traditionalmethod2 (maps), 17//using the enhanced for loop to traverse the Map collection Method 1; KeySet ()//strongformethod1 (maps); 19//Use the enhanced for loop to traverse the Map collection Method 2; EntrySet () strongForMethod2 (maps),}22 the private static void StrongForMethod2 (Map<string, STRING&G T  Maps) {set<entry<string, string>> Set = Maps.entryset (); (Entry<string, string> Entry:set) {String key = Entry.getkey (); string value = Entry.getvalue (); System.out.println (key + ":" + value),}30}31 + private S tatic void StrongForMethod1 (map<string, string> maps) {set<string> Set = Maps.keyset (); or (string s:set) {string key = s;36 String value = Maps.get (s); PNs System.out.pri NTLN (key + ":" + value); 38}39}40 41//Use the EntrySet () method to get each key-value pair in the Maps collection, private static void Trad ITIONALMETHOD2 (map<string, string> maps) {set<map.entry<string, string>> sets = Maps.entryset (); 44//Gets the iterator to traverse the corresponding value.             Iterator<entry<string, string>> it = Sets.iterator (); (It.hasnext ()) {47 map.entry<string, string> Entry = (entry<string, string>) It.next (); String key = Entry.getkey         (); Entry.getvalue String value = (); System.out.println (key + ":" + value); 51}52}53 54//Use the Keyset () method to get all the keys in the Maps collection and traverse the keys to get the corresponding values. private static void TraditionalMethod1 (map<string, string> maps) {set<string> sets = Maps.ke Yset (); 57//Gets the iterator to traverse the corresponding value. Iterator<string> it = Sets.iterator (); It.hasnext ()) {String key = It.nex     T (); String value = Maps.get (key), System.out.println (key + ":" + value), 63}64 }65}

Java traversal of collections and the use of enhanced for loop (foreach)

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.