Collection, MAP, array traversal mode

Source: Internet
Author: User

One, for each underlying implementation

For the collection,for each is implicitly called iterator implementation, the efficiency is slightly lower than the display call iterator, for the array,for each is achieved by the subscript reference, the efficiency is lower than the for loop. For Each returns a collection object, so you cannot use for each to assign a value.

Second, the collection implements the iterator interface, so the list, Queue, and set can all be traversed using the for each method.

Three, hashmap three kinds of traverse way:

map.entryset () returns a set<entry<k,v>>, and then uses for each (implicitly calls iterator)

map<string, string> map = new hashmap<string, string> (); for (entry<string, string> Entry:map.entrySet ()) {    entry.getkey ();    Entry.getvalue ();}

②map.keyset () returns a Set<key>, and then uses for each (implicitly calls iterator)

map<string, string> map = new hashmap<string, string> (); for (String Key:map.keySet ()) {map.get (key);}

③map.value () returns a Collection<value>, and then uses for each (implicitly calls iterator)

Array goto List

String[] Strarray = {"AAA", "BBB", "CCC"};
List list= arrays.aslist (Strarray);

Note that list is ArrayList type, but it is not the same as java.util.ArrayList. Strarray must be an object array, and if it is a primitive type array, List.size () is 1,

Collection to Array
Direct use of the collection ToArray () method

Map Turn Collection
Use the values () method of the map directly.
List and set conversions

A collection object can be passed directly through the constructor function.

Reference: http://www.cnblogs.com/xwdreamer/archive/2012/05/30/2526822.html

Collection, MAP, array traversal mode

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.