Java Traversal Object

Source: Internet
Author: User

in JDK1.4

Map map = new HashMap ();

    Iterator it = Map.entryset (). iterator ();

    while (It.hasnext ()) {

        Map.entry Entry = (map.entry) it.next ();

        Object key = Entry.getkey ();

        Object value = Entry.getvalue ();

}

JDK1.5, the application of new characteristics For-each cycle

Map m = new HashMap ();

For (Object O:map.keyset ()) {

    map.get (o);

}

Each element in the returned set is a map.entry type.

Private hashtable<string, string> emails = new hashtable<string, string> ();

In addition, we can change the HashMap to a set collection, then iterate the output, but the resulting object

Method One: Use EntrySet ()

   Iterator it = Emails.entryset (). iterator ();

   while (It.hasnext ()) {

    map.entry m= (map.entry) It.next ();

    Logger.info ("email-" + m.getkey () + ":" + m.getvalue ());

   }

  

   Method Two: jdk1.5 support, with EntrySet () and For-each Loop () for

   (map.entry<string, string> m:emails.entryset ()) {

   

    Logger.info ("email-" + m.getkey () + ":" + m.getvalue ());

   }

  

   Method Three: Use Keyset ()

   Iterator it = Emails.keyset (). iterator ();

   while (It.hasnext ()) {

    String key;

    key= (String) It.next ();

    Logger.info ("email-" + key + ":" + emails.get (key));

   }

Method Five: jdk1.5 support, with keyset () and For-each loops for

(Object m:emails.keyset ()) {

    logger.info ("email-" + m+ ":" + Emails.get (m));

   

Map AA = new HashMap ();

Aa.put ("Tmp1", New Object ());

Append substitution with the same function.

Aa.remove ("Temp1");

Delete

for (Iterator i = aa.values (). iterator ();    I.hasnext (); )    {

Object temp = I.next ();

}//Traversal

To a complete, treeset element that contains the interior sort.

public static void Main (string[] args) {arraylist<string> list = new arraylist<string> ();

   hashmap<object,object> hash = new hashmap<object,object> ();

   treemap<object,object> TreeMap = new treemap<object,object> ();

   List.add ("a");

   List.add ("B");

  

   List.add ("C");

   Hash.put (3, 3);

   Hash.put (4, 4);

   Hash.put (5, 5);

   Hash.put (6, 6);

   Hash.put (1, 1);

  

   Hash.put (2, 2);

   Treemap.put (1, 1);

   Treemap.put (2, 2);

   Treemap.put (3, 3);

   Treemap.put (4, 4);

   Treemap.put (5, 5);

  

   Treemap.put (6, 6);

   List traversal for (String m:list) {System.out.println (M); }//HashMap EntrySet () Traversal for (map.entry<object,object> M:hash.entryset ()) {System.out.println (M.getke

   Y () + "---" +m.getvalue ());

   //hashmap keyset () Traversal for (Object M:hash.keyset ()) {System.out.println (m+ "---" +hash.get (m)); }//TreeMap keyset () Traversal for (Object M:treemap.keyset ()) {SYSTEM.OUt.println (m+ "---" +treemap.get (m)); }

}

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.