Enhanced for loop, map interface traversal, and variable parameter Methods

Source: Internet
Author: User

Enhance the For Loop

1. The for loop can do things, and most of the enhancement for loops can be done (if you want to obtain the lower mark, you must use a simple for loop)


2. Enhanced for can easily process the traversal of a set, but the standard traversal of a set uses the iterator.


3. It shows its strength in the Collection framework.


Map interface Traversal

Map interface traversal:
1. The first method uses the map. Values () method. First, the value is put in a collection.
2. The second method uses the map. entryset () method (recommended)
3. The third method is map. keyset ().
In fact, there are very few traversal operations on the map interface, and query operations are usually performed on it.


Code operations by traversing values in the map interface

System. Out. println ("========= first method ========= ");
// How to iterate the map content
// The first method uses the map. Values () method. The value is first put in a collection.
Collection <person> collections = map. Values ();

For (person: collections ){

System. Out. println (person );
}

// The second method uses the map. entryset () method (the most recommended method)

System. Out. println ("========= second method ======= ");
// Set <entry <string, person> set = map. entryset ();

For (Entry <string, person> entry: map. entryset ()){

System. Out. println (entry. getvalue ());
}

System. Out. println ("========= third method ======= ");
// Method 3 use map. keyset ()
// Set <string> keyset = map. keyset ();

For (string key: map. keyset ()){
System. Out. println (key );
System. Out. println (Map. Get (key ));
}

Variable parameters

Variable parameters (simple but practical tips)

The appearance of variable parameters facilitates Program Development

Object... ARGs

String... ARGs

Format: (data type... Parameter Name (can be obtained at Will ))




Zookeeper

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.