Java Collection Series II: A variety of methods for collection traversal, and encapsulated as a public method

Source: Internet
Author: User
Tags repetition set set

One: How to use the For loop and iterator to traverse the map collection text address (reprint)

1, through the key set access, to key interest, you can access the value corresponding to the key;

For (String K:maps.keyset ()) {  
           System.out.println (k +: +maps.get);  
       }  

2, through the value set access, only the value of interest, can not access the key value;

For (String value:maps.values ()) {  
           System.out.println (value);  
       }  

3, through Entry collection access, interested in entry, you can access the value corresponding to the key

For (Entry Entry:maps.entrySet ()) {       System.out.println (Entry.getkey () + ":" +entry.getvalue ());  
       }  

4, through the iteration key set to access the Map collection, Maps.keyset () returns a set set, the set directly inherits collection, so it can be iterated.

Iterator<string> iterator = Maps.keyset (). iterator ();  
        while (Iterator.hasnext ()) {  
            String key = Iterator.next ();  
            System.out.println (key+ ":" +maps.get (key));  
        }  

5, through the iteration values collection to access the Map collection, maps.values () returns the collection, so you can iterate over it.

Iterator<string> iter= maps.values (). iterator ();  
        while (Iter.hasnext ()) {  
            System.out.println (Iter.next ());  
        }

6, by iterating over the entry collection to access the Map collection, Maps.entryset () returns a set

Iterator<entry<string, string>> it = Maps.entryset (). iterator ();  
        while (It.hasnext ()) {  
            entry<string, string> Entry = It.next ();       System.out.println (Entry.getkey () + ":" +entry.getvalue ());  
        }  

Two: Continue to use foreach Traversal collection: Original address
Can be encapsulated as a public method for the traversal of various collections

Public method publicly
static void Foreachcollection (Collection<string> colleciton) {for  
        (String Str:colleciton {////saves the object  
            System.out.print (str+ "") in the order in which it was added;  
        }  
        System.out.println ();  
    }
 Test method public
 static void Listcollection () {  
        string[] array = {"A", "a", "B", "E", "F", "G", "C", "D"};  
        Foreachcollection (New arraylist<string> (Arrays.aslist (array));//a A B E F G C D   
        foreachcollection (new linkedlist<string> arrays.aslist (array))//Save object in the order added//a A B E F G C D   
        foreachcollection (New hashset< String> (array))/no repetition, disorder, storage order has no practical meaning//d E F G A B C   
        foreachcollection (New treeset<string> (Arrays.aslist (array)); /No Duplicates, save the object in ascending order of the comparison result//a B C D E F G   
        foreachcollection (New linkedhashset<string> (Arrays.aslist (array));/No repetition , save the object in the order that it was added//a B E F G C D   
    }     

Three: Iterate over the collection using iterations (in the same way packaged into a common method for use)

Common method public class Foreachdemo {publicly static void Printcollection (Collection<string> colleciton) {  
        Iterator<string> it = Colleciton.iterator ();  
        while (It.hasnext ()) {System.out.print (It.next () + "");  
    } System.out.println ();  
        ///test method public static void main (string[] args) {string[] array = {"A", "a", "B", "E", "F", "G", "C", "D"}; Printcollection (New arraylist<string> (Arrays.aslist (array));//a A B E F G C D printcollection (NE W linkedlist<string> (Arrays.aslist (array))//Save objects in the order added//a A B E F G C D printcollection (New hashset& Lt string> arrays.aslist (array));/no repetition, disorder, storage order has no practical meaning//d E F G A B C printcollection (New treeset<string> ( Arrays.aslist (array));//No Duplicates, save objects in ascending order of comparison//a B C D E F G printcollection (new linkedhashset<string> (array  
 S.aslist (array));/No duplicates, save object in the order added//a B E F G C D}

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.