Java map and map.entry details and uses

Source: Internet
Author: User

The map is an interface in Java, and Map.entry is an internal interface to the map.

Map provides a number of common methods, such as keyset (), EntrySet (), and so on.

The return value of the KeySet () method is a collection of key values in map, and the return value of EntrySet () returns a set collection of type Map.entry.

Map.entry is an internal interface to the map declaration, which is generic and defined as entry<k,v>. It represents an entity (a Key-value pair) in a map. The interface has a getkey (), GetValue method.

From the above can be drawn, the common method of traversing map:

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 a traversal method, simply traversing the value values, map has a values method, which returns the collection collection of value. By traversing collection, you can also traverse value, such as

Map map = new HashMap ();

Collection C = map.values ();

Iterator Iterator = C.iterator ();

while (Iterator.hasnext ()) {

Object value = Iterator.next ();

}

Java map and map.entry details and uses

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.