Java Map and Map. Entry

Source: Internet
Author: User

Java Map and Map. Entry

Map is an interface in java, and Map. Entry is an internal interface of Map.

Map provides some common methods, such as keySet () and entrySet.

The Return Value of the keySet () method is the Set of key values in the Map. The return value of entrySet () is also a Set, which is of the Map. Entry type.

Map. Entry is an internal interface declared by Map. This interface is generic and defined as Entry. . It indicates an object in Map (a key-value Pair ). The Interface contains the getKey () and getValue methods.

 

From the above, we can conclude that the common methods for Traversing Map are as follows:

1. Map map = new HashMap ();

Irerator iterator = map. entrySet (). iterator ();

While (iterator. hasNext ()){

Map. Entry entry = iterator. next ();

Object key = entry. getKey ();

//

}

2. Map map = new HashMap ();

Set keySet = map. keySet ();

Irerator iterator = keySet. iterator;

While (iterator. hasNext ()){

Object key = iterator. next ();

Object value = map. get (key );

//

}

 

In addition, there is also a Traversal method, simply traversing the value, Map has a values method, the returned value Collection. You can also traverse the value by traversing the collection, as shown in figure

Map map = new HashMap ();

Collection c = map. values ();

Iterator iterator = c. iterator ();

While (iterator. hasNext ()){

Object value = iterator. next ();

}

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.