The value () method differs from keyset (), EntrySet () in the Map collection

Source: Internet
Author: User

http://blog.csdn.net/liu826710/article/details/9001254

In the Map collection

values (): The method is to get all the values in the collection----no keys, no correspondence,

KeySet ():
Deposits all the keys in the map into the set collection. Because set has iterators. All of the keys can be iterated out and then based on the Get method. Gets the value corresponding to each key. KeySet (): The key can only be taken after the iteration via get ()

EntrySet ():

Set<map.entry<k,v>> EntrySet ()//Returns a Set view of the mapping relationships contained in this map. Map.entry represents a mapping relationship. EntrySet (): After iteration you can E.getkey (), E.getvalue () take key and value. The entry interface is returned.

Let's look at the following examples:

map<string,string> map = new hashmap<string,string> ();
Map.put ("Zhangsan");
Map.put ("Lisi");
Map.put ("Wangwu");

collection<string> Collection = Map.values ();//The return value is a Collection collection of values
System.out.println (collection);
Printing results:
[Zhangsan, Lisi, Wangwu]


Set<k> KeySet ()///return value is a set set that holds only the key value (unordered in the collection)

Set<map.entry<k,v>> EntrySet ()//Returns a set set of mapping relationships contained by a map (a relationship is a key-value pair), which is the (Key-value) stored in the set set as a whole pair of pairs.



A. KeySet () method.

map<string,string> map = new hashmap<string,string> ();

Map.put ("Zhangsan");
Map.put ("Lisi");
Map.put ("Wangwu");


set<string> KeySet = Map.keyset ();//Get the set set of all keys for the map collection first

Iterator<string> it = Keyset.iterator ();//If you have a set set, you can get its iterator.

while (It.hasnext ()) {
String key = It.next ();
String value = Map.get (key);//A key can get its corresponding value through the Get method of the map collection.

System.out.println ("Key:" +key+ "-->value:" +value);//Get Key and value
}

Two. EntrySet () mode:
map<string,string> map = new hashmap<string,string> ();

Map.put ("Zhangsan");
Map.put ("Lisi");
Map.put ("Wangwu");

The mapping relationship in the map collection is taken out by the EntrySet () method (the relationship is the Map.entry type)
set<map.entry<string, string>> entryset = Map.entryset ();

Iterate over the entryset of the relationship collection and store it in an iterator
iterator<map.entry<string, string>> it2 = Entryset.iterator ();

while (It2.hasnext ()) {
Map.entry<string, string> me = It2.next ();//Get Map.entry Relationship Object me
String Key2 = Me.getkey ();//Get key through a relational object
String value2 = Me.getvalue ();//Get value from a relational object

System.out.println ("Key:" +key2+ "-->value:" +value2);
}

Although using keyset and entryset to traverse can achieve the same result
But the traverse speed of the two is different.

KeySet (): The key can only be taken after the iteration via get ()
EntrySet (): After iteration you can E.getkey (), E.getvalue () take key and value. The entry interface is returned

Note: The speed of KeySet () is much slower than entryset (), that is, KeySet way to traverse map performance is not as good as EntrySet performance
to improve performance, consider using EntrySet () as a way to iterate.

The value () method differs from keyset (), EntrySet () in the Map collection

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.