Java Instance-Collection traversal

Source: Internet
Author: User
Tags set set

The following example shows how to traverse a collection of list, set, and map types stored as key-value pairs from the collection interface, using normal for, enhanced for, iterator, and so on to iterate through the collection:

Import Java.util.arraylist;import java.util.hashset;import Java.util.iterator;import Java.util.List;import Java.util.Set;      public class Main {public static void main (string[] args) {//List traversal listtest ();   The traversal settest () of the set set;      } private static void Settest () {set<string> Set = new hashset<string> ();      Set.add ("JAVA");      Set.add ("C");      Set.add ("C + +");      Duplicate Data add failed Set.add ("JAVA");       Set.add ("JAVASCRIPT");      Use Iterator to traverse the set set iterator<string> it = Set.iterator ();         while (It.hasnext ()) {String value = It.next ();      System.out.println (value);      }//Use the enhanced for loop to traverse the set set for (String s:set) {System.out.println (s);      }}//Traverse list collection private static void Listtest () {list<string> list = new arraylist<string> ();      List.add ("dish");      List.add ("Bird");      List.add ("teaching");      List.add ("Cheng");       List.add ("www.runoob.com"); Using Iterator traverse iterator<string> it = List.iterator ();         while (It.hasnext ()) {String value = It.next ();      System.out.println (value);         }//Use a traditional for loop to traverse for (int i = 0, size = list.size (); i < size; i++) {String value = List.get (i);      System.out.println (value);      }//Use an enhanced for loop to traverse for (String value:list) {System.out.println (value); }   }}

  

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:

ImportJava.Util.Map;ImportJava.Util.HashMap;ImportJava.Util.HashSet;ImportJava.Util.Iterator;ImportJava.Util.List;ImportJava.Util.Set;ImportJava.Util.Map.Entry;Enhanced for LoopPublic Class Main { Public Static voidMain(String[]Args) { Creates a HashMap object and joins some key-value pairs. Map<String, String>Maps= New HashMap<String, String> ();Maps.Put("1", "PHP");Maps.Put("2", "Java");Maps.Put("3", C);Maps.Put("4", "C + +");Maps.Put("5", "HTML"); The traditional method of traversing the map set 1; KeySet () TRADITIONALMETHOD1 (maps); The 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); } Private Static voidStrongForMethod2(Map<String, String>Maps) { Set<Entry<String, String>> Set =Maps.EntrySet(); For (Entry<String, String>Entry: Set) { StringKey=Entry.GetKey(); StringValue=Entry.GetValue(); System.Out.println(Key+ " : " +Value); } } Private Static voidStrongForMethod1(Map<String, String>Maps) { Set<String> Set =Maps.KeySet(); For (StringS: Set) { StringKey=S; StringValue=Maps.Get(S); System.Out.println(Key+ " : " +Value); } } Use the EntrySet () method to get each key-value pair in the Maps collection, Private Static voidTraditionalMethod2(Map<String, String>Maps) { Set<Map.Entry<String, String>>Sets=Maps.EntrySet(); Gets the iterator that iterates out the corresponding value. Iterator<Entry<String, String>>It=Sets.Iterator(); While (It.Hasnext()) { Map.Entry<String, String>Entry= (Entry<String, String>)It.Next(); StringKey=Entry.GetKey(); StringValue=Entry.GetValue(); System.Out.println(Key+ " : " +Value); } } Use the keyset () method to get all the keys in the Maps collection and traverse the keys to get the corresponding values. Private Static voidTraditionalMethod1(Map<String, String>Maps) { Set<String>Sets=Maps.KeySet(); Gets the iterator to traverse the corresponding value Iterator<String>It=Sets.Iterator();  while(it.Hasnext()){StringKey=it.Next();Stringvalue=Maps.Get(Key);System. out.println(Key+" : "+value);}}}

Java Instance-Collection traversal

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.